PHP
Client Server Architecture -
• A server is a computer system that selectively shares
its resources.
• A client is a computer or computer program that
request a server in order to make use of its resource.
Web (Request-Response) model-
Client Side Server side
Browser 1. Sends a request
2. Receives the
request.
3. Executes the
5. Send the
requested page
response (HTML) PHP
6. Display (server side- PHP)
page code.
the
response 4. Generates the
(HTML) response.
page.
PHP (Hypertext Preprocessor)-
• PHP is a server-side scripting language.
• It is embedded within HTML.
• It’s an Open Source software.
• It allows web developers to develop dynamic and
interactive web applications quickly.
• It can connect with most of the Database Servers
(MySQL, PostgreSQL) and can provide the database
driven applications easily.
• It can run on different platforms.
• It is compatible with almost all the web servers used
today i.e. (Apache, IIS etc.)
PHP more -
• The PHP files are saved with .php extension.
• PHP file contains HTML tags and PHP scripting code
• The PHP code is put inside the
<?php and ?> tags.
• The PHP statements always ends with semicolon ‘;’
• Example code –
<?php
statement 1;
statement 2;
……….
?>
‘echo’ command -
• It is used to output content (text or HTML) in
PHP.
• Syntax – echo ‘the text here…’;
• Example –
<?php
echo ‘Hello PHP!’;
?>
Output –
Hello PHP!
Example -
<html>
<head>
<title>
My First PHP Page
</title>
</head>
<body>
<?php
echo "<h1>Hello PHP!</h1>";
?>
</body>
</html>
Variables -
• Variables are used to hold data.
• In PHP, variables are always started with ‘$’ sign
followed by the name of the variable.
• All variables must only contain alpha-numeric
characters with the only exception of _ (an
underscore)
• Variables are case sensitive i.e. x and X are different.
• Ex -
$msg = ‘hello php’;
$age = 23;
Example -
<html>
<head><title>
PHP page
</title></head>
<body>
<?php
$str1 = '<h1>Hello World!</h1>';
$age = 23;
echo $str1;
echo '<br>';
echo 'I am '.$age.' years old!';
?>
</body>
</html>
Arrays -
• Arrays are special type of variables that can hold
multiple values.
• Arrays elements can be accessed with the help of
their index values, starting from ‘0’ index.
• There are 3 types of arrays –
– Numeric array - Array with numeric index
– Associative array - Array is represented as key-value pairs
– Multidimensional array - Array containing one or more
arrays
• Numeric Arrays
$name=array(“Amit",“Kumar");
Or
$name[0] = “Amit";
$name[1] = “Kumar";
• Associative Arrays : (ID key => element_value)
$name=array("First" => “Amit", "Last" => “Kumar");
Or
$name['First'] = “Amit";
$name['Last'] = “Kumar";
• Multidimensional Arrays – It can be called as an
array of arrays.
$marks = array
(
“Amit" => array (71, 68, 69, 64),
“Ashish" => array (57, 55, 68, 58),
“Ashley" => array (81, 67, 78, 83)
);
Example -
<head><title>
Arrays in PHP
</title></head>
<body>
<?php
$name*0+ = “Amit";
$name*1+ = “Kumar";
$name1*'First'+ = “Amit";
$name1*'Last'+ = “Kumar";
$marks = array
(
“Amit" => array (71, 68, 69, 64),
“Ashish" => array (57, 55, 68, 58)
);
echo $name[0];
echo " ";
echo $name[1];
echo "<br>";
print_r($name);
echo "<br>";
echo $name1['First'];
echo " ";
echo $name1['Last'];
echo "<br>";
print_r($name1);
echo "<br>";
print_r($marks);
?>
</body>
Strings in PHP -
• We can create a string variable in php, by just creating a
variable and assigning a string to it.
$str = “Hello PHP!”
• To know length of the string, we can use strlen() function.
strlen(“Hello PHP!”) //output – 10
• To search string within another string, we can use strpos()
function.
strpos(“Hello PHP!”, “P”) //output - 6
• We can concatenate two strings in php with the ‘ . ’ operator.
$str1 = “Hello”;
$str2 = “PHP!”;
Then – echo $str1.’ ‘.str2; //output – Hello PHP!
Operators in PHP -
Operator Example
. echo ‘Hello!’.’<br />’.’World’;
= $a = 4;
Arithmetic Operators
+ $a + $b =4+2=6
- $a - $b =5-2=2
* $a * $b = 6 * 2 = 12
/ $a / $b = 7/ 2 = 3
% $a % $b =9%2=1
Assignment Operators -
Assignment Same Statement Description
The left operand gets set to the value of the
x=y x=y
expression on the right
x += y x=x+y Addition
x -= y x=x-y Subtraction
x *= y x=x*y Multiplication
x /= y x=x/y Division
x %= y x=x%y Modulus
a .= b a=a.b Concatenate two strings
Increment/Decrement Operators -
Operator Name Description
++ x Pre-increment Increments x by one, then returns x
x ++ Post-increment Returns x, then increments x by one
-- x Pre-decrement Decrements x by one, then returns x
x -- Post-decrement Returns x, then decrements x by one
Comparison Operator -
Operator Meaning Example Result
== Equal To $a == $b False
!= or <> Not Equal To $a != $b True
< Less Than $a < $b True
> Greater Than $a > $b False
<= Less Than or Equal To $a <= $b True
>= Greater Than or Equal To $a >= $b False
Logical Operator -
Operator Meaning Example Result
&& AND $a == 4 && $b == 6 True
|| OR $a != 4 || $b == 6 True
! NOT True (if $a is
!($a)
false)
Control Structures in PHP -
• If…Else statement
• Switch statement
• For Loop
• While Loop
• Do-while Loop
• For Loop
• Foreach Loop
If…Else statement -
• Syntax –
if (condition){
statement1;
} else if (condition){
statement 2;
} else
statement3;
Switch structure-
• Syntax –
switch($variable)
{
case ‘value1’:
statement1;
break;
case ‘value2’:
statement2;
break;
default:
defaultstatement;
break;
}
While Loop -
• Syntax –
$var = 0;
while (condition is true)
{
statement1;
statement2;
}
Do while Loop -
• Syntax –
do
{
statement 1;
}while (condition is true);
For Loop –
• Syntax –
for (initialization; condition; increment)
{
statements;
}
Foreach Loop -
• It is used to loop through arrays.
• Syntax –
foreach ($array as $value)
{
statements;
}
Functions in PHP -
• Functions are the group of statements, needs to be
executed again and again.
• There are lots of built-in functions in PHP. Ex- strlen(),
strcmp(), strpos() etc…
• Syntax -
function function_name(para1, para2)
{
statements;
return variable;s
}
Forms -
• Forms provides a way to capture user input from a web page
and sends it to the server.
• Forms contains other elements like textbox, radio buttons,
checkbox, buttons etc. in the web page to capture the user
input.
• HTML forms are placed on a web page by using <form> tag.
This tag contains other form elements.
• The <form> tag uses the ‘action’ attribute to identify to where
to send the data and ‘method’ attribute to identify how to get
the data.
• It contains a SUBMIT button to send the data from one web
page to server.
• HTML itself can’t process the data. Hence the scripting
language such as PHP, PERL etc. are needed.
Example -
[Link] -
<form name=“myForm" action=“[Link]” method="post">
First Name: <input type="text" name=“firstname”/> <br />
Last Name: <input type="text" name=“lastname”/> <br />
<input type="submit" value="SUBMIT" />
</form>
[Link] –
<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
echo 'Your firstname is - '.$firstname;
echo '</br>';
echo 'Your lastname is - '.$lastname;
?>
$_GET and $_POST in php -
• These are predefined associative arrays , used to send data from
client to server.
• $_GET and $_POST collect values in a form with method="get"
and method="post" respectively.
• When method=“get” is used, the data passed to the server are
visible as query string in the browser.
• When method=“post” is used, the data is not visible in the
browser.
Example GET -
[Link] -
<form name=“myForm" action=“[Link]” method=“get">
First Name: <input type="text" name=“firstname”/> <br />
Last Name: <input type="text" name=“lastname”/> <br />
<input type="submit" value="SUBMIT" />
</form>
[Link]-
<?php
$firstname = $_GET['firstname'];
$lastname = $_GET['lastname'];
echo 'Your firstname is - '.$firstname;
echo '</br>';
echo 'Your lastname is - '.$lastname;
?>
Table generation example -
[Link] –
[Link] -
Example for different input elements in html forms –
[Link] -
[Link] -
[Link] -
Assignment 2-
• Create a web page for calculator. Take two numbers as input
from the user in the text box as ‘operand1’ & ‘operand2’ along
with list of operators to add, subtract, divide, multiply and
modulus. Based on the two inputs and operand selected,
display the result on the next page when the calculate button
is clicked.