5. php

Page 1: Introduction

  • Brief overview of what is covered in this document

Page 2: What is PHP?

  • PHP: Hypertext Preprocessor (recursive acronym)

  • Server-side processing: unlike JavaScript, which is client-side

  • Generates HTML and JavaScript for client processing

  • PHP code is hidden from the client; processed server-side before page delivery

Page 3: PHP Pages

  • File extension: use .php instead of .html / .htm

  • PHP code integrates with HTML and JavaScript

  • PHP code is encapsulated in <?php and ?> tags

Page 4: PHP Language Syntax

  • Similar statement termination to JavaScript and C: uses semi-colons

  • Not a typed language: variable types are inferred, which allows dynamic versatility

  • Pros: rapid development; Cons: potential for confusing runtime errors

Page 5: PHP References

  • W3Schools: a respected source for PHP tutorials

  • Provides foundational learning resources for beginners

Page 6: Echo Function

  • echo() function outputs text to the page

  • Example: <h1><?php echo 'Hello world'; ?></h1> outputs <h1>Hello world</h1>

  • Implicit use with shorthand <?= ... ?> tags, e.g., <h1><?= 'Hello world' ?></h1>

Page 7: Variables in PHP

  • Variables prefixed with $ sign; case-sensitive

  • Allowable character for variables: must begin with an alphabet or underscore

  • Dynamic typing: no need for type declaration

  • Assigning null gives a variable an empty value

  • Example: <?php $var = 'Mary'; Var=′Joe′;echo"Var = 'Joe'; echo "var, Var";?></code>outputs"Mary,Joe"</p></li></ul><h2><spanclass="heading−content">Page8:ConditionalBlocks</span></h2><ul><liclass="drag"><p>PermitsconditionalHTMLoutput</p></li><liclass="drag"><p>Assignmentoperator<code>=</code>vsconditionaloperator<code>==</code></p></li><liclass="drag"><p>Supports<code>switch</code>statementsandloopingstructures</p></li><liclass="drag"><p>Exampleofif−elsestructure:</p><pre><codeclass="language−php"><?phpif(Var"; ?></code> outputs "Mary, Joe"</p></li></ul><h2><span class="heading-content">Page 8: Conditional Blocks</span></h2><ul><li class="drag"><p>Permits conditional HTML output</p></li><li class="drag"><p>Assignment operator <code>=</code> vs conditional operator <code>==</code></p></li><li class="drag"><p>Supports <code>switch</code> statements and looping structures</p></li><li class="drag"><p>Example of if-else structure:</p><pre><code class="language-php"><?php if (expression == true): ?> <p>This will show if the expression is true.</p> <?php else: ?> <p>Otherwise this will show.</p> <?php endif; ?>

Page 9: Superglobals

  • Special predefined array-like variables available globally

  • Common superglobals include:

    • SERVER</code>:containsserverinformation</p></li><liclass="drag"><p><code>_SERVER</code>: contains server information</p></li><li class="drag"><p><code>_GET: retrieves data from the URL

    • POST</code>:retrievesdatafromforms</p></li></ul></li><liclass="drag"><p>Exampleusage:<code>_POST</code>: retrieves data from forms</p></li></ul></li><li class="drag"><p>Example usage: <code>_SERVER['REQUEST_METHOD'] returns HTTP method

    Page 10: Accessing Form Data

    • Utilize POST</code>toaccessdatasubmittedviaforms</p></li><liclass="drag"><p>Exampleofdataaccessforfieldsnamed"fullname"and"age":</p><pre><codeclass="language−php">_POST</code> to access data submitted via forms</p></li><li class="drag"><p>Example of data access for fields named "full_name" and "age":</p><pre><code class="language-php">_POST['full_name'] $_POST['age']

    • Sample form:

      <form action="action.php" method="post">
      <p>Your name: <input type="text" name="full_name" /></p>
      <p>Your age: <input type="text" name="age" /></p>
      <p><input type="submit" /></p>
      </form>

    Page 11: Sanitizing Inputs

    • Importance of sanitizing inputs to mitigate malicious attacks

    • Use htmlspecialchars() for alphanumeric validation

    • Typecasting integers to (int) for validation

    • Example of sanitized output:

      <div>Hi <?php echo htmlspecialchars(POST[′fullname′]);?>.Youare<?phpecho(int)_POST['full_name']); ?>. You are <?php echo (int)_POST['age']; ?> years old.</div>

    Page 12: Example (form.html)

    • Sample form structure in HTML:

      <!DOCTYPE html>
      <html>
      <head>
         <title>My first page</title>
      </head>
      <body>
         <form action="response.php" method="POST">
            <p><label for="firstname">First name: </label><input type="text" id="firstname" name="firstname" /></p>
            <p><label for="lastname">Last name: </label><input type="text" id="lastname" name="lastname" /></p>
            <div>Select your favorite school:<br />
               <input type="radio" id="hogwarts" name="school" value="hogwarts" />
               <label for="hogwarts">Hogwarts</label><br />
               <input type="radio" id="dawson" name="school" value="dawson" />
               <label for="dawson">Dawson</label><br />
            </div>
            <p><label for="spell">Select your favorite spell:</label><br>
               <select id="spell" name="spell">
                  <option value="expelliarmus">Expelliarmus</option>
                  <option value="leviosa">Wingardium Leviosa</option>
               </select>
            </p>
            <p><input type="submit" /></p>
         </form>
      </body>
      </html>

    Page 13: Example (response.php)

    • Handling form responses with PHP:

      <!DOCTYPE html>
      <html>
      <head>
         <title>Form response</title>
      </head>
      <body>
         <p>Your name is: <?= htmlspecialchars(POST[′firstname′])."".htmlspecialchars(_POST['firstname']) . " " . htmlspecialchars(_POST['lastname'])?></p>
         <p>Your school is: <?= htmlspecialchars($_POST['school']) ?></p>
         <p>Your favorite spell is: <?= htmlspecialchars($_POST['spell']) ?></p>
      </body>
      </html>
    • Differences between POST and GET requests (POST sends data in the body)

    Page 14: Sequence Diagram

    • Illustrates requests and responses between client and server during data submission process

    Page 15: Troubleshooting

    • PHP error logging location: /var/log/apache2/error.log

    • Use the tail command to view latest logs

    • Log location can be adjusted in /etc/php5/apache2/php.ini