This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Operators


Operators:

There is always use for math. If you're not a math person, no need to worry. We stick with easy math. So no calculus, trig, algreba, and so on.
We have 4 types of operators, and this goes for pretty much every other computer language out there.

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators


Arithemetic Operators:

Operator Name Examples Display
+ Addition $x = 15;
echo $x + 5;
20
- Subtraction $x = 15;
echo $x - 10;
5
* Multiply $x = 15;
echo $x * 2;
30
/ Divide $x = 15;
echo $x / 5;
3
% Modulus $x = 15;
echo $x % 7;
1
++ Increment $x = 15;
echo $x++;
16
-- Decrement $x = 15;
echo $x--;
14


Quick explanation of the last 3 rows:
the Modulus (%) is the remainder in a division. If you divide 15 by 7, you'll get 2 full parts, and 1 extra. That 1 extra is called the modulus.
the Increment (++) is very useful, especially in loops. It will add one to your variable.
the Decrement (--) is also useful. Exactly what you thought! Subtracts one from your variable.

Comparison Operators:

Operator Description Example Returns
== Is equal to 3==3
3==10
true
false
!= Is not equal 5!=10
5!=5
true
false
<> Is not equal
(Same as !=)
5<>10
5<>5
true
false
> Greater than 10>1
10>100
true
false
>= Greater than or equal to 10>=10
10>=12
true
false
< Less than 10<100
10<1
true
false
<= Less than or equal to 10<=10
10<=9
true
false



Logical Operators:

Operator Description Example Returns
&& and $x = 5;
$y = 10;
$x>0 && $y<20
true
|| or $x = 5;
$y = 10;
$x ==5 || $y ==10
true
! not $x = 5
$y = 10
!($x==$y)
true

These will make more sense when we get to if statements

Assignment Operators:

Operator Example Same as
= $x = $y $x = $y
+= $x += $y $x = $x + $y
-= $x -= $y $x = $x - $y
*= $x *= $y $x = $x * $y
/= $x /= $y $x = $x / $y
.= $x .= $y $x = $x . $y (like string concentation)
%= $x %= $y $x = $x % $y

Every operator can be put into a variable, changed, re-declared, and called (echo'd)

Remember:
When you get into large math formulars, the basic law of math: BEDMAS - Brackets, Exponents, Division/Multiplication, Addition/Subtraction


Your Task:
Create a new

 

php file called operators.php
Declare 2 integer variables. (Integers alone don't need quotes, remember)
Play with and figure out all the arithmetic operators and assignment operators.
Then make a sentence display with some text and one of your variables.

Don't worry about Logical or Comparison operators right now. By the end of this course, you'll be an operator proffessional!

Task Discussion


  • Tynanh   March 16, 2013, 7:36 p.m.

    This one definitely posed a few challenges. An interesting tidbit for others as they work through this:

    The increment and decrement operators "==" and "--" work dependent on on their position in the code. If you put the operator after the variable the value is returned and then the increment is applied. If the operator is before the variable the increment is applied and then the value returned. ie:

    <?php

    $var1=10;

    echo ($var1++);  /* will display 10 and then apply the increment */

    echo (++$var1); /* will display 11 because the increment was applied before */

    ?>

    Check out my page on operators for how I did it and other things that had to be worked out.

    operators.php

  • Anonym   Feb. 16, 2013, 1:44 a.m.

    I enjoy working with variables, and you can see it in my file: http://practicephp.net63.net/operators.php

  • Landon   Oct. 25, 2012, 8:46 a.m.

     

    Variables
    
    $var1 = 20
    $var2 = 7
    $var3 = 89
    
    
    Arithmetic Operations
    
    $var1 + $var2 = 27
    $var1 - $var2 = 13
    $var1 * $var2 = 140
    $var1 / $var2 = 2.8571428571429
    $var1 % $var2 = 6
    $var1++
    $var1 is now is 21
    $var1--
    $var1 is now is 21
    
    Assignment operators
    
    $var1 = $var3
    $var1 is now is 89
    $var1 += $var2
    $var1 is now is 96
    $var1 -= $var2
    $var1 is now is 89
    $var1 *= $var2
    $var1 is now is 623
    $var1 /= $var2
    $var1 is now is 89
    $var1 %= $var2
    $var1 is now is 5

     


    Source Code:

     

    <html>
    
    <body>
    
    <?php
    
    $var1 = 20;
    
    $var2 = 7;
    
    $var3 = 89;
    
    
    echo "<h2>Variables</h2>";
    
    echo "\$var1 = 20" . "<br />";
    
    echo "\$var2 = 7" . "<br />";
    
    echo "\$var3 = 89" . "<br />" . "<br />";
    
    
    
    
    
    echo "<h2>Arithmetic Operations</h2>";
    
    echo "\$var1 + \$var2 = " . ($var1 + $var2) . "<br />";
    
    echo "\$var1 - \$var2 = " . ($var1 - $var2) . "<br />";
    
    echo "\$var1 * \$var2 = " . ($var1 * $var2) . "<br />";
    
    echo "\$var1 / \$var2 = " . ($var1 / $var2) . "<br />";
    
    echo "\$var1 % \$var2 = " . ($var1 % $var2) . "<br />";
    
    
    echo "\$var1++" . "<br />";
    
    $var1++;
    
    echo "<font color=\"red\">\$var1 is now  is " . $var1 . "</font><br />";
    
    
    echo "\$var1--" . "<br />";
    
    $var--;
    
    echo "<font color=\"red\">\$var1 is now  is " . $var1 . "</font><br />";
    
    
    echo "<h2>Assignment operators</h2>";
    
    echo "\$var1 = \$var3" . "<br />";
    
    $var1 = $var3;
    
    echo "<font color=\"red\">\$var1 is now  is " . $var1 . "</font><br />";
    
    
    echo "\$var1 += \$var2" . "<br />";
    
    $var1 += $var2;
    
    echo "<font color=\"red\">\$var1 is now  is " . $var1 . "</font><br />";
    
    
    echo "\$var1 -= \$var2" . "<br />";
    
    $var1 -= $var2;
    
    echo "<font color=\"red\">\$var1 is now  is " . $var1 . "</font><br />";
    
    
    echo "\$var1 *= \$var2" . "<br />";
    
    $var1 *= $var2;
    
    echo "<font color=\"red\">\$var1 is now  is " . $var1 . "</font><br />";
    
    
    echo "\$var1 /= \$var2" . "<br />";
    
    $var1 /= $var2;
    
    echo "<font color=\"red\">\$var1 is now  is " . $var1 . "</font><br />";
    
    
    echo "\$var1 %= \$var2" . "<br />";
    
    $var1 %= $var2;
    
    echo "<font color=\"red\">\$var1 is now  is " . $var1 . "</font><br />";
    
    
    ?>
    
    
    </body>
    
    </html>
  • MF   Sept. 12, 2012, 5:31 p.m.

    Hi,

    Link to my exersise operators.php

    http://www.marianaphp.tit4tat.co.uk/Operators.php

    I have a problem with (echo $x++) and (echo $y--) - when combined with text the increment and/or decrement don't add or substract one... When on it's own - works fine and the number figure is correct (-1 or +1)?

    Sorry for long code and perhaps very messy ... all sugestions for improvement very welcome!

    <html>
    <body>
    <br/>

    <?

    echo nl2br (strtoupper ("operators")."\r\r");

    echo nl2br (strtoupper ("Arithmetic Operators:")."\r\r");

    echo nl2br ("Examples:" ."\r\r");     

    $x = 33;
    $y = 14;

    echo nl2br ("X = 33"."\r\r");
    echo nl2br ("Y = 14"."\r\r");
    echo nl2br ("X + Y = ?"."\r\r");

    echo nl2br ("Addition = ".($x + $y)."\r\r");

    echo nl2br ("X - Y = ?"."\r\r");
    echo nl2br("Subtraction = ". ($x - $y) ."\r\r");

    echo nl2br ("Y - X = ?"."\r\r");
    echo nl2br ("Subtraction = ".($y - $x)."\r\r");


    echo nl2br ("X / Y = ?"."\r\r");
    echo nl2br ("Divide = " .($x / $y) ."\r\r");


    echo nl2br ("X x Y = ?"."\r\r");
    echo nl2br ("Multiply = " .($x * $y)."\r\r");


    echo "Modulus (%) is the remainder in a division. If you divide 15 by 7, you'll get 2 full parts, and 1 extra. That 1 extra is called the modulus. <br/><br/>";
    echo nl2br ("% X = ?"."\r\r");
    echo nl2br ("Modulus X = " . $x % 7 ."\r\r");
    echo nl2br ("Modulus Y = " . $y % 7 ."\r\r");

    $x = 33;
    $y = 14;

    echo nl2br ("Increment (echo \$x++); is very useful, especially in loops. It will add one to your variable."."\r\r");

            
    echo nl2br ("Increment X: (echo \$x++) = ".$x++."\r\r");

    echo nl2br ($x++."\r\r");

    echo nl2br ("Decrement (echo \$x--) subtracts one from your variable."."\r\r");

    echo nl2br ("Decrement Y: (echo \$y--) =  ".$y--."\r\r");
    echo nl2br ($y--."\r\r");


    echo nl2br (strtoupper ("Assignment  Operators:")."\r\r");

    echo nl2br ("\$x+=\$y\r\r\$x=\$x+\$y \r\r");
    echo nl2br ("New X = " .($x+=$y).\"\r\r");

    $x = 33;
    $y = 14;

    echo nl2br ("\$x-=\$y\r\r\$x=\$x-\$y \r\r");
    echo nl2br ("New X = " .$x-=$y .\"r\r");

    $x = 33;
    $y = 14;

    echo nl2br ("\r\r\$x*=\$y\r\r\$x=\$x*\$y \r\r");
    echo nl2br ("New X = " .$x*=$y .\"r\r");

    $x = 33;
    $y = 14;

    echo nl2br ("\r\r\$x/=\$y\r\r\$x=\$x/\$y \r\r");
    echo nl2br ("New X = " .$x/=$y .\"r\r");


    echo nl2br ("\r\r X = How are you? ");
    echo nl2br ("\r\r Y = Not too bad.\r\r");

    $x = "How are you? ";
    $y = " Not too bad.";


    echo nl2br ("\$x.=\$y\r\r\$x=\$x.\$y \r\r");
    echo nl2br ("New X = " .$x.=$y ." (like string concentration)\r\r");

    $x = 33;
    $y = 14;

    echo nl2br ("X = 33\r\r");
    echo nl2br ("Y = 14");

    echo nl2br ("\r\r\$x%=\$y\r\r\$x=\$x%\$y \r\r");
    echo nl2br ("New X = " .$x%=$y .\"r\r");
    ?>


    </html>
    </body>
     

  • katpal   June 21, 2012, 11:17 a.m.

    x=10 and y=20
    arithmetic operators example
    addition: x + y = 30
    subtraction: x - y = -10
    division: x / y = 0.5
    adding 1: 10
    minus 1: 11
    modulus: 10


    assignment operators

    x %=y which is x % y means that the the new X = 10
    x +=y means that the the new X = 30
    x -=y means that the the new X = 10
    x *=y means that the the new X = 200
    x /=y means that the the new X = 10

     

     

     

    and the code is:

    <?
    $x=10;
    $y=20;

    echo nl2br("x=10 and y=20 \n");

    echo nl2br("arithmetic operators example \n");
    echo "addition: x + y = ".($x + $y)."<br/>";
    echo nl2br( "subtraction: x - y = ".($x - $y)."\n"); //different way of line break
    echo "division: x / y = ".($x / $y)."<br/>";
    echo "adding 1: ".$x++."<br/>";
    echo "minus 1: ".$x--."<br/>";
    echo "modulus: ".($x%$y)."<br/>";

    echo "<p><strong>assignment operators</strong></p>";
    /*echo "<script type='text/javascript'> window.alert ($x);</script>";*/
    echo "x %=y which is x % y means that the the new X = ".($x%= $y)."<br/>";
    echo "x +=y means that the the new X =   ".($x += $y)."<br/>";
    echo "x -=y means that the the new X = ".($x -= $y)."<br/>";
    echo "x *=y means that the the new X = ".($x *= $y)."<br/>";
    echo "x /=y means that the the new X = ".($x /= $y)."<br/>";
    //echo "x .=y means that the the new X = ".($x .= $y)."<br/>";

    ?>

  • Asier Iturralde Sarasola   April 12, 2012, 7:31 p.m.

    Here is the code of my opeators.php file.

    It displays this:

    $var1 = 11
    $var2 = 5
    $var3 = 21
    $var1 + $var2 = 16
    $var1 - $var2 = 6
    $var1 * $var2 = 55
    $var1 / $var2 = 2.2
    $var1 % $var2 = 1
    $var1++
    Now the value of $var1 is 12
    $var1--
    Now the value of $var1 is 11
    $var1 = $var3
    Now the value of $var1 is 21
    $var1 += $var2
    Now the value of $var1 is 26
    $var1 -= $var2
    Now the value of $var1 is 21
    $var1 *= $var2
    Now the value of $var1 is 105
    $var1 /= $var2
    Now the value of $var1 is 21
    $var1 .= $var2
    Now the value of $var1 is 215
    $var1 %= $var2
    Now the value of $var1 is 0

  • W8RDG   Feb. 14, 2012, 4:48 p.m.

    Here is my operators.php file.