PHP
PHP
Audience
This tutorial has been designed to meet the requirements of all
those readers who are keen to learn the basics of PHP.
Prerequisites
This book assumes you have no prior knowledge on Programming
knowledge and assume you are at a beginner level.
All the content and graphics published in this e-book are the
property of PHPBootcamp.com. The user of this e-book is prohibited
to reuse, retain, copy, distribute or republish any contents or a part
of contents of this e-book in any manner without written consent of
the publisher.
Support
You can reach me for technical discussions and other support
related queries from here.
Free Courses
Learn JAVASCRIPT in 1
Hour
Table of Contents
3 Expressions ........................................................................................ 86
3.1 Assignments ............................................................................... 86
3.2 Arithmetic ................................................................................... 91
3.3 Comparison ................................................................................ 97
3.4 Logical ............................................................................................102
4 Statements .......................................................................................108
4.1 If Statements ............................................................................108
4.2 Switch Statements ...................................................................113
4.3 While Statements .....................................................................118
4.4 For Statements .........................................................................123
5 General .............................................................................................129
5.1 Exceptions ................................................................................129
5.2 Debug ........................................................................................134
5.3 Files............................................................................................136
5.4 Includes & Requires.................................................................141
5.5 Libraries .........................................................................................145
6 Forms................................................................................................ 150
6.1 GET ............................................................................................150
6.2 POST ..........................................................................................157
6.3 Cookies ......................................................................................164
6.4 Session ......................................................................................168
7 Snippets ...........................................................................................174
7.1 Regex .........................................................................................174
8 Projects.............................................................................................179
8.1 Save Student Registration Form Data to File .......................179
8.2 Online Test ...............................................................................181
1. PHP BASICS
1 PHP Basics
Installation of PHP
To run PHP on your local system you need to install HTTP Web
Server.
PHP is not like Java or C where you can install the libraries and run
PHP on command prompt.
Things to Note:
You need a Web Server to run PHP code.
Server executes php on the server and returns the output of the
code.
Browser pass the user data from browser to server and fetch the
data back from server.
Webserver interpret the PHP code and send the output to the
browser.
That is the reason you need a Web Server to execute PHP code.
Apache – https://httpd.apache.org/
PHP – http://php.net/software.php
MySQL – https://www.mysql.com/
phpMyAdmin – https://www.phpmyadmin.net/
Apache2
PHP
MySQL
phpMyAdmin
Install WAMP
Step 1: Locate the downloaded Software “wampserver3.1.3_x64”
It will have this icon. Even you can find this icon in the system tray
once the software is installed.
Step 6: Verify the path where the software is installed and click on
Install.
Step 7: Close all the Browsers and Select the Default Browser.
http://wampserver.aviatechno.net/files/tools/check_vcredist.exe
Step 5: Install the VC and then Run Again to make sure you all the
supported Libraries.
Verify WAMP
WAMP Server is installed in “C:\wamp64” folder.
See the Server running with GREEN color icon in the system tray.
Stop WAMP
Step 1: Click on the System Tray WAMP Icon and Select Stop Services.
Start WAMP
Step 1: Click on the System Tray WAMP Icon and Select Star Services.
C:\wamp64
C:\wamp64\www
C:\wamp64\logs
You can check the PHP version by opening the localhost when WAMP
server is running.
1.2 Echo
You should write the php code with in this starting and ending symbols.
<?php
?>
<?php
//PHP CODE
?>
SYNTAX:
<body>
<?php
?>
</body>
All the statements you write between the php block should end with “;”
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Page Title</title>
</head>
<body>
<h1>Heading</h1>
<p>Paragraph Text</p>
<!-- PHP Code -->
</body>
</html>
There is no parenthesis () required to call the method and pass data to the
method.
You use single quote or double quotes and statement should end with
semi colon “;”.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Learning PHP">
<title>Echo - PHP</title>
</head>
<body>
<h1>How to Print on the Browser with PHP</h1>
<p>You can use echo and print functions to print!</p>
<h2>
<?php print "Called from h2 tag"; ?>
</h2>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
Hello Program
You should know the following things:
index.php
css include
js include
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Learning PHP">
<title>Hello World Program!</title>
</head>
<body>
<h1>Welcome to PHP Application</h1>
<div>
<!-- on hover on this link to get a red line -->
<a href="#" onclick="greet('Welcome to PHP'); ">Click Me!</a>
</div>
<hr>
<div>
<?php
</body>
</html>
styles.css
a:hover{
text-decoration: underline;
text-color: red;
}
h1 {
text-align: center;
}
scripts.js
function greet(message){
alert(message);
}
Live Preview
Exercise 1
Live Preview
Exercise 2
Understand that you can write anything on the page with echo.
Live Preview
1.4 Comments
Usage of Comments
You can use the special notation to comment the code inside the php.
Comments are ignored by the Web Server and it is not displayed and not
sent to the browser.
You will not see php comments in the final HTML output.
Browser will never see php comments because it is ignored by the server.
SYNTAX:
<body>
<?php
/*
*/
?>
</body>
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Learning PHP">
<title>Comments PHP</title>
</head>
<body>
<?php
/*
* This is a multi line comments
* can go many lines.
*/
?>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
1.5 Functions
Usage of Fuctions
Functions is a block of statements that performs an action.
You can pass parameters to functions and it can return a value from the
function using “return” keyword.
function nameOfFunction(Parameters){
return someValue;
Example 1:
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Learning PHP">
<title>Functions PHP</title>
</head>
<body>
<?php
return $a + $b;
}
?>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
2. DATA BASICS
2 Data Basics
2.1 Variables
Usage of Variables
Variables are used to store information which are used inside the
program.
Variables in php are defined with dollar ($) sign in front of it.
Variables are case sensitive. $message and $Message are not same.
Examples of Variables:
$index = 0;
$firstName = ‘WPFreelancer’;
$firstName = “WPFreelancer”;
$price = 10;
$price = 10.50;
$result = true;
$fullName = $firstName;
You can add the variables inside the double quotes only with the variable
names.
“.” DOT symbol can be used to add the variable to the strings.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Variables</title>
<?php
?>
</head>
<body>
<h1>Variables</h1>
<?php
$counter = 100;
?>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
2.2 Strings
Usage of String
Strings in PHP can be enclosed with Single Quote or Double Quotes.
You can use Single quotes inside the double quotes and vice-versa.
Variable when used inside the Double Quote then it will resolve into the
variable value.
That’s why you must always use Single Quotes and only use Double
Double quotes when you need interpolation feature.
or
$message = null;
String Examples
$firstName = ‘WPFreelancer’;
$firstName = “WPFreelancer”;
$numPrice = 10.20;
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>String</title>
</head>
<body>
<h1>String</h1>
<?php
$fullName = 'WPFreelancer';
$site = "http://$fullName.com/";
echo "Visit us at $site <br>";
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
2.3 Numbers
Usage of Numbers
Numbers can also be called as Integers.
Integers
100
-200
Floating Point
10.34
Numbers are not wrapped with quotes or they do not include “,”
You can append the – (minus) symbol in front of the number to indicate it
is negative number.
Examples:
$price = 10;
$total = 10.20;
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Numbers</title>
</head>
<body>
<h1>Numbers</h1>
<?php
$length = 10;
$breath = 10;
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
2.4 Arrays
Usage of Arrays
Examples:
$colorName = array(‘red’, ‘white’, ‘yellow’);
$newColors = array();
$newColors[0] = $colorName[0];
$newColors[1] = $colorName[1];
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Arrays</title>
</head>
<body>
<h1>Arrays</h1>
<?php
//Define an Array
$colorNames = array('red', 'green', 'white');
//Print an Array
print_r($colorNames);
echo "<br>";
//Print Array
print_r($age);
?>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Add 1 Element
Add 2 Element
Live Preview
2.5 Objects
$this is a special object that will help to access the existing object
of a class.
Class Syntax:
class Student{
}
Create Instance of Class
$studentObj = new Student(1, “WP”, 20);
echo $studentObj->getStudentDetails();
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Classes and Objects</title>
<?php
class Student{
}
?>
</head>
<body>
<h1>Classes and Objects</h1>
<?php
$studentObj = new Student(1, "WP", 20);
echo $studentObj->getStudentDetails();
?>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
2.6 Constants
Usage of Constant
Constant are like variables but once you define the constant with a
fixed value you cannot change it later.
Example:
define(‘AGE’, 20);
echo AGE;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Constants</title>
<?php
//Define a Constant
define('MESSAGE', "Welcome to PHP!");
define('AGE', 20);
?>
</head>
<body>
<h1>Constants</h1>
<?php
echo MESSAGE . ". I am " . AGE . " years old!";
?>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
2.7 Boolean
Usage of Boolean
$result = 2 > 1;
$message = ($result) ? “CORRECT”: “WRONG”;
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Constants</title>
</head>
<body>
<h1>Constants</h1>
<?php
$result = 2 > 1;
$message = ($result) ? "CORRECT" : "INCORRECT";
echo "Is 2 > 1? - $message";
?>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
We need to pass the format of the date to get the system date.
TimeStamp:
$todaytime = time();
echo $todaytime();
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
Live Preview
Exercise 1
Live Preview
Exercise 2
Tip:
$futuredate = strtotime('first day of next month');
Live Preview
3. EXPRESSIONS
3 Expressions
3.1 Assignments
Comparison Expressions
Logical Expressions
.=
+=
-=
*=
/=
%=
OR
$message = ‘WP’;
$message .= ‘Freelancer’;
counter += 1;
counter = counter + 1;
counter -= 1;
counter = counter – 1;
counter *= 1;
counter = counter * 1;
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Assignment Expressions</title>
</head>
<body>
<h1>Assignment Expressions</h1>
<?php
$firstName = "WP";
$lastName = "Freelancer";
$fullName = $firstName . ", " . $lastName;
echo "Name $fullName <br>";
$counter = 10;
echo "Counter: $counter <br>";
$counter += $counter;
echo "Counter+=: $counter <br>";
$counter -= 10;
echo "Counter-=: $counter <br>";
?>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
3.2 Arithmetic
Assignment Expressions
Arithmetic Expressions
Comparison Expressions
Logical Expressions
++
//Increment by 1
counter++;
counter = counter + 1;
//Decrement by 1
counter—;
counter = counter – 1;
Order of Precedence:
++
*/%
+–
(5 + 2) * 2; //Result – 14
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Arithmetic Expressions</title>
</head>
<body>
<h1>Arithmetic Expressions</h1>
<?php
$counter = 10;
$counter = 10;
echo "Counter: $counter <br>";
$counter--;
echo "Counter--: $counter <br>";
$counter = (10 - 5) * 2;
echo "(10 - 5) * 2: $counter <br>";
?>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
1++;
echo “$counter++”;
++1;
$message = “Hello”;
$message++;
$result = true;
$result++;
Some of the things won’t work so comment them and run the
code.
Live Preview
3.3 Comparison
Assignment Expressions
Arithmetic Expressions
Comparison Expressions
Logical Expressions
== – Equal to
!= – Not Equal
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Comparison Expressions</title>
</head>
<body>
<h1>Comparison Expressions</h1>
<?php
$counter = 10 == 10;
echo $counter; //1 = true and 0 = false
echo "<br>";
$result = 15 <= 21;
$message = ($result)? "YES": "NO";
echo $message;
?>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
3.4 Logical
Assignment Expressions
Arithmetic Expressions
Comparison Expressions
Logical Expressions
&& is the logical operator which checks left side and right side value
and decides if the condition is true or false.
(5 > 3) – true
(8 < 5) – false
true && false = false
&& – AND
|| – OR
! – NOT
Order of Precedence
NOT
AND
OR
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Logical Expressions</title>
</head>
<body>
<h1>Logical Expressions</h1>
<?php
$input1 = 10;
$input2 = 20;
$result = ($input1 < $input2) || ($input1 == $input2);
?>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
4. STATEMENTS
4 Statements
4.1 If Statements
Usage of if Statements
It can choose some action when the condition is true and also take
some action when it false.
// Statements
// Statements
} else {
// Statements
}
Example 1:
if( $marks > 35 ){
}else{
}
Example 2:
if( $marks > 35 && $marks < 60 ){
}else{
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>If Statements</title>
<?php
$dayOfWeek = "Wednesday";
$result = "";
$dayOfWeek = strtolower($dayOfWeek);
</head>
<body>
<h1>If Statements</h1>
<?php
echo "This is the result: " . $result;
?>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
case value:
//Statement
break;
case value:
//Statement
break;
default:
//Statement
break;
}
Example 1:
switch ( $dayOfWeek ){
case ‘Mon’:
break;
case ‘Tuesday’:
break;
default:
break;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Switch Statements</title>
<?php
$dayOfWeek = "Friday";
$result = "";
$dayOfWeek = strtolower($dayOfWeek);
switch($dayOfWeek){
case 'monday':
$result = "First Day of Week";
break;
case 'tuesday':
$result = "Second Days of Week";
break;
case 'wednesday':
$result = "Mid Week";
break;
case 'thursday':
$result = "Preparing for Weekend";
break;
case 'friday':
$result = "It's Friday!";
break;
case 'saturday':
$result = "Enjoying Day!";
break;
case 'sunday':
$result = "Resting Day!";
break;
default:
$result = "Cannot find that Value!";
}
?>
</head>
<body>
<h1>Switch Statements</h1>
<?php
echo "This is the result: " . $result;
?>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
//Statements
break;
Live Preview
While statement are used to loop a block code and run it until a
condition is met.
//Statements
Example 1:
$counter = 0;
$counter++;
Syntax:
do{
//Statement
}while ( condition );
Example 1:
$counter = 0;
do{
$counter++;
break; – break is the keyword used to break the loop and come out
of the loop and execute statements after the while loop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Boolean Expressions</title>
<script>
</script>
</head>
<body>
<h1>Boolean Expression</h1>
<script type="text/javascript">
document.write(message);
</script>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
for Loop are used to loop a block code and run it until a condition
is met.
//Statements
Example 1:
for($counter = 0; $counter <= 10; $counter++){
echo $counter;
echo $input1;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>For Statements</title>
</head>
<body>
<h1>For Statements</h1>
<?php
?>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
5. GENERAL
5 General
5.1 Exceptions
Syntax Errors
Runtime Errors
Logic Errors
Syntax errors is where you forgot to follow the rules of PHP. It will
cause error when you execute the program.
Logic Errors are logically error that are cause because the program
instructions are not logically correct.
The process of making sure the code will not break and if it does it
know the reason for it and make a clean exit.
//Statements
}catch(Exception $exceptionObj) {
//Statments
}
Example:
try{
$firstName = “”;
}catch(Exception $e){
}finally{
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Exceptions</title>
</head>
<body>
<h1>Exceptions</h1>
<?php
try{
}catch(Exception $e){
}finally{
?>
</body>
</html>
Live Preview
Exercise 1
Tip:
if( !is_numeric(INPUTVALUE ) )
Live Preview
Exercise 2
Live Preview
5.2 Debug
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Debugging</title>
</head>
<body>
<h1>Debugging</h1>
<?php
$inputValue = 10;
echo inputValue;
?>
</body>
</html>
Live Preview 1
Exercise 1:
Add Sum() method in the php file and do not create that function.
This will cause the program to fail.
Exercise 2:
Write echo statement before the sum() method and echo after the
sum() method.
5.3 Files
Usage of Files
You can read files on the server using the PHP libraries.
xb – Create a new file and if already exists then it does not create.
fopen($path, $mode)
fclose($file)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<body>
<h1>Files</h1>
<?php
/*
* READ A FILE - rb mode
*
*/
$file = fopen('readme.txt', 'rb');
$line = "";
while( !feof($file) ){
$line = fgets($file);
echo $line;
}
fclose($file);
/*
* WRITE A FILE - wb mode
*
*/
$file = fopen("newfile.txt", "wb");
fwrite( $file, "<br><h1>It is a long established fact that
a reader
will be distracted by the readable
content of a page when looking at its
layout.</h1>");
fclose( $file );
?>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
PHP allows to break the code into small pieces of file and then
include then in the main page.
include
include_once
require
require_once
include and require both help to include the file into another file.
if the included file is not available then require statement will stop
the execution of the program.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Includes</title>
</head>
<body>
<h1>Includes</h1>
<?php
include 'functions.php';
echo add(1, 2);
require 'display.php';
?>
</body>
</html>
File: functions.php
<?php
function add($a, $b){
return $a + $b;
}
?>
File: display.php
<?php
echo '<br><h1>This is displayed from display.php</h1>';
?>
Live Preview
Exercise 1
Live Preview
Exercise 2
Exercise 2: Put all the HTML code in index.html and include that
file in the index.php
Live Preview
5.5 Libraries
Usage of Libraries
You can create a library file and put all the functions that you
commonly use in this library file.
This is the common practice for any web development where you
break the main program into smaller chunks of code and then
include them in the main program.
You can use include statement to import this functions file in your
page so that you can access those functions.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Libraries</title>
</head>
<body>
<h1>Libraries</h1>
<?php
include 'calculator.php';
define('INPUTVALUE1', 50);
define('INPUTVALUE2', 23);
?>
</body>
</html>
FileName: functions.php
<?php
?>
Live Preview
Exercise 1
Exercise 1: Create your own Library and use it. Continue using it
for other projects as well.
Follow this practice to split the project into small files and include
them.
6. FORMS
6 Forms
6.1 GET
GET is type of method used by the form to pass the form data to
the page that is mentioned in the action of the form.
action attribute – This define to which file this form data has to be
sent to.
Using GET we can send limited data and using POST we can send
huge data.
<input> type has a name attribute which helps to define the name
of the element. This name is like a variable that holds the data
what user enters.
http://site.com/display.php?input_text=hello&input_email=test@tes
t.com
$text = $_GET[‘input_text‘];
$emailid = $_GET[‘input_email‘];
When you click the submit button the form data is passed to
display.php which read the parameters and display the output.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Forms - GET</title>
</head>
<body>
<h1>Form - GET</h1>
<form action="display.php" method="get">
<fieldset>
<legend>Student Enquiry Form</legend>
<p>
<label for="input_text">Text:</label>
<input name ="input_text" type="text"
placeholder="Text">
</p>
<p>
<label for="input_email">Email:</label>
<input name ="input_email" type="email"
placeholder="[email protected]">
</p>
</fieldset>
<p><input type="submit"> <input type="reset"></p>
</form>
</body>
</html>
FileName: display.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Forms - GET</title>
</head>
<body>
<h1>Form - GET</h1>
<a href="index.php">Back to Home Page</a><br>
<?php
$name = $_GET['input_text'];
$email = $_GET['input_email'];
Live Preview
Exercise 1
Live Preview
Exercise 2
Tips:
Live Preview
6.2 POST
POST is type of method used by the form to pass the form data to
the page that is mentioned in the action of the form.
action attribute – This define to which file this form data has to be
sent to.
Using GET we can send limited data and using POST we can send
huge data.
<input> type has a name attribute which helps to define the name
of the element. This name is like a variable that holds the data
what user enters.
$text = $_POST[‘input_text‘];
$emailid = $_POST[‘input_email‘];
When you click the submit button the form data is passed to
display.php which read the parameters and display the output.
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Forms - POST</title>
</head>
<body>
<h1>Form - POST</h1>
<form action="display.php" method="post">
<fieldset>
<legend>Student Enquiry Form</legend>
<p>
<label for="input_text">Text:</label>
<input name ="input_text" type="text"
placeholder="Text">
</p>
<p>
<label for="input_email">Email:</label>
FileName: display.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Forms - POST</title>
</head>
<body>
<h1>Form - POST</h1>
<a href="index.php">Back to Home Page</a><br>
<?php
$name = $_POST['input_text'];
$email = $_POST['input_email'];
Live Preview
Exercise 1
Live Preview
Exercise 2
Tips:
Live Preview
6.3 Cookies
Usage of Cookies
Cookies helps to track what user is doing on the web page and
send that information to server so that server knows what client
did on the web page.
For every request, browser sends the cookies to server and if there
are any changes to cookies then that information is also sent to
the server.
Cookies last until the browser is closed. We can also manually set
the expiration time for any cookie.
Some of the browser disable cookies in that case cookies will not
work and also user can choose to change browser setting to not
store cookies.
Cookies can help to change the view of the page based on the user
actions.
How to Set a Cookie
$name = ‘WPFreelancer.com’;
$value = 20;
$expire = strtotime(‘+1 year’);
$path = ‘/’;
//Its a name=value pair.
setcookie($name, $value, $expire, $path);
How to Get a Cookie
$_COOKIE[$cookie_name]
By setting the time last year all the cookies with that name are
deleted.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Cookies</title>
</head>
<body>
<h1>Cookies</h1>
<?php
$cookie_name = "user";
$cookie_value = "WPFreelancer";
setcookie($cookie_name, $cookie_value, time() + (86400 *
30), "/");
if(!isset($_COOKIE[$cookie_name])) {
} else {
?>
</body>
</html>
Live Preview
Exercise 1
Exercise 1: Ask user to enter the cookie name on one page and
check on the other page if the cookie is new or old.
Live Preview
Exercise 2
Live Preview
6.4 Session
Usage of Sessions
session id are generated when the user first time visit the site and
this session id are stored in the cookie.
So, every time user make request to the server, this cookie is
passed to the server with the sessionid and based this session id
server is able to maintain an active session of the user.
If a session is already created for that specific user then PHP will
not create a duplicate session.
session_start();
session_start();
$_SESSION[“firstname”] = “WPFreelancer”;
Read a Session Variable
session_start();
echo $_SESSION[“firstname”];
Delete a Session Variable
session_start();
if(isset($_SESSION[“firstname“])){
unset($_SESSION[“firstname“]);
}
Sample Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Sessions</title>
</head>
<body>
<h1>Sessions</h1>
<?php
session_start();
//Create a Session
$_SESSION["firstname"] = "WPFreelancer";
echo $_SESSION["firstname"];
?>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
7. Snippets
7 Snippets
7.1 Regex
Usage of Regex
$pattern = “/WP/’;
Some patters:
\W – Any character
\d – Any Digit
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="description" content="Page Description">
<title>Regular Expressions</title>
</head>
<body>
<h1>Regular Expressions</h1>
<?php
$pattern = '/WP/';
$sitename = "WPFreelancer";
$found = preg_match($pattern, $sitename);
echo $found;
?>
</body>
</html>
Live Preview
Exercise 1
Live Preview
Exercise 2
Live Preview
8. PROJECTS
8 Projects
In this Project, you will write a simple application where user will his
details and when submitted the data is save in a text file in append
mode.
Live Preview
EXERCISE 1:
Become PHP Full Stack Web Developer in Just 30 Days
The above program will throw error when checkbox is not checked.
This is on purpose.
You need find out first if the parameter exists in that array $_GET
and then assign the value to variable.
EXERCISE 2:
Make a function to save the content and save it in functions.php
In this Project, you will write a simple application online test with
some question. Once user provide the answer he immediately verify
the result.
EXERCISE 2:
Display the result on the next page.
EXERCISE 1:
Add some Math formulas and add to the table.
EXERCISE 2:
Add some styling to the table.