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.
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 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.
What is MySQL?
PHP + MySQL
* PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix
platform)
Why PHP?
Where to Start?
* 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
<?php
?>
Comments in PHP
/*
PHP is a Loosely Typed Language
This is
a comment
block
*/
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.
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.
Server-side scripting,
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
php?>
<script language=”php”>
</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.
Example :-
$first_name=”srikanth Reddy”;
$last_name=”M”;
$name=$first_name.$last_name;
echo $name;
Note pad
Magma studio
Dreamweaver
Edit plus
Eclipse
HTML-Kit
PHPEdit
Notepad++
Net beans IDE
PHPEdit
phpDesigner
simple intrest
example :-
<?php
$p=5000;
$r=5;
$t=3;
$s=($p*%t*$r)/100;
$ta=$p+$s;
echo $p;
echo $s;
echo $ta;
?>
<center><h1>
<?php
$sum=0;
for($n=1;$n<=5;$n=$n+1)
{
$sum=$sum+$n;
echo $sum;
?>
<center><h1>
<?php
for($n=1;$n<=100;$n=$n+1)
{
if($n%2==0)
{
echo”<br>”.$n;
if($n==20)
{
break;
}
}
?>
<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;
?>
<center><h1>
<?php
$n=1;
while($n<=100)
{
if($n%2==0)
{
echo “<br>”.$n;
if($n==20)
{
break;
}
}
$n=$n+1;
?>
<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;
<center><h1>
<?php
$name=”shree”;
$regno=1;
$s1=90;
$s2=90;
$s3=90;
$t=$s1+$s2+$s3;
$p=$t/3;
echo $r;
echo $t;
echo $p;
?>
Eaxmple :-
<?php
$a=10;
$b=20;
$t=$a;
$a=$b;
$b=$t;
<?php
$a=10;
$b=20;
?>
control structure :-
1. sequence c.s :-
are executed in the same order in which they appear in the program.
allow the computer to take a decision as which instruction or control is to be executed next.
3.Repitition or loop c.s :-
allow the computer to take a case as which instruction or control is to be executed next.
OR
1 x==y x is equal to y
2 !x=y x is not equal to y
example :-
$a=50;
$result=($a%2==0)?”Even”: “odd”;
echo $result
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");
break;
case 2";
header("Location: http://www.youtube.com");
break;
Example :-
$value=1;
switch ($value)
{
case 1 || 2:
echo ‘the value is either 1 or 2’;
break;
Php Loops
Loops execute a block of code a specified number of times, or 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
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++;
}
Exampple :-
$i=1;
do
{
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.
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:-
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++;
}
Syntax:-
foreach ($array as $value)
{
code to be executed;
}
Exampple :-
$x=array (“one”,”two”,”three”);
foreach ($x as $value)
{
echo $value . “
“;
}
PHP Example
<?php
$age=9;
if($age>=18)
{
echo ” Major”;
}
else
{
echo “Minor”;
}
?>
<?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;
?>
<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;
?>
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.
Php Loops
Loops execute a block of code a specified number of times, or 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
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++;
}
do
{
code to be executed;
}
while (condition);
Exampple :-
$i=1;
do
{
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.
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:-
Exampple :-
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++;
}
Syntax:-
foreach ($array as $value)
{
code to be executed;
}
Exampple :-
$x=array (“one”,”two”,”three”);
foreach ($x as $value)
{
echo $value . “
“;
}