09.22.07
Operators used in PHP - Arithmetic and Comparison
In the PHP scripting language, there are three primary kinds of operators; arithmetic operators, comparison operators and logical operators. Depending on the type of scripting that you do, you are more likely to use one group than the other two. However, this article provides a brief précis of two of the three types so that you know generally what to do should that situation arise in one of your scripts.
Arithmetic
There are seven primary arithmetic operators that can be used in PHP scripting.
The first one is the addition operator, symbolized by the + symbol. If you designate a variable x to be equal to one and then add the x+1 operation into your script, the number 2 will be returned. The same operational arithmetic is used in the subtraction (-), multiplication (*) and division (/) operators.
The fifth operator is the modulus operator, symbolized by the % symbol. It is expressed in the form of x%y, with both x and y being numbers. It basically returns the remainder when x is divided by y, so for example the script of 5%2 would return a result of 1, because one is the remainder when 5 is divided by 2.
The increment (++) and decrement (–) operators are used to increase or decrease the number in a variable by one. For example, if x=5 and you place x++ down into the script, the value six will be returned. Likewise, x—would return a value of four.
Comparison
Comparison operators, as the name might imply, are used to compare one number to another. They consist of equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=) and less than or equal to (<=). At both ends of the operator being used, numbers are placed. Based on the numbers and the operator being used, the output that is returned is either the word ‘true’ or the word ‘false’.
For example, if the statement is 1==2 (1 is equal to 2), then the output that was returned would be false. Likewise, if the statement is 1!=2 (1 is not equal to 2), then the output that was returned would be true. These types of operators might not seem important right now, but like most of the PHP fundamentals that you end up learning they are going to get more important as the PHP scripts that you design start to take on more complicated parts.
Scriptycan is a great software repository featuring both free and commercial PHP scripts and applications for developers and programmers.