It doesn't mean to be a complete list, and don't ask me why not include this and that. I don't care. :P
1. Flow Control
1.1 If
More 'C'/'Java' like:
if (expression)
statement
elseif (expression)
statement
else
statement
Single statment doesn't need bracket {}.
2. Array
2.1 Hash/Array
Perl hash is now PHP array with arbitrary index. No more '@' symbol.
$array1 = array(
0 => 'value1',
'stringkey' => 'value2',
);
echo $array1[0] . ',' . $array1['stringkey'];
2.2 Append new array element
No need to use 'push'. Just assign it with blank bracket:
$array1[] = 'I will be appended at the end.';
The above only works for simple numeric indexed array. If you use the above type assignment for associative array, PHP will still add to the array, starting with index 0.
If you want to perform similar things for more than one elements, you can use array_push():
array_push($array1, "one value", "two value");
And, if you want to append an array to another array, try array_merge():
$merged_array = array_merge($array1,$array2);
Note that if duplicate keys found in both array, the latter values will overide.
2.3 Loop through array
Do like this:
foreach ($array1 as $value)
echo "value is $value<br>\n";
foreach ($array2 as $key => $value)
echo "key is $key, value is $value<br>\n";
No comments:
Post a Comment