PHP Operators

Revision as of 15:32, 30 May 2007 by Neil (Talk | contribs) (Concatenation of Numbers and Strings in PHP)

Revision as of 15:32, 30 May 2007 by Neil (Talk | contribs) (Concatenation of Numbers and Strings in PHP)

Opertors in PHP, and any other prgramming language for that matter, enable us to perfrom tasks on variables and values such as assign, multiply, add, subtract and concatenate them. Operators take the form of symbols (such as + and -) and combinations of symbols (such as ++ and +=).

Operators in PHP work with operands which specify the variables and values that are to be used in the particalar operation. The number and location of these operands in relation to the operators (i.e. before and/or after the operator) depends on the type of operator in question. Let's take, for example, the following simple expression:

1 + 3;

In this expression we have one operator (the '+') and two operands (the numbers 1 and 3). The '+' operator adds the values of two operands (resulting in a vlaue of 4).

Operators can be combined to create complete expressions:

$myVar = 1 + 3;

In the above example, the assignment operator (=) assigns the result of the addition to the operand represented by the variable $myVar. After evaluating this expression the value of 4 will have been assigned to the variable $myVar.

In this chapter of PHP Essentials we will explore each type of operator and explain how they are used in relation to their operands.

PHP Assignment Operators

We breifly covered the basic PHP assignment operator in the An Introduction to PHP Variables chapter. We will now look at this and other assignment operators in more detail.

The assignment operator is used to assign a value to a variable and is represented by the equals (=) sign. The assignment operator can also be combined with arithmetic operators to combine an assignment with a mathemtatical operation (for example to multiple one value by another and assigning the result to the variable).

The following table lists the seven assignment operators available in PHP, together with descriptions and examples of their use:

OperatorTypeDescriptionExample
=AssignmentSets the value of the left hand operand to the value of the right $myVar = 30;
+=Addition-AssignmentAdds the value of left hand operand to the value of the right hand operand and assigns result to left hand operand$myVar = 10;
$myVar += 5;
-=Subtraction-AssignmentSubtracts the value of right hand operand from the value of the left hand operand and assigns result to left hand operand$myVar = 10;
$myVar -= 5;
*=Multiplication-AssignmentMultiplies the left hand operand by value of the right hand operand assigning result to left hand operand$myVar = 10;
$myVar *= 5;
/=Division-AssignmentDivides the left hand operand by value of the right hand operand assigning result to left hand operand$myVar = 10;
$myVar /= 5;
%=Modulo-AssignmentSets the value of the left hand operand to the remainder of the value after being divided by the right hand operand$myVar = 10;
$myVar %= 5;
.=Concatenation-OperandSets the value of the left hand operand to a string containing a concatenation of its value appended with the string in the right hand operand$sampleString="My color is ";
$sampleString += "blue";

PHP Arithmetic Operators

As the name suggests PHP arithmetic operators provide a mechanism for performing mathematical operations:

OperatorTypeDescriptionExample
+AdditionCalculates the sum of two operands $total = 10 + 20;
-SubtractionCalculates the difference between two operands $total = 10 - 20;
*MultiplicationMultiplies two operands $total = 10 * 20;
/DivisionDivides two operands $total = 10 / 20;
%ModulusReturns the reminader from dividing the first operand by the second $total = 20%10;

PHP String Concatenation Operator

The PHP String concatenation operator is used to combine values to create a string. The concatenator operator is represented by a period/full stop (.) and can be used to build a string from other strings, variables containing non-strings (such as numbers) and even constants:

We will start with the operater in its simplest form concatenating two strings:

echo 'My favorite color is ' . 'blue.';

The above example will display a string that is the result of second string appended to the end of the first string:

My favorite color is blue.

The string concatenation operator can also be used with variables. In the following example the value of the $myString variable is appended to the end of the string:

$myString = "red";

echo 'My favorite color is ' . $myString;

We can also reference constants (see PHP Constants for details on using constants in PHP) when using concatenation:

define (MY_COLOR, "Green");

echo 'My favorite color is ' . MY_COLOR;

The above example will result in the following output:

My favorite color is Green.

Concatenation of Numbers and Strings in PHP

We mentioned at the begining of this section that it is also possible to mix numbers and strings in a concatenation operation to create strings. For example we can include the number 6 in our string as follows:

echo 6 . ' is my lucky number';

The above example will create output as follows:

6 is my lucky number

We can also performa calculation and have the result included in the concatenation:

echo 6 + 5 . ' is my lucky number'

In this example the mathematical operation will be evaluated before the concatenation, thereby producing:

11 is my lucky number

it is important to note an issue when dealing with strings and numbers. While the above works fine because we began the expression with the addition something different happens when we have the addition afer the string:

echo 'My Lucky number is ' . 6 + 5;

The above will produce unexpected results (typically it will output a number such as 5). The reason for this is because we have asked PHP to take a string (My Luck number is ), append the number 6 to it (to produce My Luck number is 6) and then final arithmetically add the number 5 to the string (wihch doesn't make a lot of sense). To resolve this issue we need to tell the PHP pre-processor to perform the addition (6 + 5) before performing the concatenation. We can achieve thjis by surrounding the addition expressoin in parentheses ( and ). Therefore the modified script:

echo 'My Lucky number is ' . (6 + 5);

will now produce the desired output:

My Lucky number is 11