PHP - examples - 03 : operators, if.. else statements, . . .

revision:


operators

15
5
50
2
0
100000

20
120
-80
2000
0.2
7

bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(false)
bool(false)
bool(true)
bool(true)
int(0)

11
10
9
10

Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!

Hello world!
Hello world!

Array ( [a] => red [b] => green [c] => blue [d] => yellow )
bool(false)
bool(false)
bool(true)
bool(true)
bool(true)

anonymous
logged in

anonymous
red
code:
            <?php
                $a = 10; $b = 5;
                echo ($a + $b) . "< br>";
                echo ($a - $b) . "< br>";
                echo ($a * $b) . "< br>";
                echo ($a / $b) . "< br>";
                echo ($a % $b) . "< br>";
                echo ($a ** $b) . " < br>";
                echo "< br>";
                $c = 20;  echo $c . "< br>";
                $c = 20; $c /= 100;  echo $c . "< br>";
                $c = 20; $c %= 13;  echo $c . "< br>";
                echo "< br>";
                $d = 100;  $e = "100"; var_dump($d == $e); echo "< br>"; 
                $d = 100;  $e = "100"; var_dump($d === $e); echo "< br>"; 
                $d = 100;  $e = "100"; var_dump($d != $e); echo "< br>"; 
                $d = 100;  $e = "100"; var_dump($d <> $e); echo "< br>"; 
                $d = 100;  $e = "100"; var_dump($d !== $e); echo"< br>"; 
                $d = 100;  $e = "100"; var_dump($d > $e); echo"< br>"; 
                $d = 100;  $e = "100"; var_dump($d < $e); echo"< br>"; 
                $d = 100;  $e = "100"; var_dump($d >= $e); echo"< br>"; 
                $d = 100;  $e = "100"; var_dump($d <= $e); echo"< br>"; 
                $d = 100;  $e = "100"; var_dump($d <=> $e); echo"< br>"; 
                echo "< br>";
                $f = 10; echo ++$f; echo "< br>";
                $f = 10; echo $f++; echo "< br>";
                $f = 10; echo --$f; echo "< br>";
                $f = 10; echo $f--; echo "< br>";
                echo "< br>";
                $g = 100;  $h = 50;
                    if ($g == 100 and $h == 50) {
                        echo "Hello world!";
                    }
                echo "< br>";
                    if ($g == 100 or $h == 80) {
                        echo "Hello world!";
                    }
                echo "< br>";
                    if ($g == 100 xor $h == 80) {
                        echo "Hello world!";
                    }
                echo "< br>";
                    if ($g == 100 && $h == 50) {
                        echo "Hello world!";
                    }
                echo "< br>";
                    if ($g == 120 || $h == 50) {
                        echo "Hello world!";
                    }
                echo "< br>";
                    if ($g !== 110 ) {
                        echo "Hello world!";
                    }
                echo "< br>"; echo "< br>";
                $txt1 = "Hello"; $txt2 = " world!"; echo $txt1 . $txt2;
                echo "< br>";
                $txt1 = "Hello"; $txt2 = " world!"; $txt1 .= $txt2; echo $txt1;
                echo "< br>"; echo "< br>";
                $j = array("a" => "red", "b" => "green"); $k = array("c" => "blue", "d" => "yellow");  
                print_r($j + $k); echo "< br>";
                var_dump($j == $k); echo "< br>";
                var_dump($j === $k); echo "< br>";
                var_dump($j != $k); echo "< br>";
                var_dump($j <> $k); echo "< br>";
                var_dump($j !== $k); echo " < br>";
                echo "< br>";
                 // if empty($user) = TRUE, set $status = "anonymous"
                echo $status = (empty($user)) ? "anonymous" : "logged in";
                echo("< br>");
                $user = "Edgard Peeters";
                // if empty($user) = FALSE, set $status = "logged in"
                echo $status = (empty($user)) ? "anonymous" : "logged in";
                echo("< br>");
                echo("< br>");
                // variable $user is the value of $_GET['user'] and 'anonymous' if it does not exist
                echo $user = $_GET["user"] ?? "anonymous";
                echo("< br>");
                // variable $color is "red" if $color does not exist or is null
                echo $color = $color ?? "red";
            ? >
        

if . . .else . . . elseif statements

Have a good day!
Have a good day!
The hour (of the server) is 18, and will give the following message:
Have a good day!
code:
            <?php
                $l = date("H"); if ($l < "20") { echo "Have a good day!";}
                print "< br>";
                $m = date("H");
                if ($m < "20") {
                    echo "Have a good day!";
                } else {
                    echo "Have a good night!";
                }
                print "< br>";
                $n = date("H");
                echo "< a>The hour (of the server) is " . $n; 
                echo ", and will give the following message:< /a>" . "< br>";
                if ($n < "10") {
                echo "Have a good morning!";
                } elseif ($n < "20") {
                echo "Have a good day!";
                } else {
                echo "Have a good night!";
                }
            ? >
        

switch statement

Your favorite color is red!
code:
            <?php
                $favcolor = "red";
                switch ($favcolor) {
                case "red":
                    echo "Your favorite color is red!";
                    break;
                case "blue":
                    echo "Your favorite color is blue!";
                    break;
                case "green":
                    echo "Your favorite color is green!";
                    break;
                default:
                    echo "Your favorite color is neither red, blue, nor green!";
                }
            ?>
        

loops

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

The number is: 0
The number is: 20
The number is: 40
The number is: 60
The number is: 80
The number is: 100

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7

The number is: 6

The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10

The number is: 0
The number is: 20
The number is: 40
The number is: 60
The number is: 80
The number is: 100

red
green
blue
yellow

Peter = 35
Alice = 37
Staf = 43

The number is: 0
The number is: 1
The number is: 2
The number is: 3

The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
code:
            <?php
                $x = 1;
                while($x <= 5) {
                echo "The number is: $x < br>";
                $x++;
                }
                echo("< br>");
                $x = 0;
                while($x <= 100) {
                echo "The number is: $x < br>";
                $x+=20;
                }
                echo("< br>");
                $x = 1;
                do {
                  echo "The number is: $x < br>";
                  $x++;
                } while ($x <= 7);
                echo("< br>");
                $x = 6;
                do {
                echo "The number is: $x < br>";
                $x++;
                } while ($x <= 5);
                echo("< br>");
                for ($x = 0; $x <= 10; $x++) {
                    echo "The number is: $x < br>";
                  }
                echo("< br>");
                for ($x = 0; $x <= 100; $x+=20) {
                    echo "The number is: $x < br>";
                  }
                echo("< br>");
                $colors = array("red", "green", "blue", "yellow");
                foreach ($colors as $value) {
                  echo "$value < br>";
                }
                echo("< br>");
                $age = array("Peter"=>"35", "Alice"=>"37", "Staf"=>"43");
                foreach($age as $x => $val) {
                echo "$x = $val< br>";
                }
                echo("< br>");
                for ($x = 0; $x < 10; $x++) {
                    if ($x == 4) {
                      break;
                    }
                    echo "The number is: $x < br>";
                  }
                echo("< br>");
                for ($x = 0; $x < 10; $x++) {
                    if ($x == 4) {
                      continue;
                    }
                    echo "The number is: $x < br>";
                  }
            ?>