PHP OPERATORS


php tutorial

OPERATORS

php tutorial

In any programming without performing action on value we can't achieve desire results. Operators are used to performing predefined action on value to achieve result.

Operators have been divided in following groups:
Four primitive types

☛ Assignment Operators


An assignment operator is the operator that assign value from variable, expression, function and new value to any relevant type variable.

Example

	<?php
						
		$a=100;  	// Assigning 100 to variable $a by assignment operator
						
		echo $a; 	// Printing $a value on screen by echo
		
	?>
						

☛ Arithmetic Operators


Arithmetic Operator is mathematical function that takes two operands and performs calculation on them.

Following table description give you more details about arithmetic operator.
OperatorDefinition
+ [plus]addition of two variables and return it's result.
Example $c=$a+$b (Assume Like $a=20 & $b=10 result $c=30)
- [minus]subtraction of two variables and return it's result.
Example $c=$a-$b (Assume Like $a=20 & $b=10 result $c=10)
* [multiply]multiply of two variables and return it's result.
Example $c=$a*$b (Assume Like $a=20 & $b=10 result $c=200)
/ [division]division of expression & return it's quotient.
Example $c=$a/$b (Assume Like $a=20 & $b=10 result $c=2)
% [modules]modules operator return remainder of expression.
Example $c=$a%$b (Assume Like $a=20 & $b=10 result $c=0)
** [exponentiation]return the power of expression.
Example $c=$a**$b (Assume Like $a=5 & $b=3 result $c=125)

☛ Comparison Operators


Comparison operator is used to comparing value between variables or expression and return true or false as result of it.

ExampleNameResult
$a==$bEqualreturn TRUE If $a is equal to $b.
$a===$bIdenticalreturn TRUE If $a is equal to $b and they are same type.
$a!=$bNot Equalreturn TRUE If $a is not equal to $b.
$a!==$bNot Identicalreturn TRUE If $a is not equal to $b or they are not same type either values are same.
$a<$bLess Thanreturn TRUE If $a is strictly less than $b.
$a>$bGreater Thanreturn TRUE If $a is strictly greater than $b.
$a<=$bLess Than Equal Toreturn TRUE If $a is less than or equal to $b.
$a>=$bGreater Than Equal Toreturn TRUE If $a is greater than or equal to $b.

☛ Logical Operators


If we want to implement multiple conditions then logical operator is best solution about it. logical operator do control multiple conditions and return true or false on behalf of result return by logical operator.

ExampleNameResult
$a and $bAND or &&return TRUE If both operands are true.
$a or $bOR or ||return TRUE If anyone operand is true or both.
! $aNOTreturn TRUE If single operand is not true, behave like just vice versa.

☛ Ternary Operators


It is conditional operator and take three arguments first is condition and second for true result and third for false result. It is mostly used in programming for short conditional operation, it's behave like if-else.

Syntax:
variable= (Condition) ? true segment : false segment;

☛ String Operators


There are two type string operators, that work with string in php. when we want to concat or adjust with new contents in string operation for that we can apply these string operator.

ExampleNameResult
$a="Hello";
$b=$a."World!";
concatenation operator ('.')return Hello World! contcat "World!" string with $a and assign to $b.
$a="Hello";
$a.="World!";
concatenating assignment operator ('.=')return Hello World! append "World!" string in $a.