UECS2094 UECS 2194 - Topic 5
UECS2094 UECS 2194 - Topic 5
Scripting
using PHP (part 1)
UECS2094/UECS2194
WEB APPLICATION DEVELOPMENT
1
What You Should Already
Know
Before you continue you should have a basic understanding of the following:
2
Introduction
PHP is an acronym for "PHP: Hypertext Preprocessor“
PHP is a widely-used, open source scripting language
PHP scripts are executed on the server
PHP is free to download and use
3
What is a PHP File?
PHP files can contain text, HTML, CSS, JavaScript, and PHP code
PHP code are executed on the server, and the result is returned to the browser as
plain HTML
4
What Can PHP Do?
PHP can generate dynamic page content
PHP can create, open, read, write, delete, and close files on the server
PHP can collect form data
PHP can send and receive cookies
PHP can add, delete, modify data in your database
PHP can be used to control user-access
PHP can encrypt data
With PHP you are not limited to output HTML. You can output images, PDF files, and
even Flash movies. You can also output any text, such as XHTML and XML.
5
PHP Syntax
PHP code is executed between <?php ?> tags
Semicolons (;) are used to end statements
Comments in PHP begin with // or /* */
<?php
// This is a single-line comment
/*
This is a
multi-line
comment
*/
// Declare and initialize a variable
$name = "John";
// Echo the variable's value
echo "Hello, " . $name . "!";
?>
6
PHP Variables
Variables are used to store values
Variable names begin with a $ sign followed by a letter or underscore
Example: $name = "John";
7
Echo/Print
• Used to output data to the screen
• Echo and print are both language constructs
• Example: echo "Hello, World!";
8
Data Types
• PHP supports several data types including:
• String
• Integer <?php // Output the data types
// Integer data type echo "Age: " . $age . "<br>";
• Float
$age = 25; echo "Price: " . $price . "<br>";
• Boolean // Float data type echo "Name: " . $name . "<br>";
• Null $price = 19.99; echo "Active: " . $is_active . "<br>";
// String data type echo "Discount: " . $discount . "<br>";
$name = "John";
// Boolean data type ?>
$is_active = true;
// Null data type
$discount = null;
9
Strings
• A sequence of characters
10
Constants
• Similar to variables, but their value cannot be changed
11
Operators
• Arithmetic (+, -, *, /, %)
12
If Else Statement
• Used to make decisions based on conditions
• Executes a block of code if a condition is true
• Example:
13
Switch Statement
• Used to select one of many blocks of code to execute
• Example:
switch ($x) {
case 1:
echo "Number is 1";
break;
case 2:
echo "Number is 2";
break;
default:
echo "Number is not 1 or 2";
}
14
While Loops
• Used to execute a block of code repeatedly while a condition is true
• Example:
$i = 0;
while ($i < 10) {
echo $i;
$i++;
}
15
For Loops
• Used to execute a block of code a specified number of times
• Example:
16
Functions
• A block of code that can be reused
• Can accept parameters and return values
• Example:
17
Arrays
• Used to store multiple values in a single variable
• Indexed arrays use numeric keys
• Associative arrays use string keys
• Example:
18
Superglobals
• Superglobals are special PHP variables that are available in all scopes throughout a PHP
script.
• They are predefined by PHP and can be used to access various types of information, such as
server and request data, form data, and session data.
• Superglobals are always in the form of associative arrays, which means that they contain
key-value pairs.
• Examples:
• $_SERVER
• $_GET
• $_POST
• $_SESSION
• $_COOKIE
19
Superglobals
• $_SERVER: Contains information about the server and the execution environment, such as
server name, script name, and request method.
• $_GET: Contains data that is passed in the URL as query parameters.
• $_POST: Contains data that is submitted through an HTML form using the HTTP POST
method.
• $_COOKIE: Contains data that is stored on the client's machine as cookies.
• $_SESSION: Contains data that is stored on the server and associated with a specific user
session.
• $_FILES: Contains data about files that are uploaded through an HTML form.
20
Superglobals
<?php In this example, we have:
?>
21
PHP Form Handling
Forms are a crucial part of many web applications, and PHP provides powerful tools for
HTML forms are used to collect input from the user. They are defined using the <form> tag,
which contains one or more input fields such as text boxes, radio buttons, and checkboxes.
22
Form Submission
When a user submits an HTML form, the data is sent to the server using either the GET or
POST method.
In PHP, the submitted form data is available in the $_GET or $_POST superglobal array,
depending on which method was used.
23
Form Validation
Form validation is the process of ensuring that the submitted form data is valid and meets
certain criteria.
This is important for preventing security vulnerabilities and ensuring that the application
works as intended.
PHP provides a number of functions and techniques for validating form data, such as
checking for required fields and validating email addresses and URLs.
24
Example Code: Form
Handling and Validation
<?php
// Process form data if there are no errors
// Check if form is submitted if(empty($errors)) {
if($_SERVER["REQUEST_METHOD"] == "POST") { // TODO: Process form data (send email, store in
// Get form data database, etc.)
$name = $_POST["name"]; echo "Form submitted successfully!";
$email = $_POST["email"]; } else {
$message = $_POST["message"]; // Output errors
echo "<ul>";
// Validate form data foreach($errors as $error) {
$errors = []; echo "<li>" . $error . "</li>";
if(empty($name)) { }
$errors[] = "Name is required."; echo "</ul>";
} }
if(empty($email)) {
$errors[] = "Email is required."; }
} elseif(!filter_var($email,
FILTER_VALIDATE_EMAIL)) { ?>
$errors[] = "Invalid email format."; <form method="post">
} <label>Name:</label>
if(empty($message)) { <input type="text" name="name"><br>
$errors[] = "Message is required."; <label>Email:</label>
} <input type="email" name="email"><br>
<label>Message:</label>
<textarea name="message"></textarea><br>
26
Example: Accessing Server Info
& Handling Form Data
27