This site is mobile accessible. Press the "Tap Here" button to use a different font-size.


Smartphone icons created by Freepik - Flaticon

PHP Casting

Cast to Array

array(1) {
  [0]=>
  int(5)
}
array(1) {
  [0]=>
  float(5.34)
}
array(1) {
  [0]=>
  string(5) "hello"
}
array(1) {
  [0]=>
  bool(true)
}
array(0) {
}

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

NULL values converts to an empty array object.

<?php
// data types to be cast to array
$a = 5;       // Integer
$b = 5.34;    // Float
$c = "hello"; // String
$d = true;    // Boolean
$e = NULL;    // NULL

// casting to array
$a = (array) $a;
$b = (array) $b;
$c = (array) $c;
$d = (array) $d;
$e = (array) $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);
?>