Changes

Jump to: navigation, search

PHP Operators

7 bytes removed, 18:18, 7 June 2007
no edit summary
Opertors Operators in PHP, and any other prgramming language for that matter, enable us to perfrom tasks on variables and values such as assign, multiply, add, subtract and concatenate them. Operators take the form of symbols (such as + and -) and combinations of symbols (such as ++ and +=).
Operators in PHP work with ''operands'' which specify the variables and values that are to be used in the particalar particuilar operation. The number and location of these operands in relation to the operators (i.e. before and/or after the operator) depends on the type of operator in question. Let's take, for example, the following simple expression:
<pre>
</pre>
In this expression we have one operator (the '+') and two operands (the numbers 1 and 3). The '+' operator adds the values of two operands (resulting in a vlaue value of 4).
Operators can be combined to create complete expressions:
</table>
Arithmetic operators work with two operands, one to the left and the other to the right of the operator. For example:
<pre>
== PHP Comparison Operators ==
The comparison operators provide the ability to compare one value against another and return either a ''true'' or ''false'' result depending on teh the status of the match. For example , you might use a comparison operator to check if a variable value matches a particular number, or whether one string is identical to another. PHP provides a wide selection of comparison operators for just about every comparison need.
The comparison operators are used with two operands, one to the left and one to the right of the operator. The following table outlines the PHP comparison operators and provides brief descriptions and examples:
''If $var is less than 25 AND $var2 is greater than 45 display a message.''
Here the logical operator is the "AND" part of the sentence. If we were to express this in JavaScript PHP we would use the comparision operators we covered earlier together with the && logical operator:
<pre>
''If $var1 is less than 25 OR $var2 is greater than 45 display a message.''
Then we would replace the "OR" with the JavaScript PHP equavalent ||:
<pre>
</pre>
A much quicker way, however, is to use the PHP increment and decrement operators. These consist of the operators ''++'' (to increment) and ''--'' (to decrement) combined with an operand (the name of the variable to whcih which the change is to be applied).
There are two ways of using these operators, ''pre'' and ''post''. The ''pre'' mode performs the increment or decrement ''before'' performing the rest of the expression. For example, you might want to increment the value of a variable before it is assigned to another variable, or used in a calculation. In the ''post'' mode the increment or decrement is performed after the expression a has been performed. In this instance, you might want the value to be decrement decremented after it has been assigned or used in a calculation.
Whether a pre or post is used depends on whether the operator appears before (for pre), or after (for post) the variable name in the expression. For example ''--$myVariable'' or ''$myVariable++''.
The following table outlines the various forms of pre and post increment and decrement operatrorsoperators, together with examples that show how the equivalent task would need to be performed without the increment and decrement operators.
<table border="1" cellspacing="0" width="100%">
== PHP String Concatenation Operator ==
The PHP String concatenation operator is used to combine values to create a string. The concatenator concatenation operator is represented by a period/full stop (.) and can be used to build a string from other strings, variables containing non-strings (such as numbers) and even constants:
We will start with the operater in its simplest form concatenating two strings:
</pre>
The above will produce unexpected results (typically it will output just the number 5). The reason for this is because we have asked PHP to take a string (My Luck Lucky number is ), append the number 6 to it (to produce My Lucky number is 6) and then finally ''arithmetically'' add the number 5 to the string (which doesn't make a lot of sense). To resolve this issue we need to tell the PHP pre-processor to perform the addition (6 + 5) ''before'' performing the concatenation. We can achieve this by surrounding the addition expression in parentheses. Therefore the modified script:
<pre>
You can do anything in an execution operator that you could do as if you were sitting at a terminal window on the computer (within the confines of the user account under which PHP is running). Given this fact, it should not escape your attention that there are potential security risks to this, so this PHP feature should be used with care.
The excecution operator consists of enclosing the command to be executed in back quotes (`). The following example runs the UNIX/Linux ''uname'' and ''id'' commands to display information about the operating syustem system and user account on which the web server and PHP module are running (note that these command will not work if you are running a Windows based server):
<pre>

Navigation menu