PHP-Operators

Back to ACP page

Table of Contents

PHP Arithmetic Operators

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

<?php $x = 10; $y = 6; // Addition echo $x + $y; // Output: 16 echo "<br>"; // Subtraction echo $x - $y; // Output: 4 echo "<br>"; // Multiplication echo $x * $y; // Output: 60 echo "<br>"; // Division echo $x / $y; // Output: 1.6666666666667 echo "<br>"; // Modulus echo $x % $y; // Output: 4 echo "<br>"; // Exponentiation echo $x ** $y; // Output: 1000000 echo "<br>"; ?>

Example 1

Result View Example

PHP Assignment Operators

The PHP assignment operators are used with numeric values to write a value to a variable.

<?php $x = 10; // Assignment echo $x; // Output: 10 echo "<br>"; $x = 20; $x += 100; // Addition echo $x; // Output: 120 echo "<br>"; $x = 50; $x -= 30; // Subtraction echo $x; // Output: 20 echo "<br>"; $x = 5; $x *= 6; // Multiplication echo $x; // Output: 30 echo "<br>"; $x = 10; $x /= 5; // Division echo $x; // Output: 2 echo "<br>"; $x = 15; $x %= 4; // Modulus echo $x; // Output: 3 ?>

Example 2

Result View Example

PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

<h2>Comparison_Operators</h2> <p>The PHP comparison operators are used to compare two values (number or string): </p> <?php $x = 100; $y = "100"; var_dump($x == $y); // Equal: returns true because values are equal echo "<br>"; var_dump($x === $y); // Identical: returns false because types are not equal echo "<br>"; var_dump($x != $y); // Not equal: returns false because values are equal echo "<br>"; var_dump($x <> $y); // Not equal: returns false because values are equal echo "<br>"; var_dump($x !== $y); // Not identical: returns true because types are not equal echo "<br>"; var_dump($x > $y); // Greater than: returns false because $x is not greater than $y echo "<br>"; var_dump($x < $y); // Less than: returns false because $x is not less than $y echo "<br>"; var_dump($x <= $y); // Less than or equal to: returns true because $x is less than or equal to $y echo "<br>"; var_dump($x >= $y); // Greater than or equal to: returns true because $x is greater than or equal to $y echo "<br>"; $x = 5; $y = 10; var_dump($x <=> $y); // Spaceship: returns -1 because $x is less than $y echo "<br>"; $x = 10; $y = 10; var_dump($x <=> $y); // Spaceship: returns 0 because values are equal echo "<br>"; $x = 15; $y = 10; var_dump($x <=> $y); // Spaceship: returns +1 because $x is greater than $y echo "<br>"; ?>

Example 3

Result View Example

PHP Increment-Decrement Operators

<h2>Increment-Decrement_Operators</h2> <p>The PHP increment operators are used to increment a variable's value. The PHP decrement operators are used to decrement a variable's value.</p> <?php $x = 10; echo ++$x; // Output: 11 echo "<br>"; echo $x; // Output: 11 echo "<br>"; echo --$x; // Output: 10 echo "<br>"; echo $x; // Output: 10 echo "<hr>"; $x = 10; echo $x++; // Output: 10 echo "<br>"; echo $x; // Output: 11 echo "<br>"; echo $x--; // Output: 11 echo "<br>"; echo $x; // Output: 10 ?>

Example 4

Result View Example

PHP Logical Operators

<h4>Logical_Operators</h4> <p>The PHP logical operators are used to combine conditional statements.</p> <?php $x = 100; $y = 50; // and operator if ($x == 100 and $y == 50) { echo "Hello world!"."<br>"; } // or operator if ($x == 100 or $y == 80) { echo "Hello world!"."<br>"; } // xor operator if ($x == 100 xor $y == 80) { echo "Hello world!"."<br>"; } // not operator if ($x !== 90) { echo "Hello world!" ."<br>"; } // && operator if ($x == 100 && $y == 50) { echo "Hello world!" ."<br>"; } // || operator if ($x == 100 || $y == 80) { echo "Hello world!" ."<br>"; } ?>

Example 5

Result View Example

PHP String Operators

<h2>String_Operators</h2> <p>PHP has two operators that are specially designed for strings. </p> <?php $txt1 = "Hello"; $txt2 = " world!"; // Concatenation echo $txt1 . $txt2; echo "<hr>"; // Concatenation assignment $txt1 .= $txt2; echo $txt1; ?>

Example 6

Result View Example

Array Operators

Lorem ipsum dolor sit, amet consectetur adipisicing elit.

<h4>Array_Operators</h4> <p>The PHP array operators are used to compare arrays. </p> <?php $x = array("a" => "red", "b" => "green"); $y = array("c" => "blue", "d" => "yellow"); // $y = array("a" => "red", "b" => "green"); // union print_r($x + $y); // union of $x and $y echo "<br>"; // equality var_dump($x == $y); // Returns true if $x and $y have the same key/value pairs echo "<br>"; // identity var_dump($x === $y); // Returns true if $x and $y have the same key/value pairs in the same order and of the same types echo "<br>"; // inequality var_dump($x != $y); // Returns true if $x is not equal to $y echo "<br>"; // inequality var_dump($x <> $y); // Returns true if $x is not equal to $y echo "<br>"; // non-identity var_dump($x !== $y); // Returns true if $x is not identical to $y echo "<br>"; ?>

Example 7

Result View Example

PHP Conditional Assignment Operators

<h2>Conditional_Assignment_Operators</h2> <p>The PHP conditional assignment operators are used to set a value depending on conditions:</p> <?php // if empty($user) = TRUE, set $status = "anonymous" echo $status = (empty($user)) ? "anonymous" : "logged in"; echo("<br>"); $user = "John Doe"; // if empty($user) = FALSE, set $status = "logged in" echo $status = (empty($user)) ? "anonymous" : "logged in"; echo("<hr>"); // variable $user is the value of $_GET['user'] // and 'anonymous' if it does not exist echo $user = $_GET["user"] ?? "anonymous"; echo("<br>"); // variable $color is "red" if $color does not exist or is null echo $color = $color ?? "red"; ?>

Example 8

Result View Example

Document

Document in project

You can Download PDF file.

Reference