Arithmetic Operators
Example Name Result
$a + $b Addition Sum of $a and $b.
$a - $b Subtraction Difference of $a and $b.
$a * $b Multiplication Product of $a and $b.
$a / $b Division Quotient of $a and $b.
$a % $b Modulus Remainder of $a divided by $b.
The division operator ("/") returns an integer value (the result of an integer division) if the two operands are integers (or strings that get converted to integers) and the quotient is an integer. If either operand is a floating-point value, or the operation results in a non-integer value, a floating-point value is returned.
Assignment Operators
The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the the left operand gets set to the value of the expression on the rights (that is, "gets set to").
The value of an assignment expression is the value assigned. That is, the value of "$a = 3" is 3. This allows you to do some tricky things:
$a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.
In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic and string operators that allow you to use a value in an expression and then set its value to the result of that expression. For example:
$a = 3;
$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;
$b = "Hello ";
$b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";
Note that the assignment copies the original variable to the new one (assignment by value), so changes to one will not affect the other. This may also have relevance if you need to copy something like a large array inside a tight loop. PHP 4 supports assignment by reference, using the $var = &$othervar; syntax, but this is not possible in PHP 3. 'Assignment by reference' means that both variables end up pointing at the same data, and nothing is copied anywhere. To learn more about references, please read References explained.
Bitwise Operators
Bitwise operators allow you to turn specific bits within an integer on or off.
Bitwise Operators
Example Name Result
$a & $b And Bits that are set in both $a and $b are set.
$a | $b Or Bits that are set in either $a or $b are set.
$a ^ $b Xor Bits that are set in $a or $b but not both are set.
~ $a Not Bits that are set in $a are not set, and vice versa.
$a << $b Shift left Shift the bits of $a $b steps to the left (each step means "multiply by two") $a >> $b Shift right Shift the bits of $a $b steps to the right (each step means "divide by two")
Comparison Operators
Comparison operators, as their name implies, allow you to compare two values.
Comparison Operators
Example Name Result
$a == $b Equal True if $a is equal to $b.
$a === $b Identical True if $a is equal to $b, and they are of the same type. (PHP 4 only)
$a != $b Not equal True if $a is not equal to $b.
$a !== $b Not identical True if $a is not equal to $b, or they are not of the same type. (PHP 4 only)
$a < $b Less than True if $a is strictly less than $b. $a > $b Greater than True if $a is strictly greater than $b.
$a <= $b Less than or equal to True if $a is less than or equal to $b. $a >= $b Greater than or equal to True if $a is greater than or equal to $b.
Another conditional operator is the "?:" (or ternary) operator, which operates as in C and many other languages.
(expr1) ? (expr2) : (expr3);
This expression evaluates to expr2 if expr1 evaluates to true, and expr3 if expr1 evaluates to false.
Error Control Operators
PHP supports one error control operator: the at sign (@). When 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 global variable $php_errormsg. This variable will be overwritten on each error, so check early if you want to use it.
Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.
Execution Operators
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).
$output = `ls -al`;
echo "
$output";
Note: The backtick operator is disabled when safe mode is enabled.
Incrementing/Decrementing Operators
PHP supports C-style pre- and post-increment and decrement operators.
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.
Here's a simple example script:
Postincrement";
$a = 5;
echo "Should be 5: " . $a++ . "
\n";
echo "Should be 6: " . $a . "
\n";
echo "
Preincrement
";$a = 5;
echo "Should be 6: " . ++$a . "
\n";
echo "Should be 6: " . $a . "
\n";
echo "
Postdecrement
";$a = 5;
echo "Should be 5: " . $a-- . "
\n";
echo "Should be 4: " . $a . "
\n";
echo "
Predecrement
";$a = 5;
echo "Should be 4: " . --$a . "
\n";
echo "Should be 4: " . $a . "
\n";
?>
Logical Operators
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.
The reason for the two different variations of "and" and "or" operators is that they operate at different precedences.
Operator Precedence
The precedence of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator. Parentheses may be used to force precedence, if necessary. For instance: (1 + 5) * 3 evaluates to 18.
The following table lists the precedence of operators with the lowest-precedence operators listed first.
Operator Precedence
Associativity Operators
left ,
left or
left xor
left and
right print
left = += -= *= /= .= %= &= |= ^= ~= <<= >>=
left ? :
left ||
left &&
left |
left ^
left &
non-associative == != === !==
non-associative < <= > >=
left << >>
left + - .
left * / %
right ! ~ ++ -- (int) (double) (string) (array) (object) @
right [
non-associative new
String Operators
There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the 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.
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"
$a = "Hello ";
$a .= "World!"; // now $a contains "Hello World!"
After we know about operator, the next step is understand about the control structure such as looping structrures and conditional selection that will be discussed in chapter 5
Comments :
0 komentar to “PHP Programing | PHP Basic | chapter 4 Operators”
Post a Comment
Please Report me if there's a dead link or invalid link in this post