Chapter-2 Web (php)
Chapter-2 Web (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
13
4. Basic PHP - Syntax
Example
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().
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
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>
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
– $_POST["name"];
– $_POST["name"];
– $_POST["age"]: .
32
6. Form Handling using PHP
■ $_REQUEST METHOD
■ 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