PHP Operators - Page 2

PHP Operators - Page 2
  • Comparison Operators
    These six PHP operators are used to compare the values of two variables. For example, I might test if one value is equal to another value. The result (or answer) of the comparison is either true or false.

    == equal to

    This is used to test if one value is equal to another. Notice that there are two equal signs.

    is $a == $b
    != not equal to

    This is used to test if one value is not equal to another.

    is $a != $b
    > greater than

    This is used to test if one value is greater than another.

    is $a > $b
    < less than

    This is used to test if one value is less than another.

    is $a < $b
    >= greater than or equal to

    This is used to test if one value is greater than or equal to another.

    is $a >= $b
    <= less than or equal to

    This is used to test if one value is less than or equal to another.

    is $a <= $b


  • Logical Operators
    There are three logical operators used for complicated comparisons. By complicated I mean more then one comparison. For example I might ask "is the sky blue and is the grass green". The result of the entire question is either true or false.

    || or

    This is used to test if at least one comparison is true. You can use any number of comparisons. The result will be true if at least one of the comparisons is true.

    For example is the sky blue or is the grass green or is the water cold.

    is (($sky == "blue") || ($grass == "green")
    || ($water == "cold"))
    && and

    This is used to test if all comparisons are true. You can use any number of comparisons. The result will be true if all of the comparisons are true.

    For example is the sky blue and is the grass green and is the water cold.

    is (($sky == "blue") && ($grass == "green")
    && ($water == "cold"))
    ! not

    This is used to test if something is not true.

    is !$a
    Note--The arrow indicates that the code is wrapped to a second line and should really be all on one line.

  • Increment / Decrement Operators
    These four operators are used to increase or decrease a value of a variable by a step (increment). An example would be a webpage counter. Each time someone views a webpage the counter will increment by 1 step.

    ++$aPre-increment

    Increase by one and then grab the value of $a
    $a++Post-increment

    Grab the value of $a and increase by 1
    --$a Pre-decrement

    Decrease by one and then grab the value of $a
    $a--Post-drecrement

    Grab the value of $a and then decrease by 1
← Back






This site needs an editor - click to learn more!



RSS
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map





Content copyright © 2023 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.