0% found this document useful (0 votes)
39 views

Unit 10 - WDD - Lesson 07 - PHP

PHP is a widely used scripting language that can be used to create dynamic and interactive web applications. It allows developers to add server-side scripting to HTML pages. This document discusses PHP basics like what PHP is, PHP files, variables, data types, functions, forms, sessions, and connecting to MySQL databases. It provides code examples for common PHP tasks like form validation, working with dates and times, and inserting/updating data in a MySQL database using PHP's MySQLi extension.

Uploaded by

Thab Thab
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)
39 views

Unit 10 - WDD - Lesson 07 - PHP

PHP is a widely used scripting language that can be used to create dynamic and interactive web applications. It allows developers to add server-side scripting to HTML pages. This document discusses PHP basics like what PHP is, PHP files, variables, data types, functions, forms, sessions, and connecting to MySQL databases. It provides code examples for common PHP tasks like form validation, working with dates and times, and inserting/updating data in a MySQL database using PHP's MySQLi extension.

Uploaded by

Thab Thab
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/ 57

Website Design &

Development
LESSON [7] – PHP

PHP WEBSITE DESIGN & DEVELOPMENT 1


What is PHP?
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
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"
What Can PHP Do?
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
How to run php files using wamp server?
Run wamp server by this selction
start->All programs->Wamp server->Start wamp server
Now you can see the w icon in system tray.
Let us create our php file first. 
Open notepad and type the php code.
save the file inside this folder
Left click on the wamp icon in system tray.
It will display list of options.
Select “start all services”.
Now all services(especially php) is running. 
Open the mozilla and type localhost in address bar.
Hit enter
It will show the default page of wamp server.
Now include this Testing.php (my php file name) at the end of the url
Basic PHP Syntax
A PHP script can be placed anywhere in the document.
Comments in PHP
PHP 5 Variables
Rules for PHP variables:
◦ A variable starts with the $ sign, followed by the name of the variable
◦ A variable name must start with a letter or the underscore character
◦ A variable name cannot start with a number
◦ A variable name can only contain alpha-numeric characters and underscores (A-z, 0-
9, and _ )
◦ Variable names are case-sensitive ($age and $AGE are two different variables)
Creating (Declaring) PHP Variables
Output Variables
PHP Data Types
Variables can store data of different types, and different data types can do different things.
PHP supports the following data types:
◦ String
◦ Integer
◦ Float (floating point numbers / double)
◦ Boolean
◦ Array
◦ Object
◦ NULL
◦ Resource
PHP String
◦ A string is a sequence of characters, like "Hello world!".
◦ A string can be any text inside quotes. You can use single or double quotes

PHP Integer
◦ An integer data type is a non-decimal number between -2,147,483,648 and
2,147,483,647.
◦ Rules for integers:
◦ An integer must have at least one digit
◦ An integer must not have a decimal point
◦ An integer can be either positive or negative
PHP String Functions
Get The Length of a String
◦ The PHP strlen() function returns the length of a string.

Count The Number of Words in a String


◦ The PHP str_word_count() function counts the number of words in a string:
PHP Functions
PHP User Defined Functions
◦ Besides the built-in PHP functions, we can create our own functions.
◦ A function is a block of statements that can be used repeatedly in a program.
◦ A function will not execute immediately when a page loads.
◦ A function will be executed by a call to the function.
Creating a user define function
PHP Function Arguments
Information can be passed to functions through arguments. An argument is
just like a variable.
PHP Function Arguments
Information can be passed to functions through arguments. An argument is just like a variable.
Arguments are specified after the function name, inside the parentheses. You can add as many
arguments as you want, just separate them with a comma.
Global Variables - Superglobals
The PHP superglobal variables are:
They are always accessible, regardless of scope - and you can access them from any function,
class or file without having to do anything special.
◦ $GLOBALS
◦ $_SERVER
◦ $_REQUEST
◦ $_POST
◦ $_GET
◦ $_FILES
◦ $_ENV
◦ $_COOKIE
◦ $_SESSION
PHP $GLOBALS

$GLOBALS is a PHP super global variable which is used to access global


variables from anywhere in the PHP script
PHP $_SERVER

$_SERVER is a PHP super global variable which holds information


about headers, paths, and script locations.

$_SERVER['PHP_SELF'] -Returns the filename of the currently executing script


$_SERVER['SERVER_NAME'] - Returns the name of the host server
$_SERVER['HTTP_HOST'] - Returns the Host header from the current request
$_SERVER['HTTP_REFERER'] - Returns the complete URL of the current page
PHP $_REQUEST

PHP $_REQUEST is used to collect data after submitting an HTML form.

$_SERVER['REQUEST_ Returns the request


METHOD'] method used to access
the page (such as
POST)
PHP $_POST
PHP $_POST is widely used to collect form data after submitting
an HTML form with method="post". $_POST is also widely used
to pass variables.
PHP $_GET
PHP $_GET can also be used to collect form data after submitting an HTML
form with method="get".
$_GET can also collect data sent in the URL.
Some other notes on GET requests:
◦ GET requests can be cached
◦ GET requests remain in the browser history
◦ GET requests can be bookmarked

Some other notes on POST requests:


◦ POST requests are never cached
◦ POST requests do not remain in the browser history
◦ POST requests cannot be bookmarked
◦ POST requests have no restrictions on data length
PHP Form Handling
The PHP superglobals $_GET and $_POST are used to collect
form-data.
The same result could also be achieved using the
HTTP GET method:
GET vs. POST
Both GET and POST are treated as $_GET and $_POST.
$_GET is an array of variables passed to the current script via the URL parameters.
$_POST is an array of variables passed to the current script via the HTTP POST method.

Developers prefer POST for sending form data.


PHP Form Validation
Validate Form Data With PHP
◦ We will also do three more things when the user submits the form:
◦ The first thing we will do is to pass all variables through PHP's htmlspecialchars() function.
◦ Strip unnecessary characters (extra space, tab, newline) from the user input data (with the PHP trim() function)
◦ Remove backslashes (\) from the user input data (with the PHP stripslashes() function)
Code Continued
Validate E-mail and URL
PHP - Validate Name
◦ The preg_match() function searches a string for pattern, returning true if the pattern exists, and false
otherwise.
PHP - Validate E-mail
◦ The easiest and safest way to check whether an email address is well-formed is to
use PHP's filter_var() function.
PHP - Validate URL
◦ The code below shows a way to check if a URL address syntax is
valid (this regular expression also allows dashes in the URL). If
the URL address syntax is not valid, then store an error
message:
Code Continued
Code Continued
PHP 5 Date and Time
Get a Simple Date
◦ The required format parameter of the date() function specifies how to format the date (or time).
◦ Here are some characters that are commonly used for dates:
◦ d - Represents the day of the month (01 to 31)
◦ m - Represents a month (01 to 12)
◦ Y - Represents a year (in four digits)
◦ l (lowercase 'L') - Represents the day of the week
Get a Simple Time
◦ Here are some characters that are commonly used for times:
◦ h - 12-hour format of an hour with leading zeros (01 to 12)
◦ i - Minutes with leading zeros (00 to 59)
◦ s - Seconds with leading zeros (00 to 59)
◦ a - Lowercase Ante meridiem and Post meridiem (am or pm)
PHP Sessions
Session variables are storing user information to be used across multiple pages (e.g. username,
favorite color, etc). By default, session variables last until the user closes the browser.
So; Session variables hold information about one single user, and are available to all pages in
one application.
Start a PHP Session
A session is started with the session_start() function.
Session variables are set with the PHP global variable: $_SESSION.
demo_session1.php

Note: The session_start() function must be the very first thing in


your document. Before any HTML tags.
Get PHP Session Variable Values
we will access the session information we set on the first page ("demo_session1.php").
Notice that session variables are not passed individually to each new page, instead they are
retrieved from the session we open at the beginning of each page (session_start()).
demo_session2.php
Modify a PHP Session Variable
To change a session variable, just overwrite it:
Destroy a PHP Session
To remove all global session variables and destroy the session, use
session_unset() and session_destroy():
PHP Connect to MySQL
PHP 5 and later can work with a MySQL database using:
MySQLi extension (the "i" stands for improved)
PDO (PHP Data Objects)

PHP WEBSITE DESIGN & DEVELOPMENT 47


Open a Connection to MySQL

PHP WEBSITE DESIGN & DEVELOPMENT 48


Insert Data Into MySQL Using MySQLi
After a database and a table have been created, we can start adding data in them.
Here are some syntax rules to follow:
• The SQL query must be quoted in PHP
• String values inside the SQL query must be quoted
• Numeric values must not be quoted
• The word NULL must not be quoted

The INSERT INTO statement is used to add new records to a MySQL table:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

PHP WEBSITE DESIGN & DEVELOPMENT 49


HTML FORM INSERT PHP CODE

PHP WEBSITE DESIGN & DEVELOPMENT 50


Update Data Using MySQLi
The UPDATE statement is used to update existing records in a table:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value 
Notice the WHERE clause in the UPDATE syntax: The WHERE clause specifies which record or
records that should be updated. If you omit the WHERE clause, all records will be updated!

PHP WEBSITE DESIGN & DEVELOPMENT 51


UPDATE PHP CODE
HTML FORM

PHP WEBSITE DESIGN & DEVELOPMENT 52


Delete Data Using MySQLi
The DELETE statement is used to delete records from a table:
DELETE FROM table_name
WHERE some_column = some_value
Notice the WHERE clause in the DELETE syntax: The WHERE clause specifies which record or
records that should be deleted. If you omit the WHERE clause, all records will be deleted!

PHP WEBSITE DESIGN & DEVELOPMENT 53


HTML FORM Delete PHP CODE

PHP WEBSITE DESIGN & DEVELOPMENT 54


Search Data Using MySQLi
The SELECT statement is used to search data from one or more tables:
SELECT column_name(s) FROM table_name
or we can use the * character to select ALL columns from a table:
SELECT * FROM table_name

PHP WEBSITE DESIGN & DEVELOPMENT 55


HTML FORM Search PHP CODE

PHP WEBSITE DESIGN & DEVELOPMENT 56


Any Question ?

The End..!!

PHP WEBSITE DESIGN & DEVELOPMENT 57

You might also like