welcome to tutorialfamily

php operators

PHP Operators – It is used to perform various operation on variable and their value.

Operators used when applying logic in script.


PHP Operators

We can perform various operation using operators.

  • Arithmetic operators
  • Assignment operators
  • Increment/Decrement operators
  • Logical operators
  • Array operators
  • Comparison operators
  • Conditional assignment operators
  • String operators

PHP Arithmetic Operators

Arithmetic operators used for various operation with numeric value.

Addition, Subtraction, Multiplication, Division, Modulus , Exponentiation


Arithmetic Operators



<!DOCTYPE html>
<html>
<head>
	<title>Arithmetic Operators</title>
</head>
<body>
// Addition
<?php
	$x = 20;  
	$y = 8;
	echo $x + $y;
?> 
// Subtraction
<?php
	$x = 20;  
	$y = 8;
	echo $x - $y;
?> 
// Multiplication
<?php
	$x = 20;  
	$y = 8;
	echo $x * $y;
?>
// Division
<?php
	$x = 20;  
	$y = 8;
	echo $x / $y;
?>
// Modulus
<?php
	$x = 20;  
	$y = 8;
	echo $x % $y;
?>
// Exponentiation 
<?php
	$x = 20;  
	$y = 8;
	echo $x ** $y;
?>  
</body>
</html>

output

PHP Assignment Operators

Assignment operator are used with numeric value to set value to variable.

Common operator are (=).

It is set value to right side variable.


Assignment Operators



<!DOCTYPE html>
<html>
<head>
	<title>Assignment Operators</title>
</head>
<body>
// set value
<?php
	$x = 20;  
	echo $x;
?> 
// Addition
<?php
	$x = 20;  
	$y = 8;
	echo $x += $y; //same as ($x + $y)
?> 
// Subtraction
<?php
	$x = 20;  
	$y = 8;
	echo $x -= $y; //same as ($x - $y)
?>
// Multiplication
<?php
	$x = 20;  
	$y = 8;
	echo $x *= $y; //same as ($x * $y)
?>
// Modulus
<?php
	$x = 20;  
	$y = 8;
	echo $x %= $y; //same as ($x % $y)
?>
// Division 
<?php
	$x = 20;  
	$y = 8;
	echo $x /= $y; //same as ($x / $y)
?>  
</body>
</html>

output

PHP Increment / Decrement Operators

Increment operator used to increase the value of variable.

Decrement operator used to decrease the value of variable.

Below given types of increment/decrement.

  • ++$x Pre-increment (Increment variable $x by one value then $x return)
  • $x++ Post-increment ($x return then Increment variable $x by one value)
  • –$x Pre-decrement (Decrement variable $x by one value then $x return)
  • $x– Post-decrement ($x return then Decrement variable $x by one value)

Increment / Decrement Operators



<!DOCTYPE html>
<html>
<head>
	<title>Increment / Decrement Operators</title>
</head>
<body>
// Pre-increment
<?php
	$x = 20;  
	echo ++$x;
?>  
// Post-increment
<?php
	$x = 20;  
	echo $x++;
?> 
// Pre-decrement
<?php
	$x = 20;  
	echo --$x;
?>  
// Post-decrement
<?php
	$x = 20;  
	echo $x--;
?> 

</body>
</html>

output

PHP Logical Operators

Logical operator are used to check two value.

  • and operator Check two value if True both value
  • or operator Check any one value if True
  • xor operator Check any one value if True, but not both
  • && (And) operator Check two value if True both value
  • || (Or) operator Check any one value if True
  • ! (Not) operator Check any one value is not True

Logical Operators



<!DOCTYPE html>
<html>
<head>
	<title>Logical Operators</title>
</head>
<body>
// and operator
<?php
	$x = 50;  
	$y = 30;
	if ($x == 50 and $y == 30) {
    echo "Welcome Here";
}
?>   
// or operator
<?php
	$x = 50;  
	$y = 30;
	if ($x == 50 or $y == 80) {
    echo "Welcome Here";
}
?> 
// Xor operator
<?php
	$x = 50;  
	$y = 30;
	if ($x == 50 xor $y == 60) {
    echo "Welcome Here";
}
?> 
// && operator
<?php
	$x = 50;  
	$y = 30;
	if ($x == 50 &#038;&#038; $y == 30) {
    echo "Welcome Here";
}
?>
// || operator
<?php
	$x = 50;  
	$y = 30;
	if ($x == 50 || $y == 90) {
    echo "Welcome Here";
}
?>
// ! operator
<?php
	$x = 50;  
	if ($x !== 40) {
    echo "Welcome Here";
}
?>  
</body>
</html>

output

PHP Array Operators

Array operator are used to compare array.

  • $x and $y Suppose we take two variable
  • + (Union) Union of two variable $x and $y
  • == (Equality) Return true if $x and $y both variable have a same value
  • === (Identity) Return true if $x and $y both variable have a same value in same order and same type
  • != (Inequality) Returns true if $x is not equal to $y
  • <> (Inequality) Returns true if $x is not equal to $y
  • !== (Non-identity) Returns true if $x is not identical to $y

Array Operators



<!DOCTYPE html>
<html>
<head>
	<title>Array Operators</title>
</head>
<body>
// + (Union) operator
<?php
	$x = array("a" => "Toyota", "b" => "Honda");  
	$y = array("a" => "Toyota", "b" => "Honda");  

	print_r($x + $y); // union of $x and $y
?>   
// == (Equality) operator
<?php
	$x = array("a" => "Toyota", "b" => "Honda");  
	$y = array("x" => "Toyota", "y" => "Honda");  

	var_dump($x === $y);
?> 
// === (Identity) operator
<?php
	$x = array("a" => "Toyota", "b" => "Honda");  
	$y = array("a" => "Toyota", "b" => "Honda");  

	var_dump($x === $y);
?>  
 
// != (Inequality) operator
<?php
	$x = array("a" => "Toyota", "b" => "Honda");  
	$y = array("x" => "Toyota", "y" => "Honda");  

	var_dump($x != $y);
?> 
// <> (Inequality) operator
<?php
	$x = array("a" => "Toyota", "b" => "Honda");  
	$y = array("x" => "Toyota", "y" => "Honda");  

	var_dump($x <> $y);
?> 
// !== (Non-identity) operator
<?php
	$x = array("a" => "Toyota", "b" => "Honda");  
	$y = array("x" => "Toyota", "y" => "Honda");  

	var_dump($x !== $y);
?>   
</body>
</html>

output

PHP String Operators

String Operators are used to combined the two or more variable string.

  • . operator (Concatenation) Concatenation of two variable $x . $y
  • .= operator (Concatenation assignment) $x .= $y (Append $y to $x)

String Operators



<!DOCTYPE html>
<html>
<head>
	<title>String Operators</title>
</head>
<body>
// . operator
<?php
	$x = "Welcome";
	$y = " here!";
	echo $x . $y;
?>   
// .= operator
<?php
	$x = "Welcome";
	$y = " here!";
	$x .= $y;
	echo $x;
?>
</body>
</html>

output

PHP Conditional Assignment Operators

It used to set value depend on their condition.

  • ?: operator (Ternary)
  • ?? operator (Null coalescing)

Conditional Assignment Operators



<!DOCTYPE html>
<html>
<head>
	<title>Conditional Assignment Operators</title>
</head>
<body>
// ternary operator
<?php
	$valid = true;
	$lang = 'English';
	$x = $valid ? ($lang === 'English' ? 'Welcome' : 'yes') : ($lang === 'English' ? 'none' : 'no');
	echo $x; // outputs 'Welcome'
?>

</body>
</html>

output

Our Content