Monday, June 1, 2009

PHP - Operators

There are 5 categories of operators in PHP
  • Assignment Operators
  • Arithmetic Operators
  • Comparison Operators
  • String Operators
  • Combination Arithmetic & Assignment Operators
Assignment operators are used to set a variable equal to a value or set a variable to another variable's value.

Example:

$my_var = vn4000;

Arithmetic Operators

OperatorEnglishExample
+ Addition 2 + 4
- Subtraction 6 - 2
* Multiplication 5 * 3
/ Division 15 / 3
% Modulus 43 % 10

Comparison Operators

Comparisons are used to check the relationship between variables and/or values.

OperatorEnglish Example Result
== Equal To $x == $y false
!= Not Equal To $x != $y true
< Less Than $x < $y true
> Greater Than $x > $y false
<= Less Than or Equal To $x <= $y true
>= Greater Than or Equal To $x >= $y false

String Operators

The period "." is used to add two strings together. Example

<?php
$a_string = "Hello";
$another_string = " vn4000";
$new_string = $a_string . $another_string;
echo $new_string . "!";
?>