PHP Casting

0
4كيلو بايت

Sometimes you need to change a variable from one data type into another, and sometimes you want a variable to have a specific data type. This can be done with casting.


Change Data Type

Casting in PHP is done with these statements:

  • (string) - Converts to data type String
  • (int) - Converts to data type Integer
  • (float) - Converts to data type Float
  • (bool) - Converts to data type Boolean
  • (array) - Converts to data type Array
  • (object) - Converts to data type Object
  • (unset) - Converts to data type NULL

Cast to String

To cast to string, use the (string) statement:

ExampleGet your own PHP Server

$a = 5;       // Integer
$b = 5.34;    // Float
$c = "hello"; // String
$d = true;    // Boolean
$e = NULL;    // NULL

$a = (string) $a;
$b = (string) $b;
$c = (string) $c;
$d = (string) $d;
$e = (string) $e;

//To verify the type of any object in PHP, use the var_dump() function:
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
Try it Yourself »

Cast to Integer

To cast to integer, use the (int) statement:

Example

$a = 5;       // Integer
$b = 5.34;    // Float
$c = "25 kilometers"; // String
$d = "kilometers 25"; // String
$e = "hello"; // String
$f = true;    // Boolean
$g = NULL;    // NULL

$a = (int) $a;
$b = (int) $b;
$c = (int) $c;
$d = (int) $d;
$e = (int) $e;
$f = (int) $f;
$g = (int) $g;
Try it Yourself »

Cast to Float

To cast to float, use the (float) statement:

Example

$a = 5;       // Integer
$b = 5.34;    // Float
$c = "25 kilometers"; // String
$d = "kilometers 25"; // String
$e = "hello"; // String
$f = true;    // Boolean
$g = NULL;    // NULL

$a = (float) $a;
$b = (float) $b;
$c = (float) $c;
$d = (float) $d;
$e = (float) $e;
$f = (float) $f;
$g = (float) $g;
Try it Yourself »

Cast to Boolean

To cast to boolean, use the (bool) statement:

Example

$a = 5;       // Integer
$b = 5.34;    // Float
$c = 0;       // Integer
$d = -1;      // Integer
$e = 0.1;     // Float
$f = "hello"; // String
$g = "";      // String
$h = true;    // Boolean
$i = NULL;    // NULL

$a = (bool) $a;
$b = (bool) $b;
$c = (bool) $c;
$d = (bool) $d;
$e = (bool) $e;
$f = (bool) $f;
$g = (bool) $g;
$h = (bool) $h;
$i = (bool) $i;
Try it Yourself »

If a value is 0, NULL, false, or empty, the (bool) converts it into false, otherwise true.

Even -1 converts to true.


Cast to Array

To cast to array, use the (array) statement:

Example

$a = 5;       // Integer
$b = 5.34;    // Float
$c = "hello"; // String
$d = true;    // Boolean
$e = NULL;    // NULL

$a = (array) $a;
$b = (array) $b;
$c = (array) $c;
$d = (array) $d;
$e = (array) $e;
Try it Yourself »

When converting into arrays, most data types converts into an indexed array with one element.

NULL values converts to an empty array object.

Objects converts into associative arrays where the property names becomes the keys and the property values becomes the values:

Example

Converting Objects into Arrays:

class Car {
  public $color;
  public $model;
  public function __construct($color, $model) {
    $this->color = $color;
    $this->model = $model;
  }
  public function message() {
    return "My car is a " . $this->color . " " . $this->model . "!";
  }
}

$myCar = new Car("red", "Volvo");

$myCar = (array) $myCar;
var_dump($myCar);
Try it Yourself »

Cast to Object

To cast to object, use the (object) statement:

Example

$a = 5;       // Integer
$b = 5.34;    // Float
$c = "hello"; // String
$d = true;    // Boolean
$e = NULL;    // NULL

$a = (object) $a;
$b = (object) $b;
$c = (object) $c;
$d = (object) $d;
$e = (object) $e;
Try it Yourself »

When converting into objects, most data types converts into a object with one property, named "scalar", with the corresponding value.

NULL values converts to an empty object.

Indexed arrays converts into objects with the index number as property name and the value as property value.

Associative arrays converts into objects with the keys as property names and values as property values.

Example

Converting Arrays into Objects:

$a = array("Volvo", "BMW", "Toyota"); // indexed array
$b = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); // associative array

$a = (object) $a;
$b = (object) $b;
Try it Yourself »

Cast to NULL

To cast to NULL, use the (unset) statement:

Example

$a = 5;       // Integer
$b = 5.34;    // Float
$c = "hello"; // String
$d = true;    // Boolean
$e = NULL;    // NULL

$a = (unset) $a;
$b = (unset) $b;
$c = (unset) $c;
$d = (unset) $d;
$e = (unset) $e;
Try it Yourself »
البحث
الأقسام
إقرأ المزيد
أخرى
PHP Multiline Comments
Multi-line Comments Multi-line comments start with /* and end with */. Any text...
بواسطة PHP Tutorial 2024-05-17 07:15:48 0 6كيلو بايت
أخرى
PHP Delete Array Items
Remove Array Item To remove an existing item from an array, you can use...
بواسطة PHP Tutorial 2024-05-17 08:06:22 0 7كيلو بايت
أخرى
PHP Break
The break statement can be used to jump out of different kind of loops. Break in For...
بواسطة PHP Tutorial 2024-05-17 07:50:37 0 5كيلو بايت
أخرى
PHP Array Functions
PHP Array Functions PHP has a set of built-in functions that you can use on arrays....
بواسطة PHP Tutorial 2024-05-17 08:07:42 1 8كيلو بايت
أخرى
PHP Numbers
In this chapter we will look in depth into Integers, Floats, and Number Strings. PHP Numbers...
بواسطة PHP Tutorial 2024-05-17 07:37:05 0 6كيلو بايت
Sociallez https://sociallez.com