Monday, 20 May 2013

php operators


PHP Operators

    Operators -

    In all programming languages, operators are used to manipulate or perform operations on variables and values.
    There are many operators used in PHP, so we have separated them into the following categories to make it easier to learn them all.
  • Assignment Operators
  • Arithmetic Operators
  • Comparison Operators
  • String Operators
  • Combination Arithmetic & Assignment Operators

  • Assignment operator

    Assignment operators are used to set a variable equal to a value or set a variable to another variable's value. Such an assignment of value is done with the "=", or equal character. 
    Example:
    $my_var = 4; 

    $another_var = $my_var;
    Now both $my_var and $another_var contain the value 4. Assignments can also be used in conjunction with arithmetic operators.

    Arithmetic operators

    OperatorNameDescriptionExampleResult
    x+yAdditionSum of x and y2+24
    x-ySubstractionDiffrence of x and y10-46
    x*yMultiplicationProduct of x and y12*896
    x/yDivisionQuotient of x and y15/53
    x%yModulusRemainder of x divided by y20%62

    Comparison operators

    OperatorNameDescryptionExample
    x==yEqualTrue if x is equals to y5==8 return false
    x===yIdenticalTrue if x is equal to y, and they are of sametype5==="5" returns false
    x != yNot equalTrue if x is not equal to y5!=8 returns true
    x <> yNot equalTrue if x is not equal to y5<>8 returns true 
    x !== yNot identicalTrue if x is not equal toy, or they are not of same type5!=="5" returns true
    x > yGreater thanTrue if x is greater than y5>8 returns false
    x < y Less thanLess thanTrue if x is less than y5<8 returns true
    x >= yGreater than or equal toTrue if x is greater than
    or equal to y
    5>=8 returns false
    x <= yLess than or 
    equal to
    True if x is less 
    than or equal to y
    5<=8 returns true

    String operators

    As we have already seen in the Echo Lesson, the period "." is used to add two strings together, or more technically, the period is the concatenation operator for strings.

    PHP Code:
    $a_string = "Hello";
    $another_string = " Billy";
    $new_string = $a_string . $another_string;
    echo $new_string . "!";
    Display:
    Hello Billy!

    PHP Incrementing/Decrementing Operators


    PHP Code:
    $x = 4;
    echo 'The value of x with post-plusplus = ' . $x++;
    echo ' The value of x after the post-plusplus is ' . $x;
    $x = 4;
    echo '
    The value of x with with pre-plusplus = ' . ++$x;
    echo ' The value of x after the pre-plusplus is ' . $x;
    Display:
    The value of x with post-plusplus = 4
    The value of x after the post-plusplus is = 5
    The value of x with with pre-plusplus = 5
    The value of x after the pre-plusplus is = 5 

    PHP Logical Operators

    OperatorNameDescryption
    x and yAndTrue if both x and y
    x or yorTrue if either or both x and y
    x && yAndTrue if both x and y are true
    x || yOrTrue if either or both x and y are true
    ! xNotTrue if x is not true x=6

No comments:

Post a Comment