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
<?phpand?>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 pageExample:
<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-sensitiveAllowable 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, 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:
_GET: retrieves data from the URL_SERVER['REQUEST_METHOD']returns HTTP method
Page 10: Accessing Form Data
Utilize
_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 validationTypecasting integers to
(int)for validationExample of sanitized output:
<div>Hi <?php echo htmlspecialchars(_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['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.logUse the tail command to view latest logs
Log location can be adjusted in
/etc/php5/apache2/php.ini