Memory App
Programming
 




Convertion

PHP automaticaly converts an unitialized array.

$x[] = 0;

$x['aa'] = 11;

var_dump($x); // array(2) { [0]=> int(0) ["aa"]=> int(11) } 
(1/5)

Values type

Echo don't display value type, and can't be used with arrays and object.

$x = 1;

echo $x; // output 1

var_dump($x); // output int(1)
(2/5)

Output

Print_r outputs and return string parameter.

$x = 1;

$y = print_r($x); // output 1

$y = print_r($x, true); // no output true
Only var_dump() is capable of outputing the value type.

$x = array(1, 2);

var_dump($x); // array(2) { [0]=> int(1) [1]=> int(2) } 
(3/5)

Assigned keys

When an element is added to an array without specifying a key, PHP assigns a number > the greatest key.

$a = array (2 => 5);

$a[] = 'a';  // This will have a key of 3
This is true even if the array contains a mix of numerical and string keys.
    
$a = array ('4' => 5, 'a' => 'b');

$a[] = 44; // This will have a key of 5
Array keys are case-sensitive, but type insensitive.

$a = array (
    'A' => 1,
    'a' => 2,
    '1' => 3, // Only '1' is eq with 1
    1 => 4,
    '01' => 5,   // Look Here
);
(4/5)

List

List constructor assign array's values to individual variables}.

$arr = array("John", "Smith", "34", "07288333",);

list($surname, $name, $age) = $arr;

echo "$surname $name is $age years old"; 

// Ouput: John Smith is 34 years old
List is very usefull for string explode

$date = '25/05/2012';

list($day, $month, $year) = explode('/', $date);
List returns keys of an assigned array in reverse order

$a = array(1,2,3);

list($b[0], $b[1], $b[2]) = $a;

print_r($b);

// Array ( [2] => 3 [1] => 2 [0] => 1)


Questions



Top Reference > Programming
0/0 (80)  
Not Logged