Numeric values operators - Arithmetic Operators
Arithmetic operators are used to perform normal arithmetical operations among variables or variables and other mathematical values. With them you can perform addition, multiplication, subtraction etc.
|Addition||+||$a + $b||Sum of the variables $a and $b|
|Subtraction||-||$a - $b||Difference of the variables $a and $b|
|Multiplication||*||$a * $b||Product of the variables $a and $b|
|Division||/||$a / $b||Partition of variables $a and $b|
|Modulus||%||$a % $b||Remainder of the variable $a divided by $b|
|Exponentiation||**||$a ** $b||Result of raising the variable $a to the $b'th power|
<?php $a = 10; $b = 4; //Addition $addition = $a + $b; echo $addition."\n"; // outputs 14 $subtraction = $a - $b; echo $subtraction."\n"; //outputs 6 $multiplication = $a * $b; echo $multiplication."\n"; //outputs 40 $division = $a / $b; echo $division."\n"; //outputs 2.5 $modulus = $a % $b; echo $modulus."\n"; /*outputs 2 8 is the maximum multiplication number that 4 can have inside 10, so we are left with remaining 2 */ $exponentiation = $a ** $b; echo $exponentiation."\n"; //outputs 10000
Numeric values operators - Assignment Operators
The assignment operator are used to write (assign) a numberic value to a variable. The default assignment operator in PHP is "=" and it assigns to the variable from the left side the value of the expression from the right side.
|$a = $b||$a = $b||The variable from left will get the value of the expression from right|
|$a += $b||$a = $a + $b||Addition|
|$a -= $b||$a = $a - $b||Subtraction|
|$a *= $b||$a = $a * $b||Multiplication|
|$a /= $b||$a = $a / $b||Division|
|$a %= $b||$a = $a % $b||Modulus|
Numeric values operators - Increment / Decrement Operators
The incremental operator increments the variables value by 1. The decremental operator decrements the variable's value by 1.
|++$a||Pre-increment||Increments $a by 1, then returns the value of $a|
|$a++||Post-increment||Returns the value of $a, then increments $a by 1|
|--$a||Pre-decrement||Decrements $a by 1, then returns the value of $a|
|$a--||Post-decrement||Returns the value of $a, then decrements $a by 1|
<?php $a = 5; ++$a; //the value of $a is now 6 $a++; //the value of $a is now 7 --$a; //the value of $a is now 6 $a--; //the value of $a is now 5 $a = 5; $b = 0; $b = ++$a; /* the value of $b is 6 and also the value of $a is 6, since $a has first been incremented by 1 and then it's value has been returned to $b */ $a = 5; $b = 0; $b = $a++; /* the value of $b is 5 and the value of $a is 6, since $a has first returned the initial value of 5 and then it's value has been incremented by 1 */ $a = 5; $b = 0; $b = --$a; /* the value of $b is 4 and also the value of $a is 4, since $a has first been decremented by 1 and then it's value has been returned */ $a = 5; $b = 0; $b = $a--; /* the value of $b is 5 and the value of $a is 4, since $a has first returned the initial value of 5 and then it's value has been decremented by 1 */ ?>
Comparison operators - Equal, identical
This operators are represented by:
|==||Equal||$a == $b||If the variable $a is equal with $b will return true|
|===||Identical||$a === $b||If the variable $a is equal with $b and they are both the same data type (like both string, integer, bool etc.) will return true|
Beside the fact that both operators are checking if the variables are equal, the 'Identical' operator check if they are also the same data type, like integer, string or bool.
So, for PHP the string "1" is equal with the number 1, even though they are different data types, to be sure that they are also the same data type you need to use the 'Identical' operator. Also, the number 0 (zero) is equal with the boolean value FALSE and with an empty string.
<?php $a = 20; $b = '20'; $c = 0; $d = false; var_dump($a == $b); // returns bool(true), so it's true var_dump($a === $b); /* returns bool(false), because even though the value is 20 in both variables, the one from the left is integer and the one in from the right is string */ var_dump($c == $d); // returns bool(true) var_dump($c === $d); // returns bool(false) ?>
The PHP var_dump function is used to display a variable or value, same as with echo, but it will give more information about the structure and the data type of the variable.
Comparison operators - Not equal, not identical
As their names says, they check if two variables or value are not equal, and for the identical if they also have same data type. They are oposite to '==' and '==='.
|!=||Not equal||$a != $b||Wil return true if $a is not equal to $b|
|<>||Not equal||$a <> $b||Wil return (same as !=) true if $a is not equal to $b|
|!==||Not identical||$a !== $b||Wil return true if $a is not equal to $b, and they are not the same data type (like string, integer etc.)|
Comparison operators - Greater than, Less than / Or equal to
This operators are:
|>||Greater than||$a > $b||If $a is greater than $b returns true|
|<||Less than||$a < $b||If $a is less than $b returns true|
|>=||Greater than or equal to||$a >= $b||If $a is greater than or equal to $b returns true|
|<=||Less than or equal to||$a <= $b||If $a is less than or equal to $b returns true|
To make their role more explicit please check the example bellow:
<?php $a = 10; $b = 7; $c = 10; var_dump($a > $b); //will output true var_dump($a < $b); //will output false var_dump($a >= $b); //will output true //since $a it's bigger than $b var_dump($a <= $b); //will output false //since $a it's neither less than $b or equal to $b var_dump($a >= $c); //will output true //since they are equal var_dump($b >= $c); //will output true //since $b it's neither bigger than $c or equal to $c ?>
Comparison operators - Spaceship operator
The spaceship operator <=>, like $a <=> $b, was introduced in PHP 7. It returns -1 if the variable from the left is smaller, 0 if the variables are equal, and 1 if the variable from the left is bigger.
<?php $a = 10; $b = 5; echo $a <=> $b; //will return 1 //since $a is bigger than $b $c = 12; $d = 12; echo $c <=> $d; //will return 0 //since $c is equal to $d $e = 7; $f = 9; echo $e <=> $f; //will return -1 //since $e is smaller than $f
What are the logical operators?
Logical operators are used to check multiple conditions.
For example we want to check in the same time if a variable is bigger than 5 and also to check if another variable is not empty. If the conditions are meet, then the operator will return the boolean value true, else it will return false.
Values that represent false are:
- the FALSE value itself;
- the numeric value 0;
- empty strings and the strings with the value '0';
- arrays with zero elements;
- the NULL value;
As long as your variable doesn't has those values it's true;
The logical operators are:
|$a && $b
$a and $b
|If both $a and $b are true it will return true, else it will return false|
|$a || $b
$a or $b
|If both $a and $b are true it will return true, also if either $a or $b is true it will return true, else it will return false|
|Xor||xor||$a xor $b||If either $a or $b is true, but not both in the same time, it will return true, else it will return false|
|Not||!||!$a||If $a is true it will return false, if $a is false it will return true|
Logical operators - && (and) Operator
The And operator has the symbol &&, it can also be used and but the most common way is '&&'.
This operator is useful when we want to be sure that all our conditions are met, since every conditions needs to be true.
For example, in a registration form we want to be sure that the first name, last name and email are all not empty.
<?php $first_name = 'Alice'; $last_name = 'Wonderland'; $email = ''; var_dump($first_name && $last_name && $email); //will output false, since the variable $email is empty $email = 'firstname.lastname@example.org'; var_dump($first_name && $last_name && $email); //will output true $pet_price = 20; $pet_name = 'White Rabbit'; //check the pet name //and if the price is bigger than 5 var_dump($pet_price > 5 && $pet_name === 'White Rabbit'); //will return true $place_name = 'Wonderland'; $place_location = null; //check if we have values for the variables var_dump($place_name && $place_location); //will output false, since NULL means false to our operator ?>
Logical operators - || (Or) Operator
The Or operator is using the symbol ||, also there is the or symbol, but most commonly is used '||'.
In order for this operator to return true at least one of the conditions must be true.
As an practical example we may want to check if a user has entered as a contact method either his email or phone number.
<?php $email = 'email@example.com'; $phone_number = ''; $address = ''; var_dump($phone_number || $email || $address); /* will output true since the email is not empty, even though the $phone_number and the $address are empty */ $price = 125; $promo_code = 'DISCOUNT'; var_dump($price > 120 || $promo_code === 'DISCOUNT'); /* will output true, even though at least one condition should've been true both are true */ $payment_method = 'cash'; var_dump($payment_method === 'credit_card' || $payment_method === 'paypal'); /* will output false since not even at least one condition was true */ ?>
Logical operators - xor Operator
xor operator is similar to the or ( || ) operator but this time only one option needs to be true. In practice this operator is not so commonly used by developers as and, or and not operators.
One practical example would be for example when having two inputs, lets say we have an article two write and there is a input for setting the article public and another one for setting it draft, and we need to choose only option.
<?php $article_is_public = 1; $article_is_draft = 1; var_dump($article_is_public xor $article_is_draft); //will output false since both variables are true ( 1 equals with true when evaluated) $article_is_draft = 0; var_dump($article_is_public xor $article_is_draft); //will output true since only article_is_public variable is true now
Logical operators - ! (Not) Operator
The Not operator is represented by the symbol !. This operator has the role to invert the logical value of the statement, more simply said if a variables value is true than it will return false, and if the value is false it will return true.
For example we have a variable and we want to get true if the variable's value is an empty string, in this case we can simply put this operator before it.
<?php $name = ''; var_dump(!$name); //will output true because the value of $name is equal to false, but we the operator inverted it $account_activated = true; var_dump(!$account_activated); //will output false, the inverted value $user_email = 'firstname.lastname@example.org'; $user_phone_number = ''; var_dump( !($user_email || $user_phone_number) ); //will return false since the inner statement is evaluated to true, but the ! operator inverted it
What are the conditional assignment operators?
The use of this type of operators is to assign the value of a variable depending on a condition.
The conditional assignment operators are:
- the thernary operator
- the null coalescing operator (introduced in PHP version 7)
Conditional assignment operators - Thernary Operator
The thernary operator will assign the value of a variable after evaluating a statement. If the statement it's true, it will try return its left side to the variable, else it will return the right side.
|Thernary||? :||$a = statement1 ? stament2 : statement3||
Will assign the value of statement2 if stament1 is true
Will assign the value of statement3 if statement1 is false
<?php $product_type = 'clothes'; $tax_percentage = $product_type === 'food' ? 10 : 20; var_dump($tax_percentage); // will output 20 since the condition was evaluate as false and so it return the right side value $product_price = 200; $apply_promotion = $product_price > 150 ? true : false; var_dump($apply_promotion); /* will output true, as the condition was evaluated as true and the variable received the value from the the left */
Conditional assignment operators - Null coalescing operator
The null coalescing operator will set the value of a variable by evaluating one by one the statements separated by ??.
|Null coalescing operator||??||$a = statement1 ?? statement2||The variable $a will get the value of $statement1 if it exists and is not null, else it will get the value of $statement2|
<?php $first_name = 'Joe'; $last_name = 'Johnny'; $contact_name = $first_name ?? $last_name; echo $contact_name; /* it will output the value of $first_name, since it was defined and not null */ $book_third = 'Foundation - Asimov'; $bought_book = $book_one ?? $book_two ?? $book_third; echo $bought_book; /* will output the value of the variable $book_third, since the other two variables are not defined */
What is the output othe following code:
<?php $a = 15; echo ($a % 13)**2; ?>
What is the output of the following code?
<?php $a = 33; $b = 3; $c = 7; $a /= $b; $a %= $c; echo $a; ?>
Are the variables $a and $b equal at the end of the script?
<?php $a = 10; $b = 10; $c = 2; $a += $c++; $b++; ++$b; //end of the script ?>