example.php

<!DOCTYPE html>
<html>
 
 <head>
  <title>PHP Example</title>
 </head>
 <body>
<?php 
    /** First Example **/

    echo "<h1>Page Heading</h1>";
    echo "<h2>Second Heading</h2>";
    echo "<p>Paragraph Text.</p>";
    // How does this look?  How does it look in source?
    // We should be sure to include "\n" to make our output readable
?>

    <p>This is another <?= "example" ?>.</p>

<?php
    // Speaking of Comments:
    // this is a PHP comment
    /* also a comment */
    # also a comment

    /** Second Example **/
    function printVar($var) {
        echo "$var, type: " . gettype($var) . "<br>\n";
    }

    // Type Juggling Example
    $myvar = "2";  // $myvar is string
    echo "$myvar, type: " . gettype($myvar). "<br>";
    $myvar *= 3;   // $myvar is now an integer (6)
    printVar($myvar);
    $myvar = $myvar * 1.3;  // $myvar is now a float (7.8)
    printVar($myvar);
    $myvar = $myvar . " example"; // $myvar is now a string again
    printVar($myvar);

    // These will throw warnings
    $myvar = 5 + "31 days in October"; // $myvar is integer (36) -- String is 31
    printVar($myvar);
    $myvar = 5 + "Halloween is Oct 31";// $myvar is integer (5) -- String is 0
    printVar($myvar);

    // Checking the type using is_type()
    $i = 5;
    if (is_int($i))
        echo '$i is an integer<br>'; // Notice single quotes -- no interpolation
    if (is_float($i))
        echo '$i is a float<br>';
    if (is_string($i))
        echo '$i is a string<br>';



    /** Third Example **/

    # Here are a few PHP predefined variables 
    echo $_SERVER["HTTP_HOST"], "<br/>";
    echo $_SERVER["SERVER_SOFTWARE"], "<br/>";
    echo $_SERVER["SCRIPT_FILENAME"], "<br/>";

    echo $_SERVER; // Note this doesn't work

    //print_r($_SERVER); // Pretty-print (text) an associative array
                         // must look at it in the source, unless...
    //echo "<pre>"; print_r($_SERVER); echo "</pre>\n";
    // NOTE: Do NOT print the _SERVER variables on a production server!

    // More PHP and Server Information with phpinfo();
    // phpinfo();
    // NOTE: Do NOT call phpinfo() on a production server!


    /** Fourth Example **/

    // We can exit PHP mode and maintain if/else!
    if (true) { ?>
    <h3>Normal HTML</h3>
    <?php } else { ?>
    <h3>HTML in else</h3>
    <?php } ?>

    <h4>We can also use alternate if/else syntax:</h4>

    <?php if (true):  ?>
    <p>Normal HTML</p>
    <?php else: ?>
    <p>HTML in else</p>
    <?php endif;

    // This syntax also works with for loops!


    $x1 = "weird";  // normal string
    $x2 = "10";     // string representing a number
    $x3 = "0";      // string representing 0
    $x4 = "00";     // string double 0
    $y = 0;         // 0 integer
    $z = false;    // false boolean

    // Be careful with == and note how it compares when using ===
    if ($x1 == $y)  // non-numeric string == 0
    {
        print "[string] $x1 == [int] $y -- WEIRD! <br/>\n";
    }
    if ($x2 != $y)  // numeric string == number value
    {
        print "[string] $x2 != [int] $y -- WEIRD! <br/>\n";
    }
    if ($x3 == $y)  // numeric string == number value
    {
        print "[string] $x3 == [int] $y -- WEIRD! <br/>\n";
    }
    if ($x1 != $z)  // non-numeric string == true
    {
        print "[string] $x1 != [boolean] $z -- WEIRD! <br/>\n";
    }
    if ($x2 != $z)  // non-zero string == true
    {
        print "[string] $x2 != [boolean] $z -- WEIRD! <br/>\n";
    }
    if ($x3 == $z)  // zero string == false
    {
        print "[string] $x3 == [boolean] $z -- WEIRD! <br/>\n";
    }
    if ($x4 != $z)  // 00 string == true
    {
        print "[string] $x4 != [boolean] $z -- WEIRD! <br/>\n";
    }


    // Normal for loop
    for ($i = 1; $i <= 10; $i++) {
        echo $i;
        if ($i < 10) echo ", ";
    }
    // except scoping persists!
    echo ", After loop: $i<br/>\n";  


?>



 </body>
</html>