Operators in PHP

Getting Started with PHP
Getting Started with PHP : Home
Hello World
Data Types in PHP
Variables in PHP
Operators in PHP
Decision Making in PHP
Iterations in PHP
Functions in PHP
Arrays in PHP
Strings in PHP
Object Oriented PHP
Classes and Objects
Object Interfaces
Static and Final
Advanced Features in PHP
Session in PHP
Cookies in PHP
Server Variables

Operators in PHP
Operators take one or more value perform operations and give us output. A simple operator is Sum (+) which takes values and give its addition.

Arithmetic Operators: These are just basic mathmatical opertaors we learned.

	$m = 10;
	$n = 2;
	echo $m+$n." "; // Addition
	echo $m-$n." "; // Substraction
	echo $m*$n." "; // Multiplication
	echo $m/$n." "; // Division
	echo $m%$n." "; // Modulus
	/* Do not enclose expression containing operators in
	   " " This will simply put values of variables and print
	   as literal string. You may  see that here*/
	    echo "$m+$n";

Assignment Operator(=): Assigns expression value to a variable on the left side of it and don't confuse it with mathmatical 'equal to'.
Assignment can be by value or by reference which we have already discussed. But keep in mind that Assign by refrence will only work for named variables as it uses memory location and only named variables are allocated memory space which can be reterived later.

Bitwise Operators: Bitwise Operators are the operataors which works with the bits i.e. these operators manipulates the bits. For example ~$a will set the bits which are not set. As this opertaor is manipulating bitwise its a bitwise operator.

Comparision Operators: Comparision Operataors gives Boolean(With exception) as output after comparing the values.

	$a = 5; // Assignment Operator
	$b = $a; // Assignment by value
	$c = &$a; // Assignment by Reference
	echo  var_dump($a = 6)." "; // = Assignment
	echo var_dump($a == 5)." "; // == Comparision Opertaor
	echo $b." ".$c; // $b by value and $c by ref


After seeing this example you will understand the difference between = and ==, = operator assigns value while == give boolean(i.e. compares) and also assignment by value and assignment by reference.

Error control operator(@): PHP provides an error control operator. After putting @ before any exression, error thrown by that expression will be ignored (PHP wil not report that error).

Incrementing/Decrementing Operator: Simply increments or decrements values.

	$a = 5;
	echo ++$a." "; //increments and then return value
	echo $a++." "; //retun value then increment
	echo --$a." "; //decrements and then retun value
	echo $a--." "; //return value then increments
	echo $a;

You can understand the difference in pre-increment/decrement ++$a and post-increment/decrement $a++. Just keep in mind that the value returend will be printed by echo.

Logical Operators: Logical opertaors also return Boolean and are the most useful operators after arithmetic operators.

String Operators: PHP provides two string operators . and .=.
. concatenate two strings and .= concatenate string to itself i.e. $a .= $b is equivalent to $a = $a.$b

	$a = "First";
	$b = $a."Second"; // concatenate 
	echo $b;
	$a .= $b;
	echo $a;

Operator Precedence
When we have multiple operators in an expression precedence will decide which operator to be solved first. Operator of higher precedence will be solved first.
5+6*2 is equal to 17 not 22, because * has higher precedence thatn +. So we can write that as 5+(6*2).
NOTE: While using and/or operators keep in mind that these have lower precedence than = so results might shock you. So try to use &&/// as these have higher precedence.

	echo 2+3*7;
	$a = true and false; // equivalent to ($a = true) and false
	$b = true && false; // equivalent to $b = true && false
	echo var_dump($a);
	echo var_dump($b);

Login to Track Your Progress