PHP
PHP
INTRODUCTION
It world’s most popular programming languages for web
development, the PHP hypertext preprocessor
Today in use on over twenty million Web sites and more than a third
of the world’s Web servers.
Version Release Date
1.0 8 June 1995
2.0 1 November 1997
3.0 6 June 1998
4.0 22 May 2000
5.0 13 July 2004
6.x cancel
7 3 December 2015(stable)
7.3 6 December 2018(current
version)
Advantage of PHP
Performance
Portability(platform independent)
Ease of use
Open source
Third-party application support
Community support
Applications of PHP
PHP performs system functions, i.e. From files on a system it can
create, open, read, write, and close them.
PHP can handle forms, i.e. Gather data from files, save data to a file,
through email you can send data, return data to the user.
You add, delete, modify elements within your database through php.
Access cookies variables and set cookies.
Using PHP, you can restrict users to access some pages of your
website.
It can encrypt data.
PHP SOFTWARE REQUIREMENT
1. PHP server
The PHP community provides some types of software server
solution under the GNU (general public license).
These are the following:
Server Stands for
WAMP server
WAMP Microsoft window o/s, Apache
LAMP server Mysql PHP
LAMP Linux Operating System Apache
MAMP server Mysql PHP
MAMP Mac os Apache Mysql PHP
XAMPP server
XAMPP x-os(cross operating system)
Apache Mysql PHP Perl
PHP XAMPP installation
Download the file from https://
www.apachefriends.org/download.html and install as ordinary
software
Open the application called Xampp
Save file as following... Create a directory inside
xampp/htdocs/myproject/
Save it inside: c:xampp/htdocs/myproject/firstprog.Php
Run the PHP script
Open your browser and write in url localhost/myproject/firstprog.Php
PHP syntax
Syntax is a way to representation of PHP script.
Basically it gives the primary idea to specify the code format.
It also specify the area of a written code.
There are three ways to start PHP environment
1. The most commonly and effective PHP syntax:
<?php echo "Welcome to the world of php"; ?>
In the given example it begin with (< ?Php) and end with(? >).
Echo statement is used to print the given string.
Mandatory closing in("abcd")double quotes.
Terminate the script with semicolon because semicolon is known as terminator.
PHP syntax
As mentioned earlier, PHP is embedded in HTML.
To run PHP script you have to start your server like apache
Save with file extension .php
<html>
<head>
<title>Hello World</title>
</head>
<body>
<?php
echo "Hello, World!";
?>
</body>
</html>
CONT.
2. Short or short-open tags look like this:
<? echo "welcome to the world of php"; ?>
<?php
$first = 100;
$second = 200;
$third = $first + $second;
echo "Sum = ".$third; ?>
In the above example Declare $first , $second variable with value=100,
200 respectively.
Now add these two numbers using arithmetic operator ("+"). sum of these
two variable result stored in a third variable($sum).
Now print the sum passing ($third) with echo statement with a string.
<?php
$first = 1000;
$second = 500;
$third = $first - $second;
echo "Subtraction = ".$third;
?>
In the above example We perform subtraction using
variables( $first, $second) with vale=1000,500.
Subtract second variable from first, result is hold by
third variable($third) .
Print this third variable passing with echo statement.
Destroying PHP variables
To destroy a variable, pass the variable to php's unset( ) function. As in the following
<?php
example:
$name="steve";
echo $name; //unset( ) function destroy the variable
reference. unset($name);
?>
Output Sum = 300
Sum = Notice error undefined third variable
In the above example declare variable $name hold value="steve".
In this program we used unset() function to delete a particular variable.
First it show the output: "steve", because we pass unset function after echo statement.
Now pass variable name inside unset($name) function output will show an notice error(variable is undefined).
Variable names in PHP are case-sensitive
<?php
$name="rexx";
$NAME="rahul";
echo $name."<br/>";
echo $NAME; ?>
<?php
$count = 5;
while($count <= 15)
{
echo $count;
echo "<br>" ;
$count++;
}
?>
ARITHMETIC OPERATORS
There are following arithmetic operators supported by PHP language
Assume variable A holds 10 and variable B holds 20 then
!= Checks if the value of two operands are equal or not, if values are not equal then condition (A != B) is not true.
becomes true.
> Checks if the value of left operand is greater than the value of right operand, if yes then (A > B) is not true.
condition becomes true.
< Checks if the value of left operand is less than the value of right operand, if yes then (A < B) is true.
condition becomes true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if (A >= B) is not true.
yes then condition becomes true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if (A <= B) is true.
yes then condition becomes true.
Logical operators
There are following logical operators supported by PHP language
Assume variable A holds 10 and variable B holds 20 then
Operator Description Example
and Called Logical AND operator. If both the operands are true then (A and B) is true.
condition becomes true.
or Called Logical OR Operator. If any of the two operands are non (A or B) is true.
zero then condition becomes true.
&& Called Logical AND operator. If both the operands are non zero (A && B) is true.
then condition becomes true.
|| Called Logical OR Operator. If any of the two operands are non (A || B) is true.
zero then condition becomes true.
! Called Logical NOT Operator. Use to reverses the logical state of !(A && B) is false.
its operand. If a condition is true then Logical NOT operator will
make false.
ASSIGNMENT OPERATORS
There are following assignment operators supported by PHP language
<body>
<?Php
$d = date("d");
if ($d == "fri")
?>
</Body>
</html>
The elseif statement
If you want to execute some code if one of the several conditions are
true use the elseif statement
Syntax
If (condition)
{code to be executed if condition is true; }
elseif (condition)
{code to be executed if condition is true; }
else
{code to be executed if condition is false;}
Example
The following example will output "have a nice weekend!" If the current day is friday, and "have a
nice sunday!" If the current day is sunday. Otherwise, it will output "have a nice day!"
<html>
<body>
<?php
$d = date("D");
if ($d == "Fri")
echo "Have a nice weekend!";
elseif ($d == "Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
</body>
</html>
The switch statement
If you want to select one of many blocks of code to be executed, use the switch statement.
The switch statement is used to avoid long blocks of if..Elseif..Else code.
Syntax
switch (expression){
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed if expression is different from both label1 and label2; }