An operator is a symbol that enables you to use one or more values to produce a new value. An operand is a value that an operator is operating on. A combination of operators and operands that produces a result is called an expression.
You have already seen the assignment operator: =. The assignment operator assigns values to variables:
<?
$x = 2; # assigns the number 2 to $x
$y = "hello world"; # assigns the string "hello world" to $y
$z = "2"; # assigns the string "2" to $z
?>
Note that when you assign 2 without parentheses, you assign it as a number; with parentheses, a string.
| Mathematical Operators | ||
|---|---|---|
| Operators | Description | Example |
| + | Adds two numbers | $y=$x+$y; |
| - | Subtracts the 2nd number from the 1st number | $y=$x-$y; |
| * | Multiplies two numbers | $y=$x*$y; |
| / | Divides the 1st number by the 2nd. | $y=$x/$y; |
| % |
Finds the remainder when the 1st number is divided by the 2nd. This is called the modulus. |
$y=$x%$y; |
When looping (doing the same operations more than one time) you keep often keep track of how many times you have looped by adding or subtracting 1 from a variable used as a counter. This is so common that there are special operators just for that
| Incrementing/Decrementing Operaters | |||
|---|---|---|---|
| Operater | Example | Equivalent to | |
| ++ | $x++: | $x=$x+1; | |
| -- | $x--; | $x=$x-1; | |
Concationation is the operation of combining two or more strings and the concationation operator is a single period.
<?
$x = "The quick brown fox ";
$y = "jumped over the lazy dog.";
$z = "$x" . "$y";
echo "$z";
# outputs "The quick brown fox jumped over the lazy dog."
?>
<?
$x = "1";
$y = "2";
$z = "$x" . "$y";
echo "$z";
?>
Combined assignment operators are short cuts that combine a mathematical operator or concationation operator with an assignment operator.
| Combined Assignment Operators | ||
|---|---|---|
| Operator | Example | Equivalent to |
| += | $x += 5; | $x = $x+5; |
| -= | $x -= 5; | $x = $x-5; |
| *= | $x *=; | $x = $x*5; |
| /= | $x /= 5; | $x = $x/5; |
| %= | $x %= 5; | $x = $x % 5; |
| .= | $x .= " test"; | $x = $x . " test"; |
The equivalence operator, ==, is often confused with the assignment operater, =. Remember, == tests if two operands are the same, = assigns a value to a variable. |
Comparison operators perform tests on operands and return the boolean values true or false. They are used to control loops and conditional statements. The following is an example of a conditional statement:
<?
$x = 10;
if ( $x == 5 )
{ echo "x = 5"; }
else { echo "x is not 5"; }
?>
The next example is NOT an example of a conditional statement:
<?
$x = 10;
if ( $x = 5 )
{ echo "x = 5"; }
else { echo "x is not 5"; }
?>
The reason it is not a conditional statement is because I used an assignment operator (=) instead of a comparison operator (==) Since I assigned 5 to $x, $x is 5, therefore the statement is always true and "x = 5" is no matter what I assigned to $x earlier.
| Operator | Name | Returns true if... | Example | Result |
|---|---|---|---|---|
| == | Equivalance | Left is equivalent to right | $x == 5 | false |
| != | Non-Equivalance | Left is not equivalent to right | $x != 5 | true |
| > | Greater than | Left is greater than right | $x > 4 | false |
| >= | Greater than or equal to | Left is greater than or equal toright | $x >= 4 | true |
| < | Less than | Left is less than right | $x < 4 | false |
| <= | Less than or equal to | Left is less than or equal to right | $x <= 4 | true |
| === | Identical | Left is equivalent to right and thy are the same type. | $x === 5 | false |
| ? | Ternary operator | Used to compare expressions | ||
Logical operators are used to make complex test expressions for condtional statements and loops
| Logical Operators | ||
|---|---|---|
| Operator | Name | Returns true if... |
| || | Or | Left or Right is true |
| or | Or | Left or Right is true |
| xor | Xor (Exclusive Or) | Left or Right is true but not both |
| && | And | Left and Right are true |
| and | And | Left and Right are true |
| ! | Not | The single operand is not true |
| [] | Used for indexing arrays |
| => | Assigns members in associative arrays |
| -> | Used in objects |
| @ | Error suppresion operator. Prevents PHP errors from being displayed to the browser if a function fails. |
Comparison operators compare two variable but often we want to know if one variable has a certain trait. For example, if we have a form, we only want it to do something if data has been entered into it. For that, we can use isset(). If you have a form where the user submits
<?
<input type="text" name="name" />?>
name is set to $name. To test if $name has been set, you do it like this:
isset($name)
If you want to test for the opposite condition, "Is $name not set", use a explamation in front of the expression:
!isset($name)
isset() is covered in more detail in Conditional Statements
| Some variable tests | |
|---|---|
| isset($var) | True if $var is set, even if it is empty |
| empty($var) | True if value is 0 is a string with no characters or is empty |
| is_array($var) | Checks if $var is an array |
| is_float($var) | Checks if $var is a floating point number |
| is_null($var) | Checks if $var is equal to zero |
| is_numeric($string) | Checks if the string $string is numeric |
| is_string($string) | Checks if the string $string is a string |
There are many higher math constants and functions built in to PHP but they are beyond the scope of this beginners tutorial. When working with higher math, it is a good idea to check PHP: Mathematical Functions - Manual first. Several times I have worked out a way to do something only to find out that I could do the same thing much more easily with a built in function.
Referances:
|
|
|