You are on page 1of 27

PHP Overview

PHP was originally created by Rasmus Lerdorf in 1995. And has been in continuous development ever
since. The main implementation of PHP is now produced by the PHP Group and serves as the de facto
standard (De facto is a Latin expression that means “by [the] fact”) for PHP as there is no formal
specification. PHP is free software released under the PHP License.
PHP is free software released under the PHP License.

Hypertext Preprocessor is a widely used, general-purpose scripting language that was originally designed
for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the
HTML.

• Php stands for Php Hypertext Preprocessor.


• Php is a server-side scripting language, and it’s used to
Create dynamic WebPages/applications like Asp.
• Php scripts are executed on the server.
• Php support many databases like mysql, oracle, generic ODBC,
Sybase, solid & Informix etc…
• Php open source software.

Examples of other server-side scripting languages:-

* ASP (Active Server Pages)


* JSP (Java Server Pages)
* Python
* Perl
* PhP
* Asp.net

What is PHP?

Hypertext Preprocessor is a widely used, general-purpose scripting language that was originally designed
for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the
HTML.

* PHP stands for PHP: Hypertext Preprocessor


* PHP is a server-side scripting language, like ASP
* PHP scripts are executed on the server
* PHP supports many databases
(MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
* PHP is an open source software
* PHP is free to download and use

PHP is a general-purpose scripting language that is especially suited to server-side web development
where PHP generally runs on a web server. Any PHP code in a requested file is executed by the PHP
runtime, usually to create dynamic web page content. It can also be used for command-line scripting
and client-side GUI applications. PHP can be deployed on most web servers, many operating systems
and platforms, and can be used with many relational database management systems (RDBMS). It is
available free of charge, and the PHP Group provides the complete source code for users to build,
customize and extend for their own use.

Originally designed to create dynamic web pages, PHP now focuses mainly on server-side scripting, and
it is similar to other server-side scripting languages that provide dynamic content from a web server to a
client, such as Microsoft’s Asp.net, Sun Microsystems’ Java Server Pages, and mod_perl. PHP has also
attracted the development of many frameworks that provide building blocks and a design structure to
promote rapid application development (RAD). Some of these include CakePHP, Symphony, Code
Igniter, and Zend Framework, offering features similar to other web application frameworks.

Usage

PHP is a general-purpose scripting language that is especially suited to server-side web development
where PHP generally runs on a web server. Any PHP code in a requested file is executed by the PHP
runtime, usually to create dynamic web page content. It can also be used for command-line scripting
and client-side GUI applications. PHP can be deployed on most web servers, many operating systems
and platforms, and can be used with many relational database management systems (RDBMS). It is
available free of charge, and the PHP Group provides the complete source code for users to build,
customize and extend for their own use.

PHP primarily acts as a filter, taking input from a file or stream containing text and/or PHP instructions
and outputs another stream of data; most commonly the output will be HTML. Since PHP 4, the PHP
parser compiles input to produce byte code for processing by the Zend Engine, giving improved
performance over its interpreter predecessor.
Originally designed to create dynamic web pages, PHP now focuses mainly on server-side scripting,[34]
and it is similar to other server-side scripting languages that provide dynamic content from a web server
to a client, such as Microsoft’s Asp.net, Sun Microsystems’ Java Server Pages, and mod_perl. PHP has
also attracted the development of many frameworks that provide building blocks and a design structure
to promote rapid application development (RAD). Some of these include CakePHP, Symfony,
CodeIgniter, and Zend Framework, offering features similar to other web application frameworks.

The LAMP architecture has become popular in the web industry as a way of deploying web applications.
PHP is commonly used as the P in this bundle alongside Linux, Apache and MySQL, although the P may
also refer to Python or Perl or some combination of the three. WAMP packages (Windows/ Apache/
MySQL / PHP) and MAMP packages (Macintosh / Apache / MySQL / PHP) are also available.

As of April 2007, over 20 million Internet domains had web services hosted on servers with PHP installed
and mod_php was recorded as the most popular Apache HTTP Server module. PHP is used as server-side
programming language on 75% of all web servers.

PHP Introduction

Hypertext Preprocessor is a widely used, general-purpose scripting language that was originally designed
for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the
HTML.

PHP was originally created by Rasmus Lerdorf in 1995.

PHP is free software released under the PHP License.


What is PHP?

* PHP stands for PHP: Hypertext Preprocessor


* PHP is a server-side scripting language, like ASP
* PHP scripts are executed on the server
* PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC,
etc.)
* PHP is an open source software
* PHP is free to download and use

PHP is a powerful server-side scripting language and it is


used to create dynamic web pages/applications.
Examples of other server-side scripting languages,
* ASP (Active Server Pages)
* JSP (Java Server Pages)
* Python
* Perl

What is a PHP File?

* PHP files can contain text, HTML tags and scripts


* PHP files are returned to the browser as plain HTML
* PHP files have a file extension of “.php”, “.php3”, or “.phtml”

What is MySQL?

* MySQL is a database server


* MySQL is ideal for both small and large applications
* MySQL supports standard SQL
* MySQL compiles on a number of platforms
* MySQL is free to download and use

PHP + MySQL

* PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix
platform)

Why PHP?

* PHP runs on different platforms (Windows, Linux, Unix, etc.)


* PHP is compatible with almost all servers used today (Apache, IIS, etc.)
* PHP is FREE to download from the official PHP resource: http://www.php.net
* PHP is easy to learn and runs efficiently on the server side

Where to Start?

To get access to a web server with PHP support, you can:

* Install Apache (or IIS) on your own server, install PHP, and MySQL
* Or find a web hosting plan with PHP and MySQL support
Download PHP

Download PHP for free here: http://www.php.net/downloads.php


Download MySQL Database

Download MySQL for free here: http://www.mysql.com/downloads/


Download Apache Server

Download Apache for free here: http://httpd.apache.org/download.cgi

Basic PHP Syntax

<?php

PHP Code Here

?>

Comments in PHP

1. single line comment


2. multiline comment
//This is a comment or #

/*
PHP is a Loosely Typed Language
This is
a comment
block
*/

What is PHP Used For

PHP can preform calculations ,PHP can collect user information ,PHP can interact with MySQL databases

PHP Introduction

Hypertext Preprocessor is a widely used, general-purpose scripting language that was originally designed
for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the
HTML.

PHP was originally created by Rasmus Lerdorf in 1995.

PHP is free software released under the PHP License.

What can PHP do?

Anything. PHP is mainly focused on server-side scripting, so you can do anything such as collect form
data, generate dynamic page content, or send and receive cookies. But PHP can do much more.

PHP script Types


There are three main areas where PHP scripts are used. :-

 Server-side scripting,

 Command line scripting,

 Writing desktop applications

Declaring PHP

PHP scripts are always enclosed in between two PHP tags. This tells your server to parse the information
between them as PHP. The three different forms are as follows:

<?

PHP Code In Here

?>

<?php

PHP Code In Here

php?>

<script language=”php”>

PHP Code In Here

</script>

Php Variables

Variables in PHP :- A variable is used to store information.like text strings, numbers or arrays.
all variables in php start with a $ sign symbol.
Every Sentence end with semicolon
Syntax :- $variblename=value ;
Example :- $name=“shree” ; // (String)
$regno=20789; //(Number)
$result=true ; // (Boolean)

Data Types

Data Types :-
1.Typed
2.Untyped ( Loosely typed)
Php is loosely typed ( UnTyped ) language .
Php is automatically converts the variable to the correct data types depending on how they are set.

PHP Naming Rules

1. A variable name must start with alerter or underscore


example:- _ or abc
2. A variable name can only contain alpha-numeric ,Characters underscore
Example :- a-z , 0-9 , _ and a10
3. A variable name should not spaces if variable name is more than one word it should be separated
with underscore.

Example :- $first_name=“Srikanth Reddy”;

The Concatenation operator

The concatenation operator ( . ) is used to put two string values together.

Example :-

$first_name=”srikanth Reddy”;

$last_name=”M”;

$name=$first_name.$last_name;

echo $name;

// Result will show as Srikanth Reddy .M

PHP Usefull Editors

Note pad
Magma studio
Dreamweaver
Edit plus
Eclipse
HTML-Kit
PHPEdit
Notepad++
Net beans IDE
PHPEdit
phpDesigner

PHP – Input & output Statements

Input statement :- variable Declaration


Example :- $name=“shree”;

Output statement :-Example :- variable Display


Syntax :- echo (variable name);
print (variable name);
printf(variable name);

Example :- echo $name;


echo($name);
print($name);
printf($name);

PHP Simple Intrest Example

simple intrest

example :-

<?php

$p=5000;
$r=5;
$t=3;
$s=($p*%t*$r)/100;
$ta=$p+$s;

echo $p;
echo $s;
echo $ta;

?>

PHP Sum Of Given Numbers Example By using For Loop

<center><h1>
<?php

$sum=0;

for($n=1;$n<=5;$n=$n+1)
{

$sum=$sum+$n;

echo $sum;

?>

PHP For Loop Example

<center><h1>
<?php

for($n=1;$n<=100;$n=$n+1)
{

if($n%2==0)
{

echo”<br>”.$n;

if($n==20)
{

break;
}

}
?>

PHP Student Marksheet Example

<center><h1>
<?php

$name=”shree”;
$regno=1;

$s1=40;
$s2=20;
$s3=30;

$t=$s1+$s2+$s3;
$p=$t/3;

if($p>=60)
{
echo ” first division “;
}
if($p>=50 and $p<60)
{
echo ” Second division “;
}
if($p>=40 and $p<50)
{
echo ” Third division “;

if($p<40)
{
echo ” fail”;
}

echo “<br>”;
if($s1>=35 and $s2>=35 and $s3>=35)
{
$r=”pass”;
}
else
{
$r=”fail”;
}

echo $r;
echo $t;
echo $p;

?>

PHP While Loop Example

<center><h1>
<?php

$n=1;
while($n<=100)
{

if($n%2==0)
{
echo “<br>”.$n;

if($n==20)
{
break;
}

}
$n=$n+1;

?>

PHP Switch Case Example

<center><h1>
<?php
echo ” <br>1 . Addition “;
echo ” <br>2 . Subtraction “;
echo ” <br>3 . Mutiplication”;
echo ” <br>4 . Division “;
echo ” <br>5 . Remainder<br> “;
$n=12;

switch($n)
{

case 1:
$a=10;$b=20;$c=$a+$b;
echo ” Addition Of Two Numbers :-“.$c;break;

case 2:

$a=10;$b=20;$c=$a-$b;
echo ” Subtration of two numbers :-“.$c;break;

case 3:

$a=10;$b=20;$c=$a*$b;
echo ” Mutiplication of two numbers :- “.$c;break;

case 4:

$a=10;$b=20;$c=$a/$b;
echo ” Division of two numbers :-“.$c;break;

case 5:
$a=10;$b=20;$c=$a%$b;
echo ” Remainder value :-“.$c;break;

default: echo ” there is no any Above Given Choice”;

PHP Student Result Example

<center><h1>

<?php
$name=”shree”;
$regno=1;

$s1=90;
$s2=90;
$s3=90;
$t=$s1+$s2+$s3;
$p=$t/3;

if($s1>=35 and $s2>=35 and $s3>=35)


{
$r=”pass”;
}
else
{
$r=”fail”;
}

echo $r;
echo $t;
echo $p;

?>

PHP variable swaping Program with third party variable Example

variable swaping with third party variable

Eaxmple :-

<?php

$a=10;
$b=20;

echo ” before swapping variables ”


echo $a;
echo $b;

$t=$a;
$a=$b;
$b=$t;

echo ” After swapping variables ”


echo $a;
echo $b;
?>

PHP variable swapping without third party variable

variable swap without third party variable.

<?php
$a=10;
$b=20;

echo ” before swapping variables ”


echo $a;
echo $b;
$a=$a+$b; // $a=$a*$b;
$b=$a-$b; // $b=$a/$b;
$a=$a-$b; // $a=$a/$b;

echo ” after swapping variables ”


echo $a;
echo $b;

?>

PHP Control Structure

control structure :-

Determine the flow of control in program.

there are four types of control structurs. they are

1. sequence c.s :-

are executed in the same order in which they appear in the program.

2.selection or decision control s :-

allow the computer to take a decision as which instruction or control is to be executed next.
3.Repitition or loop c.s :-

to execute the group of statements repeatedly.


4. case c.s :-

allow the computer to take a case as which instruction or control is to be executed next.

OR

PHP Control Structure:-

1. Conditional Control Structure


2. Iterative Control Structure

Sno This Expression Meaning

1 x==y x is equal to y
2 !x=y x is not equal to y

3 x<y x is less than y

4 x>y x is greater than y

5 x<=y x is less than or equal to y

6 x>=y x is greater than or equal to y

PHP Control Structure

1. Conditional Control Structure


2. Iterative Control Structure

1. Conditional Control Structure

Trinary Condition/ Operator

$result= (Condition)?”true”: “false”;

example :-
$a=50;
$result=($a%2==0)?”Even”: “odd”;
echo $result

PHP If…Else Statements:-

Conditional statements are used to perform different actions based on different conditions and different
decisions.

* if statement – use this statement to execute some code only if a specified condition is true

* if…else statement – use this statement to execute some code if a condition is true and another code if
the condition is false

* if…elseif….else statement – use this statement to select one of several blocks of code to be executed

* switch statement – use this statement to select one of many blocks of code to be executed.

Syntax:-

$name=”srikanth reddy”;
if ($name==”srikanth reddy”)
echo “Have a nice Day Mr Srikanth Reddy “;

The if…else Statement: – Use the if….else statement to execute some code if a condition is true and
another code if a condition is false.

Syntax:-
If (condition)
Code to be executed if condition is true;
else
Code to be executed if condition is false;

Eaxmple :-

$name=”srikanth”;
if ($name==”srikanth reddy”)
{
echo “Have a nice Day Mr Srikanth Reddy “;
}
else
{
echo “Have a nice Day Mr “. $name;
}

Example :-

$number_three = 3;

if ($number_three == 3)
{
echo “The if statement evaluated to true”;
}
else
{
echo “The if statement evaluated to false”;
}

The if…elseif….else Statement:-Use the if….elseif…else statement to select one of several blocks of code
to be executed.

Example :-

$name1=”srikanth”;
$name2=”Reddy
if ($name1==”srikanth”)
{
echo “Have a nice Day Mr Srikanth “;
}
elseif($name2==”Reddy”)
{
echo “Have a nice Day Mr Reddy “;
}
else
{
echo “Have a nice Day Mr x “;
}

PHP Switch Statement: – Use the switch statement to select one of many blocks of code to be executed.

Syntax:-

Example :-

switch (condition)
{

case “value1” :
block of coding;
if the condition equals value1;
break;

case “value2” :
block of coding;
if the condition equals value2;
break;

default :
block of coding;
if the condition does not equal value1 or value2;
break;

Example :-
$n=2
Switch ($n)
{
case 1:
echo “Number 1”;
break;
case 2:
echo “Number 2”;
break;
case 3:
echo “Number 3”;
break;
default:
echo “No number between 1 and 3”;
}

Example :-
$destination = “Tokyo”;
echo “Traveling to $destination
“;
switch ($destination)
{
case “Las Vegas”:
echo “Bring an extra $500”;
break;
case “Amsterdam”:
echo “Bring an open mind”;
break;
case “Egypt”:
echo “Bring 15 bottles of SPF 50 Sunscreen”;
break;
case “Tokyo”:
echo “Bring lots of money”;
break;
case “Caribbean Islands”:
echo “Bring a swimsuit”;
break;
default:
echo “Bring lots of Money!”;
break;

}
?>

Example :-

$n=2;
switch($n)
{
case 1;
header("Location: http://www.google.co.uk&quot;);
break;

case 2";
header("Location: http://www.youtube.com&quot;);
break;

Example :-

$value=1;

switch ($value)
{
case 1 || 2:
echo ‘the value is either 1 or 2’;
break;

Iterative Control Structure

Php Loops

Loops execute a block of code a specified number of times, or while a specified condition is true.

* while – loops through a block of code while a specified condition is true

* do…while – loops through a block of code once, and then repeats the loop as long as a specified
condition is true

* for – loops through a block of code a specified number of times

* foreach – loops through a block of code for each element in an array

The while Loop:-

Syntax:-

while (condition)
{
code to be executed;
}

Example:-

$i=1;
while($i<=5)
{
echo "The number is”. $i . "
“;
$i++;
}

Exampple :-

$test = 0;
while ($test < 10)
{
echo "$test is less than 10.
“;
$test++;
}

The do…while Statement:-


Syntax:-
do
{
code to be executed;
}
while (condition);

Exampple :-

$i=1;
do
{

echo “The number is ” . $i . “


“;
$i++;
}while ($i<=5);

Difference between While loop and do while loop:-

While loop:-
If condition is false then out of the loop. It meanes does not display any thing.

Do-While loop:-
If condition is false but atlest once this statement would be execute.

Break :- Brings the control to the end of loop.


Continue :- Brings back the control to the beginning of loop.

Note: – Both break and continue should be used inside on if conditional control structure.

For Loops:-

Syntax:-The for loop is used when you know in advance how many times the script should run.

Syntax:-

for (init; condition; increment)


{
code to be executed;
}

Exampple :-

for ($i=1; $i<=5; $i++)


{
echo "The number is " . $i . "
“;
}
Exampple :-

$brush_price = 5;
$counter = 10;

echo “

“;
echo “

“;
echo “

“;
while ( $counter <= 100 ) {
echo "

“;
$counter = $counter + 10;
}
echo “

Quantity Price

“; “;
echo $counter; echo $brush_price * $counter;
echo “ echo “

“;

The function of the while loop is to do a task over and over as long as the specified conditional
statement is true. This logical check is the same as the one that appears in a PHP if statement to
determine if it is true or
false

Exampple :-

$brush_price = 5;

echo “

“;
echo “

“;
echo “

“;
for ( $counter = 10; $counter <= 100; $counter += 10) {
echo "
“;
}
echo “

Quantity Price

“; “;
echo $counter; echo $brush_price * $counter;
echo “ echo “

“;

Exampple :-

$num = 1;
while ( $num <=10 )
{
print $num . " ";
$num++;
}

Exampple :-

$num = 1;
while ( $num <=10 )
{
if ($num < 5)
{
print $num . " is less than 5
“;

}
else
{
print $num . ” is not less than 5
“;
}
$num++;
}

The foreach Loop:-The foreach loop is used to loop through arrays

Syntax:-
foreach ($array as $value)
{
code to be executed;
}

Exampple :-
$x=array (“one”,”two”,”three”);
foreach ($x as $value)
{
echo $value . “
“;
}

PHP Example

Major or Minor By Using If Condition :-

<?php

$age=9;

if($age>=18)
{
echo ” Major”;
}
else
{
echo “Minor”;
}

?>

Discount Calculation Example :-

<?php

$p=100;
$q=20;

if($q>=20)
{
$d=0.1;

$da=$p*$q*$d;
}
else
{
$d=0;

$da=$p*$q*$d;
}

$a=($p*$q);

$dta=$a-$da;
echo “<br>Total Amount:-“.$a;
echo “<br>Discount Amount:-“.$da;
echo “<br>Discount total Amount:-“.$dta;
?>

Employee Salary Calculation :-

<center><h1>
<?php

$name=”shree”;

$bsal=20000;

if($bsal>=35000)
{

$hra=$bsal*0.1;
$da=$bsal*0.2;

}
else
{
$hra=$bsal*0.05;
$da=$bsal*0.1;

$ts=$bsal+$hra+$da;

echo “<Br>Employee Basic Salary=”.$bsal;


echo “<Br>Employee Hra=”.$hra;
echo “<Br>Employee Da=”.$da;
echo “<Br>Employee Total Salary=”.$ts;

?>

Conditional Control Structure :-

PHP If…Else Statements:-

Conditional statements are used to perform different actions based on different conditions and different
decisions.

* if statement – use this statement to execute some code only if a specified condition is true

* if…else statement – use this statement to execute some code if a condition is true and another code if
the condition is false

* if…elseif….else statement – use this statement to select one of several blocks of code to be executed
* switch statement – use this statement to select one of many blocks of code to be executed

Syntax:-

<?php

$name=”srikanth reddy”;
if ($name==”srikanth reddy”)
echo “Have a nice Day Mr Srikanth Reddy “;

?>

The if…else Statement: – Use the if….else statement to execute some code if a condition is true and
another code if a condition is false.

Syntax:-

If (condition)
Code to be executed if condition is true;
else
Code to be executed if condition is false;

<php
$number_three = 3;

if ($number_three == 3)
{
echo “The if statement evaluated to true”;
}
else
{
echo “The if statement evaluated to false”;
}
?>
The if…elseif….else Statement:-Use the if….elseif…else statement to select one of several blocks of code
to be executed.

Iterative Control Structure

Php Loops

Loops execute a block of code a specified number of times, or while a specified condition is true.

* while – loops through a block of code while a specified condition is true

* do…while – loops through a block of code once, and then repeats the loop as long as a specified
condition is true

* for – loops through a block of code a specified number of times


* foreach – loops through a block of code for each element in an array

The while Loop:-

Syntax:-

while (condition)
{
code to be executed;
}

Example:-

$i=1;
while($i<=5)
{
echo "The number is”. $i . "
“;
$i++;
}

Exampple :-

$test = 0;
while ($test < 10)
{
echo "$test is less than 10.
“;
$test++;
}

The do…while Statement:-


Syntax:-

do
{
code to be executed;
}
while (condition);

Exampple :-

$i=1;
do
{

echo “The number is ” . $i . “


“;
$i++;
}while ($i<=5);

Difference between While loop and do while loop:-

While loop:-
If condition is false then out of the loop. It meanes does not display any thing.

Do-While loop:-
If condition is false but atlest once this statement would be execute.

Break :- Brings the control to the end of loop.


Continue :- Brings back the control to the beginning of loop.

Note: – Both break and continue should be used inside on if conditional control structure.

For Loops:-

Syntax:-The for loop is used when you know in advance how many times the script should run.

Syntax:-

for (init; condition; increment)


{
code to be executed;
}

Exampple :-

for ($i=1; $i<=5; $i++)


{
echo "The number is " . $i . "
“;
}

Exampple :-

$brush_price = 5;
$counter = 10;

echo “

“;
echo “

“;
echo “

“;
while ( $counter <= 100 ) {
echo "
“;
$counter = $counter + 10;
}
echo “

Quantity Price

“; “;
echo $counter; echo $brush_price * $counter;
echo “ echo “

“;

The function of the while loop is to do a task over and over as long as the specified conditional
statement is true. This logical check is the same as the one that appears in a PHP if statement to
determine if it is true or
false

Exampple :-

$brush_price = 5;

echo “

“;
echo “

“;
echo “

“;
for ( $counter = 10; $counter <= 100; $counter += 10) {
echo "

“;
}
echo “

Quantity Price

“; “;
echo $counter; echo $brush_price * $counter;
echo “ echo “

“;

Exampple :-

$num = 1;
while ( $num <=10 )
{
print $num . " ";
$num++;
}

Exampple :-

$num = 1;
while ( $num <=10 )
{
if ($num < 5)
{
print $num . " is less than 5
“;

}
else
{
print $num . ” is not less than 5
“;
}
$num++;
}

The foreach Loop:-The foreach loop is used to loop through arrays

Syntax:-
foreach ($array as $value)
{
code to be executed;
}

Exampple :-

$x=array (“one”,”two”,”three”);
foreach ($x as $value)
{
echo $value . “
“;
}