|
|
|---|
ExpressionsPHP can also be called an expression-oriented language that is because everything you write in PHP is an expression. An earlier example where we've assigned 100 to $a, an integer value of 100 or either a string 100 is being stored in the variable $a. You may also assign a string "hello" to $a by writing $a = hello; Now, if you were to write $b = $a, then $b is containing the string "hello". Using PHP it is also possible to assign a function to a variable by writing $a = function_a(); and this is all possible because PHP is an expression-oriented language as well. Another good example of expression orientation is pre- and post-increment and decrement. This is achieved using variable++ and variable--. These are known as increment and decrement operators. For example: if you want to increment $a by 1, you can simply write '$a++' or '++$a'. But if you want to add more than 1 to $a, say 4, then you will have to write '$a++' or '++$a' multiple times. A better practice is to write '$a = $a + 4'. '$a + 4' evaluates to the value of $a plus 4, and is assigned back into $a, which results in incrementing $a by 4. This can also be written as '$a += 3' which executes faster than previous ways of incrementing values. Extending this, any two-place operator can be used in this operator-assignment mode, for example '$a -= 10' (subtract 10 from the value of $a), '$b *= 5' (multiply the value of $b by 5), etc. A very common type of expressions are comparison expressions. These expressions evaluate to either FALSE or TRUE. PHP supports > (bigger than), >= (bigger than or equal to), == (equal), != (not equal), < (smaller than) and <= (smaller than or equal to). The language also supports a set of strict equivalence operators: === (equal to and same type) and !== (not equal to or not same type). These expressions are most commonly used inside conditional execution, such as if statements. OperatorsAn operator is something that you feed with one or more values which yields another value. There are three types of operators.
Arithmetic OperatorsIt's like basic arithmetic. You add, multiply, subtract, divide.
<?Assignment OperatorsThe basic assignment operator is "=" which means that the left operand gets set to the value of the expression on the right.
<?Comparison OperatorsOne of the most important operators are Comparison Operators. They help you compare two values. Take a look at this table:
Remember, when comparing an integer with a string, the string is converted to a number. If you compare two numerical strings, they are compared as integers. |
|
|
|---|