PHP Tutorial Example
Example
PHP Mail
PHP Libxml
PHP HTTP
PHP Filter
PHP Zip
PHP Directory
PHP Error
PHP SimpleXML
PHP Calendar
PHP Date
PHP Misc
PHP XML
PHP FTP
PHP Math
PHP MySQL
PHP Filesystem
PHP Array
PHP String
PHP Tutorial
PHP Introduction
PHP Installation
PHP Syntax
PHP Variables
PHP Echo
PHP Strings
PHP Operators
PHP Comments
PHP Include File
PHP Require
PHP If Statement
PHP If Else
PHP Elseif
PHP Switch
PHP Forms
PHP Functions
PHP Array
PHP While Loop
PHP For Loop
PHP For Each
PHP Do While
PHP POST and GET
PHP Magic Quotes
PHP htmlentities
PHP Files
PHP File
PHP File Create
PHP File Open
PHP File Close
PHP File Write
PHP File Read
PHP File Delete
PHP File Append
PHP File Truncate
PHP File Upload
PHP Strings
PHP strpos
PHP str replace
PHP substrreplace
PHP Capitalization
PHP explode
PHP implode
PHP Advanced
PHP Date
PHP Session
PHP Cookies
PHP Include
PHP Email
PHP Secure Email
PHP Error
PHP Exception
PHP Filter
PHP and AJAX
AJAX Intro
AJAX PHP
AJAX Database
AJAX XML
AJAX Live Search
AJAX RSS Reader
AJAX Poll
PHP XML
XML Expat Parser
XML DOM
XML SimpleXML
PHP Database
MySQL Introduction
MySQL Connect
MySQL Create
MySQL Insert
MySQL Select
MySQL Where
MySQL Order By
MySQL Update
MySQL Delete
PHP ODBC
PHP Misc
PHP connection aborted Function
In all programming languages, operators are used to manipulate or perform operations on variables and values. You have already seen the string concatenation operator "." in the Echo Lesson and the assignment operator "=" in pretty much every PHP example so far.
There are many operators used in PHP, so we have separated them into the following categories to make it easier to learn them all.
Assignment operators are used to set a variable equal to a value or set a variable to another variable's value. Such an assignment of value is done with the "=", or equal character. Example:
* $var = 4;
* $var = $my_var;
Now both $my_var and $another_var contain the value 4. Assignments can also be used in conjunction with arithmetic operators.
Aritmetic either addition , subtraction all mathematically calculation perform .
| Operator | English | Example |
|---|---|---|
| + | Addition | 2 + 4 |
| - | Subtraction | 6 - 2 |
| * | Multiplication | 5 * 3 |
| / | Division | 15 / 3 |
| % | Modulus | 43 % 10 |
Comparisons are used to check the relationship between variables and/or values. If you would like to see a simple example of a comparison operator in action, check out our If Statement Lesson. Comparison operators are used inside conditional statements and evaluate to either true or false. Here are the most important comparison operators of PHP. Comoarison Operator
Assume: $x = 4 and $y = 5;
| Operator | English | 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 |
As we have already seen in the Echo Lesson, the period "." is used to add two strings together, or more technically, the period is the concatenation operator for strings.
$a_string = "Hello";
$another_string = " PHP";
$new_string = $a_string . $another_string;
echo $new_string . "!";
In programming it is a very common task to have to increment a variable by some fixed amount. The most common example of this is a counter. Say you want to increment a counter by 1, you would have:
However, there is a shorthand for doing this.
This combination assignment/arithmetic operator would accomplish the same task. The downside to this combination operator is that it reduces code readability to those programmers who are not used to such an operator. Here are some examples of other common shorthand operators. In general, "+=" and "-=" are the most widely used combination operators.
| Operator | English | Example | Equivalent Operation |
|---|---|---|---|
| += | Plus Equals | $x += 3; | $x = $x + 3; |
| -= | Minus Equals | $x -= 4; | $x = $x - 4; |
| *= | Multiply Equals | $x *= 3; | $x = $x * 3; |
| /= | Divide Equals | $x /= 2; | $x = $x / 2; |
| %= | Modulo Equals | $x %= 15; | $x = $x % 15; |
| .= | Concatenate Equals | $str.="hello"; | $str = $my_str . "hello"; |
This may seem a bit absurd, but there is even a shorter shorthand for the common task of adding 1 or subtracting 1 from a variable. To add one to a variable or "increment" use the "++" operator:
* $x++; Which is equivalent to $x += 1; or $x = $x + 1;
To subtract 1 from a variable, or "decrement" use the "--" operator:
* $x--; Which is equivalent to $x -= 1; or $x = $x - 1;
In addition to this "shorterhand" technique, you can specify whether you want to increment before the line of code is being executed or after the line has executed. Our PHP code below will display the difference. one is pre and other is post increment or decrement
$x = 3;
echo "The value of x with post-plusplus = " . $x++;
echo "<br /> The value of x after the post-plusplus is " . $x;
$x = 3;
echo "<br />The value of x with with pre-plusplus = " . ++$x;
echo "<br /> The value of x after the pre-plusplus is " . $x;
As you can see the value of $x++ is not reflected in the echoed text because the variable is not incremented until after the line of code is executed. However, with the pre-increment "++$x" the variable does reflect the addition immediately.

