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

Chapter-2 Web (php)

This document provides an overview of PHP, including its features, setup instructions, and basic syntax. It covers topics such as PHP variables, form handling, and the differences between GET and POST methods for data submission. The content is structured as a course chapter aimed at teaching web design and development using PHP.

Uploaded by

merir143
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Chapter-2 Web (php)

This document provides an overview of PHP, including its features, setup instructions, and basic syntax. It covers topics such as PHP variables, form handling, and the differences between GET and POST methods for data submission. The content is structured as a course chapter aimed at teaching web design and development using PHP.

Uploaded by

merir143
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

COURSE TITLE:

WEB DESIGN AND DEVELOPMENT II


Chapter- 2
Basics of PHP

By Meresa H.(MSc.)

1
Topics:

1.Introduction to PHP
2.Features of php
3.Setting up php with Apache
4.Basic php syntax
5.Retrieve data from html forms
6.Form Handling using PHP
7.Using numbers and strings in php
8.Control structures, Conditional and loop statements
9.PHP Functions
10.Sessions and Cookies management in PHPI

2
1. Introduction to PHP

■ What is php?
 PHP stands 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, JavaScript code, and PHP code
– PHP code are executed on the server, and the result is returned to the
browser as plain HTML
– PHP files have a default file extension of ".php"

3
1. Introduction to PHP
■ What Can PHP Do?
 PHP can generate dynamic page content
 PHP can create, open, read, write, 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 restrict users to access some pages on your website
 PHP can encrypt data
■ Why PHP?
 PHP runs on different platforms (Windows, Linux, Unix, Mac OS X, etc.)
 PHP is compatible with almost all servers used today (Apache, IIS, etc.)
 PHP has support for a wide range of databases
 PHP is free. Download it from the official PHP resource: www.php.net
 PHP is easy to learn and runs efficiently on the server side
4
2. Features of PHP
■ “PHP: Hypertext Preprocessor,” is a widely-used server-side scripting language designed for web
development.
■ Here are some key features of PHP:
– Open Source: PHP is free to use and has a large community of developers contributing to its
improvement1.
– Cross-Platform: PHP runs on various platforms, including Windows, Linux, Unix, and macOS1.
– Ease of Use: PHP is relatively easy to learn and use, making it accessible for beginners2.
– Embedded in HTML: PHP code can be embedded within HTML, making it easy to add dynamic
content to web pages1.
– Database Integration: PHP supports a wide range of databases, including MySQL, PostgreSQL,
Oracle, and more1.
– Fast Performance: PHP scripts are executed on the server, which can result in faster performance
for web applications2.
– Security Features: PHP includes built-in security features such as data encryption and access
control3.
– Extensive Library Support: PHP has a rich set of libraries and frameworks, such as Laravel and
Symfony, which simplify web development1.
– Error Handling: PHP provides robust error handling features, especially with the improvements
introduced in PHP 71.
– Versatility: PHP can be used for various tasks beyond web development, such as command-line
5
3. Set Up PHP on Your Own PC
■ However, if your server does not support PHP, you must:
■ install a web server
■ install PHP
■ install a database, such as MySQL
■ The official PHP website (PHP.net) has installation instructions for
■ PHP: http://php.net/manual/en/install.php

6
3. Set Up PHP on Your Own PC
■ WAMP stack (WAMP, LAMP)
■ You will be using the XAMP software stack
• Cross Platform free platform compatible with
both Windows (WAMP) and Linux (LAMP)
environments
• Apache web server
• MySQL DBMS
• PHP scripting language
• Apache and Windows
• Consider the Apache web server as the
intermediary that interprets HTTP requests that
arrive through a network port and decides how to
handle the request, which often requires working
in conjunction with PHP.

7
3. Set Up PHP on Your Own PC
■ Installing XAMPP locally
– The easiest and quickest way to do so is to use the
• XAMPP/ WAMP For Windows installation package
• MAMP for Mac installation package
• LAMP for Linux installation Package
– All of these installation packages install and configure Apache,
PHP, and MySQL.
– Later we can come back and configure these systems in more
detail

8
3. Set Up PHP on Your Own PC
■ XAMPP Control Panel

9
3. Set Up PHP on Your Own PC
■ XAMPP Settings
– PHP requests in your browser will need to use the localhost domain
(127.0.0.1)
– PHP files will have to be saved somewhere within the C:\xampp\htdocs
folder

10
4. Basic PHP - Syntax
Escaping to PHP
■ The PHP analyzing engine needs a way to differentiate PHP code from
other elements in the page.
■ The mechanism for doing so is known as 'escaping to PHP'. There are
four ways to do this: −
1. Canonical PHP tags
2. Short-open (SGML-style) tags (Standard Generalized Markup Language.)
3. ASP-style tags (Active Server Pages)
4. HTML script tags

11
4. Basic PHP - Syntax
Escaping to PHP
1. Canonical PHP tags
– The most universally effective PHP tag style is −
– <?php ...?>.
2. Short-open (SGML-style) tags
– Short or short-open tags look like this −
– <?...?> or <? // Some code ?>
3. ASP-style tags
– Active Server Pages /ASP-style tags look like this −
– <%...%>
4. HTML script tags
– HTML script tags look like this −
– <script language="PHP“, Javascript> ... </script>
12
4. Basic PHP - Syntax
Basic Syntax of PHP 5

■A PHP script can be placed anywhere in the


document.
■A PHP script starts with
<?php and ends with ?> :

13
4. Basic PHP - Syntax
Example

Note: PHP statements are


terminated by semicolon (;).

14
4. Basic PHP - Syntax
PHP echo and print Statements
■ The PHP echo Statement
– echo is a language construct, and can be used with or without
parentheses: echo or echo().
■ Echo uses display text, variable and Strings
– The following example shows how to display different strings with the
echo command (also notice that the strings can contain HTML markup):
■ Example:
<?php
echo "<h2>PHP is fun!</h2>";
echo "Hello world!<br>";
echo "This", " string", " was", " made", " with multiple parameters.";
?>
15
4. Basic PHP - Syntax
PHP print Statements
■ print is also a language construct, and can be used with or without
parentheses: print or print().

■ Print uses display text, variable and Strings


– The following example shows how to display different strings with the print
command (also notice that the strings can contain HTML markup):
Example Example: parentheses
<?php Enclose message
End in a
in quotation
print "<h2>PHP is fun!</h2>"; marks semi-colon

print "Hello world!<br>";


print ( "Your message to print" );
print "I'm about to learn PHP!";
?> Message to Output
Parenthesis are
optional
16
4. Basic PHP - Syntax
■ PHP Comments
<?php
echo 'This is a test'; // This is a one-line c++ style comment
/* This is a multi line comment
yet another line of comment */
echo 'This is yet another test';
echo 'One Final Test'; # This is a one-line shell-style comment (Perl Style)
?>
■ PHP is whitespace insensitive
– Whitespace is the stuff / fill something you type that is typically invisible on the screen, including
spaces, tabs, and carriage returns (end-of-line characters).
– PHP whitespace insensitive means that it almost never matters how many whitespace characters
you have in a row.one whitespace character is the same as many such characters.
■ Example: each of the following PHP statements that assigns the sum of 2 + 2 to the variable $four is
equivalent: −
$four = 2 + 2; // single spaces
$four <tab>=<tab>2<tab>+<tab>2 ; // spaces and tabs
$four = 2+ 2; // multiple lines 17
4. Basic PHP - Syntax
■ PHP is a Loosely Type Language
■ In the example above, notice that we did not have to tell PHP
which data type the variable is.
■ PHP automatically converts the variable to the correct data type,
depending on its value.
■ In other languages such as C, C++, and Java, the programmer
must declare the name and type of the variable before using it.

18
5. PHP Variables
■ Variable can have short names (like x and y) or more descriptive names
(age, carname, totalvolume).
■ There are two types of variables in PHP
1. Pre-defined Variables
2. User Defined Variables and
■ Rules for PHP variables:
 A variable starts with the $ sign, followed by the name of the
variable
 A variable name must begin with a letter or the underscore
character
 A variable name can only contain alpha-numeric characters
and underscores (A-z, 0-9, and _)
 A variable name should not contain spaces
 Variable names are case sensitive ($y and $Y are two different
variables) 19
5. PHP Variables
■ PHP Predefined Variables

■ PHP provides predefined variables that represent external variables, built-in environment variables, and

■ other information about the execution environment, such as the number and values of the

arguments passed to the script in the CLI environment.


– Common Predefined variabls
• Superglobals — Built-in variables that are always available in all scopes
• $GLOBALS — References all variables available in global scope
• $_SERVER — Server and execution environment information
• $_GET — HTTP GET variables
• $_POST — HTTP POST variables
• $_FILES — HTTP File Upload variables
• $_REQUEST — HTTP Request variables
• $_SESSION — Session variables
• $_ENV — Environment variables
• $_COOKIE — HTTP Cookies
• $php_errormsg — The previous error message
• $http_response_header — HTTP response headers
• $argc — The number of arguments passed to script
• $argv — Array of arguments passed to script
20
5. PHP Variables
■ Creating (Declaring) PHP Variables
■ PHP has no command for declaring a variable.
■ A variable is created the moment you first assign a value to it: E.g.
 $txt="Hello world!";
 $x=5;
■ After the execution of the statements above, the variable txt will
hold the value Hello world!, and the variable x will hold the value
5.
■ Note: When you assign a text value to a variable, put quotes
around the value.

21
5. PHP Variables
■ PHP Variable Scopes
■ The scope of a variable is the part of the script where the variable
can be referenced/used.
■ PHP has four different variable scopes:
1. local
2. global
3. static
4. parameter

22
5. PHP Variables
■ Local Scope
■ A variable declared within a PHP function is local and
can only be accessed within that function:
■ Example <!DOCTYPE html>
<html>
<?php <body>
<?php
$x=5; // global scope function myTest()
{
function myTest() $x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
{ }
myTest();
echo $x; // local scope
} // using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
myTest(); ?>
</body>
?> </html>
23
5. PHP Variables
■ Global Scope
■ A variable that is defined outside of any function, has a global scope.
■ Global variables can be accessed from any part of the script, EXCEPT from within a
function.
■ To access a global variable from within a function, use the global keyword:
<!DOCTYPE html>
■ Example: <html>
<?php <body>
<?php
$x=5; // global scope $x = 5; // global scope
$y=10; // global scope
function myTest() {
function myTest()
// using x inside this function will generate an error
{ echo "<p>Variable x inside function is: $x</p>";
global $x,$y;
}
myTest();
$y=$x+$y;
} echo "<p>Variable x outside function is: $x</p>";
?>
myTest(); </body>
echo $y; // outputs 15 </html>
?> 24
5. PHP Variables
■ Static Scope
■ When a function is completed, all of its variables are normally deleted. However,
sometimes you want a local variable to not be deleted.
■ To do this, use the static keyword when you first declare the variable:
<!DOCTYPE html>
■ Static Scope <html>
■ Example <body>
<?ph
<?php Output
function myTest() 0
Lunction my Test() { 1
{ static $x = 0; 2
echo $x;
static $x=0;
$x++;
echo $x; }
$x++; myTest();
} echo "<br>";
myTest();
myTest(); echo "<br>";
myTest(); myTest();
?>
myTest();
</body>
?> </html>
25
➤ OUTPUT ?
5. PHP Variables
■ Parameter Scope
■ A parameter is a local variable whose value is passed to the function by
the calling code.
■ Parameters are declared in a parameter list as part of the function
declaration:
■ Example:
<?php
function myTest($x)
{
echo $x;
}
myTest(5);

?>
26
6. Form Handling using PHP
■ HTML form data can be retrieved and processed in many different
ways, for example
■ by using a scripting language,
■ a server-side language such as PHP,
■ or any of the many programming languages of choice.
■ In this tutorial, we’re going to walk you through on how to access or
retrieve form data with PHP, and show you the different methods that
can be used.
■ Setting up the HTML form
■ To set up a form for server processing and data retrieval,
two important form attributes, that controls how the form
data is processed whenever it is submitted must be
specified.
■ These two form attributes are:
1. Method Attributes <form action="" method=""> ... </form>
2. Action Attributes 27
6. Form Handling using PHP
■ Setting up the HTML form
– Action Attributes:
■ specifies the PHP script file location for processing when it is submitted. If no script file
location is specified, the browser submits the form by using the current PHP script file
location ( the self-script in which the form is being called ).
– Method Attributes:
■ specifies what type of method the form will use to send the data.
■ We have two methods, the GET and POST.
■ Note: By default, if no method is specified, the GET method is used.
■ HTML forms are used to pass data to a server via different input
controls.
■ All input controls are placed in between <form> and </form>
<form action="PagetoOpen" method=“ “>
■ Syntax
//input controls placed here
</form>

Method = GET or POST 28


6. Form Handling using PHP
■ HTML Form must fulfill the following requirements
1. GET and POST Methods
2. Form Validation
3. Form Required Fields
■ GET & POST METHOD
■ The GET Method
■ The GET method sends the encoded user information appended to the
page request. The page and the encoded information are separated by
the? character.
file:///C:/Users/NiSa/Desktop/getMethod.html?fname=Nisa&Iname=soomro
■ The GET method is restricted to send upto 1024 characters only.
■ Never use GET method if you have password or other sensitive
information to be sent to the server.
■ GET can't be used to send binary data, like images or word documents, to
the server.
29
6. Form Handling using PHP
■ HTML Form must fulfill the following requirements
1. GET and POST Methods
2. Request Method
3. Form Validation
4. Form Required Fields

■ GET & POST METHOD


■ POST Method
■ The POST method does not have any restriction on data size to be sent.
■ The POST method can be used to send ASCII as well as binary data.
■ The data sent by POST method goes through HTTP header so security
depends on HTTP protocol. By using this method your information is
secure.

file:///C:/Users/NiSa/Desktop/getMethod.html 30
6. Form Handling using PHP
■ $_GET METHOD
Get Method
■ The predefined $_GET Variable use to collect
• The code below is a client-side HTML form
values in a form with method="get" information using method="get" for user to fill the
sent from a form with the GET method is visible to information
• <?php
everyone (it will be displayed in the browser • <form action="#" method="get">
• <input type="text" name="name" placeholder="Your
address bar) and has limits on the amount of Name"></input><br/>
• <input type="text" name="email" placeholder="Your
information to send. Email"></input><br/>
• <input type="text" name="contact" placeholder="Your
Mobile"></input><br/>
■ $_GET Variable to collect form data (the name of • <input type="submit" name="submit"
value="Submit"></input> </form>
the form field will automatically be the keys in the •
?>
$_GET array)

– $_GET["name"];

– $_GET["fname"];
31
– $_GET["age"];
6. Form Handling using PHP
■ $_POST METHOD

■ The predefined $_POST Variable use to collect values


in a form with method="post" information sent from
a form with the POST method is invisible to other and
has no limits on the amount of information to send.

■ Note: there is an 8MB max size for the POST Method,


by default (can be changed by setting the
post_max_size in the php.ini file)

■ $_POST Variable to collect form data (the name of


the form field will automatically be the keys in the
$_POST array)

– $_POST["name"];

– $_POST["name"];

– $_POST["age"]: .
32
6. Form Handling using PHP
■ $_REQUEST METHOD

■ The PHP $_REQUEST variable contains the contents of both $_GET,


$_POST, and $_COOKIE. We will discuss $_COOKIE variable when
we will explain about cookies.

■ The PHP $_REQUEST variable can be used to get the result from
form data sent with both the GET and POST methods.

– $_REQUEST["name"];

– $_REQUEST["fname"];

– $_REQUEST["age"];

33
6. Form Handling using PHP
■ GET VS. POST
■ Both GET and POST create an array (e.g. array(key =>
value, key2 => value2, key3 => value3, ...)). This array
holds key/value pairs, where keys are the names of the form
controls and values are the input data from the user.
■ Both GET and POST are treated as $_GET and $_POST
predefined variables. These are superglobals, which means
that they are always accessible, regardless of scope and you
can access them from any function, class or file without
having to do anything special.
■ $_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.

34
End of Ch-1

Thank you!

? 35

You might also like