PHP Operators
by caiPHPhttp://www.caiphp.com/
Wednesday, 27th July 2005
PHP support for operators includes the following:
Arithmetic Operators
$a = 4;
$b = 2;
//Addition: $a + b = 6
//Subtraction: $a - $b = 2
//Multiplication: $a * $b = 8
//Division :$a / $b = 2
//Modulus (remainder of $a / $b): $a % $b = 0
//Increment: $a++ (would equal 5 since $a = 4)
$b = 2;
//Addition: $a + b = 6
//Subtraction: $a - $b = 2
//Multiplication: $a * $b = 8
//Division :$a / $b = 2
//Modulus (remainder of $a / $b): $a % $b = 0
//Increment: $a++ (would equal 5 since $a = 4)
Assignment Operators
The two main assignment operators in php are "=" and ".". The equals sign should be obvious; it assigns a value to a variable:
$a = 4;
$b = $a;
// $b = 4
$b = $a;
// $b = 4
Comparison Operators
PHP supports the standard comparison operators:
$a == $b: test if two values are equal
$a != $b: test if two values are not equal
$a < $b: test if the first value is less than the second
$a > $b: test if the first value is greater than the second
$a <= $b: test if the first value is less than or equal to the second
$a >= $b: test if the first value is greater than or equal to the second
$a != $b: test if two values are not equal
$a < $b: test if the first value is less than the second
$a > $b: test if the first value is greater than the second
$a <= $b: test if the first value is less than or equal to the second
$a >= $b: test if the first value is greater than or equal to the second
Increment & Decrement Operators
PHP also supports the standard increment and decrement operators:
$a = 5;
$a++;
// $a = 6
$b = 5;
$b--;
//$b = 4
$a += 2;
//$a = 8
$b -= 2;
//$b = 2
$a++;
// $a = 6
$b = 5;
$b--;
//$b = 4
$a += 2;
//$a = 8
$b -= 2;
//$b = 2
Concatenating Strings
The "." operator concatenates two values:
$sentence_a = "The quick brown ";
$sentence_b = "fox jumped...";
$sentence_c = $a . $b;
//$sentence_c = "The quick brown fox jumped...";
$sentence_b = "fox jumped...";
$sentence_c = $a . $b;
//$sentence_c = "The quick brown fox jumped...";
Options:
Printer Friendly
Email Friend
About The Author:
© caiPHP
© caiPHP
