Team LiB
Previous Section Next Section

What's an Operator?

In the previous section, you used an assignment operator (=) to assign values to your variables. There are other types of assignment operators, as well as other types of operators in general. The basic function of an operator is to do something with the value of a variable. That "something" can be assigning a value, changing a value, or comparing two or more values.

Here are the main types of PHP operators:

The rest of this chapter is devoted to discussing some of the main operators used in PHP. You'll be writing example scripts for each, so hang on to your hat!

Assignment Operators

You've already seen an assignment operator at work: the equals sign is the basic assignment operator. Burn this into your brain: = does not mean "equal to"! Instead, == (two equals signs) means "equal to," and the single = means "is assigned to." In fact, you've also seen the concatenation operator in this chapter, as it is used to put strings together.

Take a look at the assignment operators in Table 5.1 and prepare to write a new script.

Table 5.1: Assignment Operators

Operator

Example

Action

+=

$a += 3;

Changes the value of a variable to the current value plus the value on the right side.

-=

$a -= 3;

Changes the value of a variable to the current value minus the value on the right side.

.=

$a .= "string";

Concatenates (adds on to) the value on the right side with the current value.

Create a simple script to show how all of these assignment operators work. This script will assign an original value to one variable and then change that value as the script executes, all the while printing the result to the screen.

  1. Open a new file in your text editor and type the following HTML:

    <HTML>
    <HEAD>
    <TITLE>Using Assignment Operators</TITLE>
    </HEAD>
    <BODY>
    
    
  2. Start a PHP block. Create a variable with a value of 100 and then print it:

    <?
    $origVar = 100;
    echo "<P>Original value is $origVar</P>";
    
  3. Add to that value and then print it:

    $origVar += 25;
    echo "<P>Added a value, now it's $origVar</P>";
    
  4. Subtract from that value and then print it:

    $origVar -= 12;
    echo "<P>Subtracted a value, now it's $origVar</P>";
    
  5. Concatenate a string and then print it:

    $origVar .= " chickens";
    echo "<P>Final answer: $origVar</P>";
    
  6. Close your PHP block and add some more HTML so that the document is valid:

    ?>
    </BODY>
    </HTML>
    
  7. Save the file with the name assignscript.php and place this file in the document root of your web server.

    Click To expand
  8. Open your web browser and type http://127.0.0.1/assignscript.php.

    Click To expand

The results of your calculations will be printed to the screen. The next section moves to arithmetic operators, none of which should be strange to you as long as you made it through your first few years of school.

Arithmetic Operators

Arithmetic operators simply perform basic mathematical tasks. Take a look at Table 5.2, be sure you remember your basic math, and start creating the test script for this section.

Table 5.2: Arithmetic Operators

Operator

Example

Action

+

$b = $a + 3;

Adds values

-

$b = $a - 3;

Subtracts values

*

$b = $a * 3;

Multiplies values

/

$b = $a / 3;

Divides values

%

$b = $a % 3;

Returns the modulus, or remainder

Create a simple script to show how all of these arithmetic operators work. This script assigns original values to two variables, performs mathematical operations, and prints the results to the screen.

Click To expand
  1. Open a new file in your text editor and type the following HTML:

    <HTML>
    <HEAD>
    <TITLE>Using
    Arithmetic
    Operators</TITLE>
    </HEAD>
    <BODY>
    
  2. Start a PHP block, create two variables with values, and print the values:

    <?
    $a = 85;
    $b = 24;
    echo "<P>Original value of
    \$a is $a and \$b is
    $b</P>";
    
    
    Note 

    If you escape the dollar sign (\$), it will print literally instead of being interpreted as a variable.

  3. Add the two values and print the result:

    $c = $a + $b;
    echo "<P>Added \$a and \$b and got $c</P>";
    
    
  4. Subtract the two values and print the result:

    $c = $a - $b;
    echo "<P>Subtracted \$b from \$a and got $c</P>";
    
  5. Multiply the two values and print the result:

    $c = $a * $b;
    echo "<P>Multiplied \$a and \$b and got $c</P>";
    
  6. Divide the two values and print the result:

    $c = $a / $b;
    echo "<P>Divided \$a by \$b and got $c</P>";
    
  7. Check the modulus of the two values and print the result:

    $c = $a % $b;
    echo "<P>The modulus of \$a and \$b is $c</P>";
    
  8. Close your PHP block and add some more HTML so that the document is valid:

    ?>
    </BODY>
    </HTML>
    
  9. Save the file with the name arithmeticscript.php and place this file in the document root of your web server.

  10. Open your web browser and type http://127.0.0.1/arithmeticscript.php.

    Click To expand

Your original values, as well as the results of the various calculations, are printed to the screen.

Next you move to comparison operators, which are crucial in coding, but not nearly as much fun as arithmetic operators.

Comparison Operators

It should come as no surprise to you that comparison operators compare two values. As with the arithmetic operators, you have probably already seen most of the comparison operators, but might not know what they are called. Take a look at Table 5.3, and then you can start creating the test script for this section.

Table 5.3: Comparison Operators

Operator

Definition

==

Equal to

!=

Not equal to

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

The result of any of these comparisons is either true or false. This isn't much fun, but you can act on the result using control statements such as ifelse and while to perform a specific task.

Create a simple script to show the result of some comparisons, using the ifelse control statements to print a result to the screen.

  1. Open a new file in your text editor and type the following HTML:

    <HTML>
    <HEAD>
    <TITLE>Using Comparison Operators</TITLE>
    </HEAD>
    <BODY>
    
  2. Start a PHP block, create two variables with values, and print the values:

    <?
    $a = 21;
    $b = 15;
    echo "<P>Original value of \$a is $a and \$b is $b</P>";
    
  3. Within an ifelse statement, test whether $a is equal to $b. Depending on the answer (true or false), one of the echo statements will print:

    if ($a == $b) {
        echo "<P>TEST 1: \$a equals \$b</P>";
    } else {
        echo "<P>TEST 1: \$a is not equal to \$b</P>";
    }
    
    
    Note 

    Conditional expressions are enclosed in parentheses, such as this:

    if ($a == $b)
    

    and not this:

    if $a == $b
    
  4. Within an ifelse statement, test whether $a is not equal to $b. Depending on the answer (true or false), one of the echo statements will print:

    if ($a != $b) {
        echo "<P>TEST 2: \$a is not equal to \$b</P>";
    } else {
        echo "<P>TEST 2: \$a is equal to \$b</P>";
    }
    
    
    Note 

    The curly braces { and } separate the blocks of statements within a control structure.

  5. Within an ifelse statement, test whether $a is greater than $b. Depending on the answer (true or false), one of the echo statements will print:

    if ($a > $b) {
        echo "<P>TEST 3: \$a is greater than \$b</P>";
    } else {
        echo "<P>TEST 3: \$a is not greater than \$b</P>";
    }
    
  6. Within an ifelse statement, test whether $a is less than $b. Depending on the answer (true or false), one of the echo statements will print:

    if ($a < $b) {
        echo "<P>TEST 4: \$a is less than \$b</P>";
    } else {
        echo "<P>TEST 4: \$a is not less than \$b</P>";
    }
    
  7. Within an ifelse statement, test whether $a is greater than or equal to $b. Depending on the answer (true or false), one of the echo statements will print:

    if ($a >= $b) {
        echo "<P>TEST 5: \$a is greater than or equal to \$b</P>";
    } else {
        echo "<P>TEST 5: \$a is not greater than or equal to \$b</P>";
    }
    
    
  8. Within an ifelse statement, test whether $a is less than or equal to $b. Depending on the answer (true or false), one of the echo statements will print:

    if ($a <= $b) {
        echo "<P>TEST 6: \$a is less than or equal to \$b</P>";
    } else {
        echo "<P>TEST 6: \$a is not less than or equal to \$b</P>";
    }
    
  9. Close your PHP block and add some more HTML so that the document is valid:

    ?>
    </BODY>
    </HTML>
    
  10. Save the file with the name comparisonscript.php, and place this file in the document root of your web server.

  11. Open your web browser and type http://127.0.0.1/comparisonscript.php.

    Click To expand

The original values, as well as the results of the various comparisons, are printed to the screen. The last group of operators you'll tackle are logical operators, which are also used frequently inside blocks of code.

Logical Operators

Logical operators allow your script to determine the status of conditions (such as the comparisons in the preceding section). In the context of ifelse or while statements, logical operators execute certain code based on which conditions are true and which are false.

For now, focus on the && (and) and || (or) operators to determine the validity of a few comparisons.

  1. Open a new file in your text editor and type the following HTML:

    <HTML>
    <HEAD>
    <TITLE>Using Logical Operators</TITLE>
    </HEAD>
    <BODY>
    
  2. Start a PHP block and create two variables with values. The comparisons in this script are based on these two variables:

    <?
    $degrees = "95";
    $hot = "yes";
    
  3. Within an ifelse statement, test whether $degrees is greater than 100 or if the value of $hot is "yes." Depending on the result of the two comparisons, one of the echo statements will print:

    if (($degrees > 100) || ($hot == "yes")) {
        echo "<P>TEST 1: It's <strong>really</strong> hot!</P>";
    } else {
        echo "<P>TEST 1: It's bearable.</P>";
    }
    
    
    Note 

    Because this conditional expression is actually made up of two smaller conditional expressions, an extra set of parentheses surrounds it.

  4. Repeat the same ifelse statement as in step 3, but change the operator from the || operator to the && operator:

    if (($degrees > 100) && ($hot == "yes")) {
        echo "<P>TEST 2: It's <strong>really</strong> hot!</P>";
    } else {
        echo "<P> TEST 2: It's bearable.</P>";
    }
    
  5. Close your PHP block and add some more HTML so that the document is valid:

    ?>
    </BODY>
    </HTML>
    
  6. Save the file with the name logicalscript.php, and place this file in the document root of your web server.

  7. Open your web browser and type http://127.0.0.1/logicalscript.php.

    Click To expand

The text message associated with each comparison result is printed to the screen. In the first test, only one expression has to be true, and that is satisfied by $hot having a value of "yes." In the second test, both expressions have to be true, and they are not; $degrees has a value of 95, which is not greater than 100, even though $hot has a value of "yes." In this case, the second message is displayed.

Numerous other types of operators are used in PHP. They are explained as they appear throughout the book. The operators listed in this chapter give you a solid foundation in the basics of using variables and operators. In the next chapter, you'll use your newly acquired knowledge of variables and operators to build scripts that perform more intriguing actions than those explained so far.


Team LiB
Previous Section Next Section