PHP

Variable Assignment, Operators and Expressions

We've already described simple examples of assignment, in which a variable is assigned the value of an expression using an equals sign. Most numeric assignments and expressions that work in other high-level languages also work in PHP. In this tutorial we'll discuss the most fundamental parts of PHP programming language: variable assignment, expressions and operators.

Here are some examples variable assignment:

PHP uses the = symbol as an assignment operator. The following line sets the value of $var to 1:

$var = 1;

The variable goes on the left of the equal sign, and the value goes on the right. Because it assigns a value, the equal sign is called the assignment operator.

Assign by reference:
By default PHP assigns all variables other than objects by value and not by reference.PHP has optimizations to make assignment by value faster than assigning by reference, but if you want to assign by reference you can use the & operator as follows:

$var1 = 1;
$var2 = &$var1; // assign by reference
$var2 += 3;
echo $var1; // 4

Assign a string value to a variable:

$var = "test string";

Concatenate two strings together to produce "test string":

$var = "test" . " string";

Add a string to the end of another to produce "test string":

$var = "test";
$var = $var . " string";

Here is a shortcut to add a string to the end of another:

$var = "a";
$var .= " test";
//a test

Arithmetic Operators

Using an operator, you can manipulate the contents of one or more variables or constants to produce a new value. For example, this code uses the addition operator ( + ) to add the values of $x and $y together to produce a new value:

$x = 4;
$ = 7;
$z = $x + $y;

So an operator is a symbol that manipulates one or more values, usually producing a new value in the process. The following list describe the types of arithmetic operators:

OperatorDescription
+sum or addition
-subtraction
/division
*multiplication
%modulus
**exponentiation (PHP 5.6 and above)
++add 1
--subtract 1

Sum integers to produce an integer:

$var = 4 + 7;

The values and variables that are used with an operator are known as operands.

Subtraction, multiplication, and division that might have a result that is a float or an integer, depending on the initial value of $var:

$var = (($var - 5) * 2) / 3;

Multiply to double a value:

$var = $var * 2;
$var *= 2;

Halve a value:

$var = $var / 2;
$var /= 2;

These work with float types too:

$var = 123.45 * 28.2;

Get the remainder of dividing 5 by 4:

$var = 5 % 4; //1

4 exponent (or power) of 2:

$var = 2 ** 4; //16

These all add 1 to $var:

$var = $var + 1;
$var += 1;
$var++;

And these all subtract 1 from $var:

$var = $var - 1;
$var -= 1;
$var--;

If the -- or ++ operator appears before the variable then the interpreter will first evaluate it and then return the changed variable:

$var = 1;
echo ++$var; //2
echo $var;   //2

echo --$var; //1
echo $var;   //1

If the -- or ++ operator appears after the variable then the interpreter will return the variable as it was before the statement executed and then increment the variable:

$var = 1;
echo $var++; //1
echo $var;   //2

echo $var--; //2
echo $var;   //1

There are many mathematical functions available in the math library of PHP for more complex tasks. We introduce some of these in next pages.

String assignments and expressions are similar:

Operator precedence

The precedence of operators in an expression is similar to the precedence defined in any other language. Multiplication and division occur before subtraction and addition, and so on. However, reliance on evaluation order leads to unreadable, confusing code. Rather than memorize the rules, we recommend you construct unambiguous expressions with parentheses, because parentheses have the highest precedence in evaluation.

For example, in the following fragment $variable is assigned a value of 32 because of the precedence of multiplication over addition:

$variable = 2 + 5 * 6;
echo $variable; //32

The result is much clearer if parentheses are used:

$variable = 2 + (5 * 6);
echo $variable; //32

But the following example displays the different result, because parentheses have the highest precedence in evaluation.

$variable = (2 + 5) * 6;
echo $variable; //42

Expressions

Let’s start with the most fundamental part of any programming language: expressions. An expression in PHP is anything that evaluates to a value; it is a combination of values, variables, operators, and functions that results in a value. Here are some examples of expressions:

$str  ='abcdef';
$x = 7;
$y = 4;
$z = $x + $y;
$x -$y;
$x;
4+7;
3;
true;
null;

An expression has a value and a type; for example, the expression 4 + 7 has the value 11 and the type integer, and the expression "abcdef" has the value abcdef and the type string. PHP automatically converts types when combining values in an expression. For example, the expression 4 + 7.0 contains an integer and a float; in this case, PHP considers the integer as a floating-point number, and the result is a float. The type conversions are largely straightforward; however, there are some traps, which are discussed later in this section.

Advertisement:
Advertisement: