0% found this document useful (0 votes)
11 views27 pages

UECS2094 UECS 2194 - Topic 5

This document provides an introduction to server-side scripting using PHP, covering its basic concepts, syntax, and functionalities such as handling form data and using superglobals. It explains PHP's capabilities, including dynamic page content generation, file manipulation, and user access control. Additionally, it includes examples of PHP syntax, variables, data types, control structures, and form validation techniques.

Uploaded by

fooweichang2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views27 pages

UECS2094 UECS 2194 - Topic 5

This document provides an introduction to server-side scripting using PHP, covering its basic concepts, syntax, and functionalities such as handling form data and using superglobals. It explains PHP's capabilities, including dynamic page content generation, file manipulation, and user access control. Additionally, it includes examples of PHP syntax, variables, data types, control structures, and form validation techniques.

Uploaded by

fooweichang2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Server-side

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

 PHP files have extension ".php"

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

• Enclosed in quotes (single or double)

• Concatenation using the . operator

• Example: $name = "John"; echo "My name is " . $name;

10
Constants
• Similar to variables, but their value cannot be changed

• Defined using the define() function

• Example: define("PI", 3.14159);

11
Operators
• Arithmetic (+, -, *, /, %)

• Comparison (==, !=, >, <, >=, <=)

• Logical (&&, ||, !)

12
If Else Statement
• Used to make decisions based on conditions
• Executes a block of code if a condition is true
• Example:

if ($x > $y) {


echo "X is greater than Y";
} else {
echo "Y is greater than or equal to X";
}

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:

for ($i = 0; $i < 10; $i++) {


echo $i;
}

16
Functions
• A block of code that can be reused
• Can accept parameters and return values
• Example:

function add($x, $y) {


return $x + $y;
}
echo add(2, 3); // Output: 5

17
Arrays
• Used to store multiple values in a single variable
• Indexed arrays use numeric keys
• Associative arrays use string keys
• Example:

$fruits = array("Apple", "Banana", "Orange");


echo $fruits[0]; // Output: Apple

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:

// The $_SERVER superglobal • Three echo statements that output some


echo "Server name: " . $_SERVER['SERVER_NAME'] . "<br>"; values from the $_SERVER superglobal
echo "Request method: " . $_SERVER['REQUEST_METHOD'] . "<br>"; array. These include the server name, request
echo "Script filename: " . $_SERVER['SCRIPT_FILENAME'] . method, and script filename.
"<br>";
• An if statement that checks if a "name"
// The $_GET superglobal parameter is set in the $_GET superglobal
if(isset($_GET['name'])) { array. If it is, it outputs a personalized greeting
echo "Hello, " . $_GET['name'] . "!<br>"; using the value of the "name" parameter.
}
• Another if statement that checks if an
// The $_POST superglobal "email" parameter is set in the $_POST
if(isset($_POST['email'])) { superglobal array. If it is, it outputs the value
echo "Your email is: " . $_POST['email'] . "<br>"; of the "email" parameter.
}

?>

21
PHP Form Handling
 Forms are a crucial part of many web applications, and PHP provides powerful tools for

processing and validating form data.

 Here are some key concepts related to PHP form handling:

 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>

<input type="submit" value="Submit">


</form> 25
Example Code: Form
Handling and Validation
This code does the following:
 Checks if the form is submitted using the $_SERVER["REQUEST_METHOD"]
superglobal.
 Gets the form data using the $_POST superglobal.
 Validates the form data using a series of if statements and PHP functions such as
empty() and filter_var().
 Processes the form data if there are no errors, or outputs a list of errors if there are.
 Displays an HTML form using the <form> tag, with various input fields and a submit
button.

26
Example: Accessing Server Info
& Handling Form Data

27

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy