PHP Operators

PHP 7 Operators are the mathematical, string, comparison, and logical commands such as
plus, minus, multiply and divide. PHP looks a lot like plain arithmetic; for instance, the following statement outputs 8:

echo 6 + 2; // here ' + ' is a operator

Before moving on to learn what PHP can do for you, take a moment to learn about the
various operators it provides

There are many types of operators in PHP are :

  1. Arithmetic operators
  2. Assignment operators
  3. Comparision operators
  4. Logical operators
  5. Increment and Decrement operators

1. Arithmetic operators

Arithmetic operators do what you would expect. They are used to perform mathematics.
You can use them for the main four operations (plus, minus, multiply, and divide) as
well as to find a modulus (the remainder after a division) and to increment or decrement
a value (see Table 3-1).

Table 3-1. Arithmetic operators

Operator Description Example
+ Addition $j + 1
Subtraction $j − 6
* Multiplication $j * 11
/ Division $j /4
% Modulus(division remainder) $j % 9
++ Increment ++$j
Decrement –$j

2. Assignment operators

These  PHP 7 operators are used to assign values to variables. They start with the very simple
= and move on to +=, −=, and so on (see Table 3-2). The operator += adds the value on the right side to the variable on the left, instead of totally replacing the value on the left.
Thus, if $count starts with the value 5, the statement:

$count += 1;

sets $count to 6, just like the more familiar assignment statement:

$count = $count + 1;

Table 3-2. Assignment operators

Operator Example Equivalent to
= $j = 15 $j = 15
+=  $j += 5 $j = $j + 5
−= $j −= 3 $j = $j – 3
*= $j *= 8 $j = $j * 8
/= $j /= 16 $j = $j / 16
%=  $j %= 4 $j = $j % 4
.= $j .= $k $j =$j.$k

 

3. Comparison operators

Comparison operators are generally used inside a construct such as an if statement in
which you need to compare two items. For example, you may wish to know whether a variable you have been incrementing has reached a specific value, or whether another
a variable is less than a set value, and so on (see Table 3-3).
Note the difference between = and ==. The first is an assignment operator, and the second is a comparison operator. Even more advanced programmers can sometimes transpose the two when coding hurriedly, so be careful.

Table 3-3. Comparison operators

Operator Description Example
== Is equal to $j == 4
!=  Is not equal to  $j != 21
> Is greater than $j > 3
< Is less than $j < 100
>= Is greater than or equal to $j >= 15
<= Is less than or equal to $j <= 8

 

4. Logical operators

If you haven’t used them before, logical operators may at first seem a little daunting. But just think of them the way you would use logic in English. For example, you might say to yourself, “If the time is later than 12 p.m. and earlier than 2 p.m., then have lunch.”
In PHP, the code for this might look something like the following (using military time):

if ($hour > 12 && $hour < 14) dolunch();

Here we have moved the set of instructions for actually going to lunch into a function
that we will have to create later called do lunch. The then of the statement is left out because it is implied and therefore unnecessary.

As the previous example shows, you generally use a logical operator to combine the
results of two of the comparison operators shown in the previous section. A logical
operator can also be input to another logical operator (“If the time is later than 12 p.m. and earlier than 2 p.m., or if the smell of a roast is permeating the hallway and there are plates on the table”). As a rule, if something has a TRUE or FALSE value, it can be input to a logical operator. A logical operator takes two true-or-false inputs and produces a true-or-false result.

Table 3-4 shows the logical operators.

Operator Description Example
&& And  $j == 3 && $k == 2
and  Low-precedence and  $j == 3 and $k == 2
|| Or $j < 5 || $j > 10
or  Low-precedence or  $j < 5 or $j > 10
! Not ! ($j == $k)
xor  Exclusive or $j xor $k

Note that && is usually interchangeable with and; the same is true for ‘||’ and ‘or’. But ‘and’ and ‘or’ have a lower precedence, so in some cases, you may need extra parentheses to force the required precedence. On the other hand, there are times when only ‘and’ or ‘or’ is acceptable, as in the following statement, which uses an or operator (to be explained
in Upcoming Tutorials):

mysql_select_db($database) or die("Unable to select database");

The most unusual of these operators is xor, which stands for exclusive or and returns a TRUE value if either value is TRUE, but a FALSE value if both inputs are TRUE or both inputs are FALSE. To understand this, imagine that you want to concoct your own cleaner for household items. Ammonia makes a good cleaner, and so does bleach, so you want your cleaner to have one of these. But the cleaner must not have both because the combination is hazardous. In PHP, you could represent this as:

 

$ingredient = $ammonia xor $bleach;

In the example snippet, if either $ammonia or $bleach is TRUE, the $ ingredient will also be set to TRUE. But if both are TRUE or both are FALSE, the $ ingredient will be set to FALSE.

Variable Assignment

The syntax to assign a value to a variable is always variable = value. Or, to reassign the
value to another variable, it is other variable = variable.

There are also a couple of other assignment operators that you will find useful. For
example, we’ve already seen:

$x += 10;

which tells the PHP parser to add the value on the right (in this instance, the value 10)
to the variable $x. Likewise, we could subtract as follows:

$y −= 10;

5. Variable incrementing and decrementing

Adding or subtracting 1 is such a common operation that PHP provides special operators for it. You can use one of the following in place of the += and −= operators:

++$x;
−−$y;

In conjunction with a test (an if statement), you could use the following code:

if (++$x == 10) echo $x;

This tells PHP to first increment the value of $x and then test whether it has the value
10; if it does, output its value. But you can also require PHP to increment (or, in the
following example, decrement) a variable after it has tested the value, like this:

if ($y−− == 0) echo $y;

which gives a subtly different result. Suppose $y starts out as 0 before the statement is
executed. The comparison will return a TRUE result, but $y will be set to −1 after the
comparison is made. So what will the echo statement display: 0 or −1? Try to guess, and
then try out the statement in a PHP processor to confirm. Because this combination of
statements is confusing, it should be taken as just an educational example and not as a
guide to good programming style.
In short, whether a variable is incremented or decremented before or after testing de‐
pends on whether the increment or decrement operator is placed before or after the
variable.
By the way, the correct answer to the previous question is that the echo statement will
display the result −1, because $y was decremented right after it was accessed in the if
a statement and before the echo statement.
String concatenation
String concatenation uses the period (.) to append one string of characters to another.
The simplest way to do this is as follows:

echo "You have " . $msgs . " messages.";

Assuming that the variable ‘$msgs’ is set to the value 5, the output from this line of code
will be:

You have 5 messages.

Just as you can add a value to a numeric variable with the += operator, you can append
one string to another using .= like this:

$bulletin .= $newsflash;

In this case, if $bulletin contains a news bulletin and $newsflash has a news flash, the
command appends the news flash to the news bulletin so that $bulletin now comprises
both strings of text.

String concatenation (Imp)

String concatenation uses the period (.) to append one string of characters to another.
The simplest way to do this is as follows:

echo "You have " . $msgs . " messages.";

Assuming that the variable $msgs is set to the value 5, the output from this line of code will be:

You have 5 messages.

Just as you can add a value to a numeric variable with the += operator, you can append
one string to another using .= like this:

$bulletin .= $newsflash;

In this case, if $bulletin contains a news bulletin and $newsflash has a news flash, the
command appends the news flash to the news bulletin so that $bulletin now comprises
both strings of text.

 

 

<< Prev Next >>

Loading