PHP Operators

0 0
Read Time:2 Minute, 49 Second

PHP Operators

  • In programming and logic, an operator symbol is used to perform an operation on some value such as addition, substraction, and so on
  • Operators assign values, works with strings and numbers and can control program flow

Types of Operators

  • Assignment
  • Math (Same as when you learned math in grade school)
  • String
  • Comparison
  • Logical

Assignment Operators

The Assignment Operators ( = ) sets the value to the left of the operand to the value on the right

You can also combine it with other operators

//Combined Operators	//Same as
$a=$b;	$a=$b;		//The left operand gets set to the value of the expression on the right
$a+=$b;	$a=$a+$b;	//Addition
$a-=$b;	$a=$a-$b;	//Substraction
$a*=$b;	$a=$a*$b;	//Multiplication
$a/=$b;	$a=$a/$b;	//Division
$a%=$b;	$a=$a%$b;	//Modulus
$a.=$b;	$a=$a.$b;	//Concatenate

Concatenate Operator is Often used to create output
TIP: Do not think of = as “equal” … think of it as “assignment”
TIP: Think of == as “equal”

Math Operators

The PHP math/arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

-$aNegativeNegative Value of $a
$a + $bAdditionSum of $a and $b
$a – $bSubtractionDifference of $a and $b
$a * $bMultiplicationProduct of $a and $b
$a / $bDivisionQuotient of $a and $b
$a % $bModulusRemainder of $a divided by $b
$a ** $bExponentiationResult of raising $a to the $b’th power

TIP: The division operator(“/”) return a float value anytime, even if the two operands are integers (or strings that get converted to integers)

There are also increment/decrement operators ++ and —
Note: $a++ is equal to $a+1

String Operators

PHP has two operators that are specially designed for strings.
The concatenation operator (.) allows you to combine two strings together into a single string

$txt1 . $txt2ConcatenationConcatenation of $txt1 and $txt2
$txt1 .= $txt2Concatenation assignmentAppends $txt2 to $txt1

for example, both these lines of code have the same result

	echo "Hello "."World";
	echo "Hello World";


Hello World
Hello World

Comparison Operators

If you compare an integer with a string, the string is converted to a number
If you compare two numerical strings, they are compared as integers

$a===$bIdentical (same value and type)
$a!=$bNot equal
$a<>$bNot equal (numerical)
$a!==$bNot Identical (values and types not the same)
$a<$bLess than
$a>$bGreater than
$a<=$bLess than or equal to
$a>=$bGreater than or equal to
$a <=> $bSpaceship(Returns an integer less than, equal to, or greater than zero, depending on if $a is less than, equal to, or greater than $b. Introduced in PHP 7.)

Note: “Not identical” values and types are not the same

Logical Operators

You are already be femiliar with some of these operators

  • and (“and” and && symbols are both supported)
  • or (“or” and || symbols are both supported)
  • xor (“xor” and ^ symbols are both supported)
  • not (“not” and ! symbols are both supported)

Why are two symbols that do same thing?

  • Order Precedence -it depends upon which symbol you use
  • For Example- and or && – the entire statement may evaluate to a different result
  • The reason for the two difference variation of “and” and “or” operators is that they operate at different precedence
  • Be sure to define xor: $a xor $b if either is true but not both
0 %
0 %
0 %
0 %
0 %
0 %

Average Rating

5 Star
4 Star
3 Star
2 Star
1 Star

4 thoughts on “PHP Operators

  1. Hi my friend! I wish to say that this post is awesome, nice written and include approximately all significant infos. I would like to see more posts like this.

  2. I’m extremely impressed along with your writing talents and also with the structure to your blog. Is that this a paid subject or did you modify it your self? Either way keep up the nice quality writing, it is uncommon to peer a great blog like this one today..

  3. Good write-up, I?¦m regular visitor of one?¦s blog, maintain up the nice operate, and It is going to be a regular visitor for a long time.

  4. Heya i’m for the first time here. I came across this board and I in finding It truly helpful & it helped me out a lot. I hope to offer something again and help others like you helped me.

Leave a Comment