<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <!-- Class examples from CS 328 - Week 9 Lecture 2 by: Sharon Tuttle last modified: 2025-03-27 you can run this using the URL: https://nrs-projects.humboldt.edu/~st10/s25cs328/328lect09-2/328lect09-2.php note: validated by: * getting an .xhtml version of the executed result with the FORM by copying the resulting source from a browser's "show source" for this document with the form and pasting it into a file ending with -1.xhtml on nrs-projects * getting an .xhtml version of the executed result with the FORM's RESPONSE by copying the resulting source from a browser's "show source" for this document with the form's response and pasting it into a file ending with -2.xhtml on nrs-projects * validating those two .xhtml versions using one of our usual validator links --> <head> <title> our second PHP </title> <meta charset="utf-8" /> <link href="https://nrs-projects.humboldt.edu/~st10/styles/normalize.css" type="text/css" rel="stylesheet" /> </head> <body> <h1> Our Second PHP document </h1> <?php $play_val = "ice cream"; ?> <ul> <li> Value of $play_val: <?= $play_val ?> </li> <li> PHP type of $play_val: <?= gettype($play_val) ?> </li> <li> Is $play_val of type string? <?= is_string($play_val) ?></li> <li> Is $play_val of type boolean? <?= is_bool($play_val) ?> </li> </ul> <?php // demo of TWO ways to define a PHP named constant // (note that its name does NOT start with $) const MAX_TEMP = 100; define("MIN_TEMP", 0); ?> <p> Legal temperature range: [<?= MIN_TEMP ?>, <?= MAX_TEMP ?>] </p> <p> <?php $thing1 = "dog"; $thing2 = 'dog'; // note: === will return true only if its operands // both have the same value AND the same type if ($thing1 === $thing2) { ?> $thing1 and $thing2 are both a string dog! <?php } else { ?> OH NO I HOPE I NEVER SEE THIS <?php } ?> </p> <?php // postback time! this PHP will now generate either a // form, or respond TO that form, depending on // whether the current request's request method // is "get" or "post" // if the current request's request method is "get", // it is not coming from this PHP's form (that is to have // method="post") -- so assume // it should generate this PHP's form for the user to respond to if ($_SERVER["REQUEST_METHOD"] == "GET") { // generate desired form // note: trying to safely use $_SERVER's "PHP_SELF" entry // to get the current PHP document's URL to use as this // form's action attribute value ?> <form method="post" action="<?= htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES) ?>"> <label for="nick"> Enter a nickname: </label> <input type="text" name="nickname" id="nick" /> <br /> <input type="submit" /> </form> <?php } // if reach here, that means $_SERVER["REQUEST_METHOD"] should be "POST"; // this request COULD be coming from the form this PHP generates, // so try to handle it accordingly else { // generate desired response // (note: stripping any tags user might have tried to // add to their response) ?> <p> Welcome, <?= strip_tags($_POST["nickname"]) ?> </p> <?php } ?> <footer> <hr /> <p> Validate by pasting .xhtml copy's URL into<br /> <a href="https://validator.w3.org/nu"> https://validator.w3.org/nu </a> or <a href="https://html5.validator.nu/"> https://html5.validator.nu/ </a> </p> </footer> </body> </html>