Expert Refresh

Basics

1) What is the output of the following code? <?php $x[] = 0; $x['aa'] = 11; echo $x;




2) Which function is capable of outputing one variable value's type?





3) What is the output of the following code? <?php $x = print_r(2); $y = print_r(3, true);







PHP automaticaly converts an unitialized array. <?php $x[] = 0; $x['aa'] = 11; var_dump($x); // --- Look Here --- // // array(2) { [0]=> int(0) ["aa"]=> int(11) } Echo don't display value type, and can't be used with arrays and object. <?php $x = 1; echo $x; // output 1 var_dump($x); // output int(1) // --- Look Here --- // Print_r outputs and return string parameter. <?php $x = 1; $y = print_r($x); // output 1 $y = print_r($x, true); // no output // --- Look Here --- // Only var_dump() is capable of outputing the value type. <?php $x = array(1, 2); var_dump($x); // array(2) { [0]=> int(1) [1]=> int(2) } When an element is added to an array without specifying a key, PHP assigns a number > the greatest key. <?php $a = array (2 => 5); $a[] = 'a'; // This will have a key of 3 // --- Look Here --- // This is true even if the array contains a mix of numerical and string keys. <?php $a = array ('4' => 5, 'a' => 'b'); $a[] = 44; // This will have a key of 5 // --- Look Here --- // Array keys are case-sensitive, but type insensitive. <?php $a = array ( 'A' => 1, 'a' => 2, '1' => 3, // Only '1' is eq with 1 1 => 4, // --- Look Here --- // '01' => 5, ); print_r($a); /* Array ( [A] => 1 [a] => 2 [1] => 4 [01] => 5 // --- Look Here --- // ) */ List constructor assign array's values to individual variables. <?php $arr = array("John", "Smith", "34", "07288333",); list($surname, $name, $age) = $arr; echo "$surname $name is $age years old"; // --- Look Here --- // // Ouput: John Smith is 34 years old List is very usefull for string explode <?php $date = '25/05/2012'; list($day, $month, $year) = explode('/', $date); // --- Look Here --- // List returns keys of an assigned array in reverse order <?php $a = array(1,2,3); list($b[0], $b[1], $b[2]) = $a; print_r($b); // Array ( [2] => 3 [1] => 2 [0] => 1) // --- Look Here --- //


References