Here is the 3rd set of PHP notes. With this all the core PHP language features are covered. The only thing left is the OOP support in PHP - classes and interfaces. For classes, I will cover only the PHP 5 features (PHP 4 has a different model). Stay tuned for the final set.
PHP Language Reference Notes - control structures and functions
1. There are two equality operators in PHP. == is for “equals” and === is for “identical”. “===” return true only if both operands are of the same type!
2. @ is an error control operator in PHP. This works only on expressions and will suppress error messages. Handy if you don’t want to expose errors to the page being displayed.
3. Backticks (“) can be used as the execution operator in PHP. for example $output = `ls`; will contain the directory listing in linux.
4. There are two types of logical operators - “and”,”&&” , “or”, “||”. The “or” operator has less precedance than “||”.
5. In arrays, “+” operator indicates union. “==” checks whether same key/pair value exist in arrays, while “===” also checks for their order (identical).
6. Following are the control structures in PHP,
if ($a > $b) {
echo “a is bigger than b”;
} elseif ($a == $b) {
echo “a is equal to b”;
} else {
echo “a is smaller than b”;
}
while ($i <= 10) {
echo $i++;
}
do {
echo $i;
} while ($i > 0);
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
switch ($i) { // switch can work on strings!
case “apple”:
echo “i is apple”;
break;
case “bar”:
echo “i is bar”;
break;
case “cake”:
echo “i is cake”;
break;
}
7. There are two forms of else if in if blocks. It can be “else if” or “elseif”.
8. All control structures has an alternate form which uses colon(:) and end<type>; For example, if($a): endif;
9. PHP has a shortcut form of for, the foreach. Consider the following example,
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as $value) {
echo $value;
}
?>
10. The break statement takes an optional parameter indicating how many levels needs to be skipped. For example, break 2;
11. The continue statement takes an optional parameter indicating how many levels needs to be skipped when going to the next iteration. For example, continue 2; Also note that continue applies to switch statement as well!
12. The declare construct can be used for profiling. For example, declare(ticks=1) { //code here }. This works along with register_tick_function(). The registered function will be called for every n low level instructions, where n is the number of ticks.
13. To include another PHP file use either include() or require() construct. require() will cause a fatal error if the required file is missing. To avoid multiple includes of the same file, use include_once() or require_once().
14. In PHP, it is possible to define functions inside functions! Function visibility is global but the outer function must be called before you can call the inner function!
15. There is no function overriding or overloading in PHP.
16. Function arguments can be passed by value or reference. To pass by reference prefix variable with &. PHP also supports variable function arguments.
17. Default function argument values are possible and they should be ordered from right to left. For example,
function test($a, $b=”value”) {
}
18. PHP also has variable functions. This can be used for implementing function callbacks!
function f1() {
echo “hello”;
}
$functionvar = “f1″;
$functionvar();