Error Control Operators
When
the at sign
(@) is prepended to an expression in PHP, any error
messages that might be generated by that expression will be ignored.
If the track_errors feature is enabled, any error message generated
by the expression will be saved in the variable $php_errormsg. This
variable will be overwritten on each error, so check early if you
want to use it.
<?php
/* Intentional file error */
$my_file = @file ('non_existent_file') or
die ("Failed opening file: error was '$php_errormsg'");
// this works for any expression, not just functions:
$value = @$cache[$key];
// will not issue a notice if the index $key doesn't exist.
?>
Note: The @-operator works only on expressions. A simple rule of
thumb is: if you can take the value of something, you can prepend
the @ operator to it.
For instance, you can prepend it to variables,
function and include() calls, constants, and so forth. You cannot
prepend it to function or class definitions, or conditional structures
such as if and foreach, and so forth.
Execution operator
PHP supports one execution operator: backticks
(``). Note that these
are not single-quotes!
PHP will attempt to execute the contents of
the backticks as a shell command; the output will be returned (i.e.,
it won't simply be dumped to output; it can be assigned to a variable).
Use of the backtick operator is identical to shell_exec().
<?php
$output = `ls -al`;
echo "<pre>$output</pre>";
?>
Increment/decrement Operators
| Example |
Name |
Effect |
| ++$a |
Pre-increment |
Increments $a by one, then returns $a. |
| $a++ |
Post-increment |
Returns $a, then increments $a by one. |
| --$a |
Pre-decrement |
Decrements $a by one, then returns $a. |
| $a-- |
Post-decrement |
Returns $a, then decrements $a by one. |
The increment/decrement operators do not affect boolean values.
Decrementing NULL values has no effect too, but incrementing them
results in 1.
PHP follows Perl's convention
when dealing with arithmetic operations on character variables
and not C's.
For example, in Perl 'Z'+1 turns into 'AA', while in
C 'Z'+1 turns into '[' ( ord('Z') == 90, ord('[') == 91 ). Note
that character variables can be incremented but not decremented.
Logical Operators
| Example |
Name |
Result |
| $a and $b |
And |
TRUE if both $a and $b are TRUE. |
| $a or $b |
Or |
TRUE if either $a or $b
is TRUE. |
| $a xor $b |
Xor |
TRUE if either $a or $b
is TRUE, but not both. |
| ! $a |
Not |
TRUE if $a is not TRUE. |
| $a && $b |
And |
TRUE if both $a and $b are TRUE. |
| $a || $b |
Or |
TRUE if either $a or $b
is TRUE. |
Two different variations of "and" and "or" operators is that they
operate at different precedences.
String Operators
There are two string operators:
- concatenation
operator ('.'), which returns the concatenation of its
right and left arguments.
- concatenating assignment
operator ('.='), which appends the argument on the right
side to the argument on the left side. Please read Assignment
Operators for more information.
<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello
World!"
$a = "Hello ";
$a .= "World!"; // now $a contains "Hello World!"
?>
Array Operators
| Example |
Name |
Result |
| $a + $b |
Union |
Union of $a and $b. |
| $a == $b |
Equality |
TRUE if $a and $b have the
same key/value pairs. |
| $a === $b |
Identity |
TRUE if $a and $b have the
same key/value pairs in the same order and of the same types. |
| $a != $b |
Inequality |
TRUE if $a is not equal
to $b. |
| $a <> $b |
Inequality |
TRUE if $a is not equal
to $b. |
| $a !== $b |
Non-identity |
TRUE if $a is not identical
to $b. |
The + operator appends the right handed
array to the left handed, whereas duplicated keys are NOT overwritten.
Elements of arrays are equal for the comparison if they have the same
key and value.
Comparing arrays
<?php
$a = array("apple", "banana");
$b = array(1 => "banana", "0" => "apple");
var_dump($a == $b); // bool(true)
var_dump($a === $b); // bool(false)
?>
Type Operators
PHP has a single type operator: instanceof. instanceof is used
to determine whether a given object is of a specified object
class.
The instanceof operator was introduced in PHP 5. Before this time
is_a() was used but is_a() has since been deprecated in favor of
instanceof.
<?php
class A { }
class B { }
$thing = new A;
if ($thing instanceof A) {
echo 'A';
}
if ($thing instanceof B) {
echo 'B';
}
?>
As $thing is an object of type A, but not B, only the block dependent
on the A type will be executed: