Make your own free website on

PHP Programming

By James N Hitz

PHP Operators

PHP Operators have been categorised into:

  1. Arithmetical Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Incrementing and Decrementing Operators
  5. Logical Operators

The examples here are gotten straight from the PHP documentation although some cream has been skimmed off to stop you from having academic constipation. Bottom Line: This is NOT the complete list of PHP operators (a few have been... skimmed).

Arithmetic Operators

Remember basic arithmetic from school? These work just like those. They are:

+ - * / %

Short examples are given here below:

$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.

The division operator ("/") returns an integer value (the result of an integer division) if the two operands are integers (or strings that get converted to integers) and the quotient is an integer. If either operand is a floating-point value, or the operation results in a non-integer value, a floating-point value is returned.

Assignment Operators

The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the the left operand gets set to the value of the expression on the rights (that is, "gets set to").

The value of an assignment expression is the value assigned. That is, the value of "$a = 3" is 3. This allows you to do some tricky things:

$a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.

In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic and string operators that allow you to use a value in an expression and then set its value to the result of that expression. For example:

$a = 3;
$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;
$b = "Hello ";
$b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";

Note that the assignment copies the original variable to the new one (assignment by value), so changes to one will not affect the other.

Comparison Operators

Comparison operators, as their name implies, allow you to compare two values.

Example Name Result
$a == $bEqualTrue if $a is equal to $b.
$a != $b Not equalTrue if $a is not equal to $b.
$a < $b Less thanTrue if $a is strictly less than $b.
$a > $b Greater thanTrue if $a is strictly greater than $b.
$a <= $b Less than or equal to True if $a is less than or equal to $b.
$a >= $b Greater than or equal toTrue if $a is greater than or equal to $b.

Another conditional operator is the "?:" (or ternary) operator, which operates as in C and many other languages.

(expr1) ? (expr2) : (expr3);

This expression evaluates to expr2 if expr1 evaluates to true, and expr3 if expr1 evaluates to false.

Incrementing/Decrementing Operators

PHP supports C-style pre- and post-increment and decrement operators.

ExampleName Effect
++$a Pre-incrementIncrements $a by one, then returns $a
$a++ Post-incrementReturns $a, then increments $a by one
--$a Pre-decrementDecrements $a by one, then returns $a
$a-- Post-decrementReturns $a, then decrements $a by one

Here's a simple example script:

	echo "<h3>Postincrement</h3>";
	$a = 5;
	echo "Should be 5: " . $a++ . "<br>\n";
	echo "Should be 6: " . $a . "<br>\n";
	echo "<h3>Preincrement</h3>";
	$a = 5;
	echo "Should be 6: " . ++$a . "<br>\n";
	echo "Should be 6: " . $a . "<br>\n";

	echo "<h3>Postdecrement</h3>";
	$a = 5;
	echo "Should be 5: " . $a-- . "<br>\n";
	echo "Should be 4: " . $a . "<br>\n";

	echo "<h3>Predecrement</h3>";
	$a = 5;
	echo "Should be 4: " . --$a . "<br>\n";
	echo "Should be 4: " . $a . "<br>\n";

Logical Operators

ExampleName Effect
$a and $b And True if both $a and $b are true
$a or $b Or True if either $a or $b is true
$a xor $b Xor True if either $a or $b is true, but not both
! $a Not True if $a is not true
$a && $b And True if both $a and $b are true
$a || $b Or True if either $a or $b is true

The reason for the two different variations of "and" and "or" operators is that they operate at different precedences.

String Operators

There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side. Please read Assignment Operators for more information.

$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"

$a = "Hello ";
$a .= "World!";     // now $a contains "Hello World!"

Well.... That's it! Click here to Close this window when you're done