What Is PHP
What Is PHP
HTML
CSS
JavaScript
Why PHP?
PHP runs on various platforms (Windows, Linux,
Unix, Mac OS X, etc.)
<?php
// PHP code goes here
?>
The default file extension for PHP files is ".php".
A PHP file normally contains HTML tags, and
some PHP scripting code.
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
Note: PHP statements end with a semicolon (;).
As you can see in the diagram above, we always start with a
browser making a request for a web page. This request is going
to hit the web server. The web server will then analyze it and
determine what to do with it.
If the web server determines that the request is for a PHP file
(often index.php ), it’ll pass that file to the PHP interpreter. The
PHP interpreter will read the PHP file, parse it (and other included
files) and then execute it. Once the PHP interpreter finishes
executing the PHP file, it’ll return an output. The web server will
take that output and send it back as a response to the browser.
Install PHP
To install PHP, we will suggest you to install AMP
(Apache, MySQL, PHP) software stack. It is
available for all operating systems. There are many
AMP options available in the market that are given
below:
o WAMP for Windows
o LAMP for Linux
o MAMP for Mac
o SAMP for Solaris
o FAMP for FreeBSD
PHP Variables
A variable in PHP is a name of memory location that
holds data. A variable is a temporary storage that is
used to store data temporarily.
$variablename=value;
PHP Variables
Rules for PHP variables:
Comments in PHP
// This is a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
PHP Operators
Operators are used to perform operations on variables and values.
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
PHP If Else
PHP if else statement is used to test condition. There are various ways to use if statement in
PHP.
o if
o if-else
o if-else-if
o nested if
PHP If Statement
PHP if statement is executed if condition is true.
Syntax
if(condition){
//code to be executed
}
<?php
$num=12;
if($num<100){
echo "$num is less than 100";
}
?>
Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
The example below will output "Have a good day!" if the current time is less than 20, and "Have
a good night!" otherwise:
Example
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}
Example
<?php
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
Example
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
PHP Loops
Often when you write code, you want the same block of code to run over and
over again in a row. Instead of adding several almost equal code-lines in a
script, we can use loops to perform a task like this.
Syntax
while (condition is true) {
code to be executed;
}
The example below first sets a variable $x to 1 ($x = 1). Then, the while loop
will continue to run as long as $x is less than, or equal to 5 ($x <= 5). $x will
increase by 1 each time the loop runs ($x++):
Example
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
The example below first sets a variable $x to 1 ($x = 1). Then, the do while
loop will write some output, and then increment the variable $x with 1. Then
the condition is checked (is $x less than, or equal to 5?), and the loop will
continue to run as long as $x is less than, or equal to 5:
Example
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
Notice that in a do while loop the condition is tested AFTER executing the
statements within the loop. This means that the do while loop would execute
its statements at least once, even if the condition is false the first time.
The example below sets the $x variable to 6, then it runs the loop, and then
the condition is checked:
Example
<?php
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
Syntax
for (init counter; test counter; increment counter) {
code to be executed;
}
Parameters:
Example
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
Syntax
foreach ($array as $value) {
code to be executed;
}
For every loop iteration, the value of the current array element is assigned to
$value and the array pointer is moved by one, until it reaches the last array
element.
The following example demonstrates a loop that will output the values of the
given array ($colors):
Example
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
<html>
<body>
<?php
?>
</body>
</html>
PHP - Arrays
There are three different kind of arrays and each array value is accessed
using an ID c which is called array index.
Numeric array − An array with a numeric index. Values are stored and
accessed in linear fashion.
Associative array − An array with strings as index. This stores element values
in association with key values rather than in a strict linear index order.
Multidimensional array − An array containing one or more arrays and values
are accessed using multiple indices
Numeric Array
These arrays can store numbers, strings and any object but their index will
be represented by numbers. By default array index starts from zero.
Example
Following is the example showing how to create and access numeric arrays.
<html>
<body>
<?php
$numbers[0] = "one";
$numbers[1] = "two";
$numbers[2] = "three";
$numbers[3] = "four";
$numbers[4] = "five";
?>
</body>
</html>
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>
Associative Arrays
The associative arrays are very similar to numeric
arrays in term of functionality but they are different
in terms of their index. Associative array will have
their index as string so that you can establish a
strong association between key and values.
To store the salaries of employees in an array, a
numerically indexed array would not be the best
choice. Instead, we could use the employees names
as the keys in our associative array, and the value
would be their respective salary.
<html>
<body>
<?php
$salaries['Ajay'] = "high";
$salaries['Sunil'] = "medium";
$salaries['Vijay'] = "low";
?>
</body>
</html>
Name=Ajay, Salary=55000
Name=Sunil, Salary=19000
Name=Vijay, Salary=5000
<html>
<body>
<?php
$salaries = array("Ajay" => 55000,
"Sunil" => 19000, "Vijay" => 5000);
$keys = array_keys($salaries);
$round = count($salaries);
for($i=0; $i < $round; ++$i) {
echo "name=". $keys[$i] . ' ' . "Salary=" .
$salaries[$keys[$i]] .
"</br>";
}
?>
</body>
</html>
name=Ajay Salary=55000
name=Sunil Salary=19000
name=Vijay Salary=5000
Multidimensional Arrays
A multi-dimensional array each element in
the main array can also be an array. And
each element in the sub-array can be an
array, and so on. Values in the multi-
dimensional array are accessed using
multiple index.
Example
In this example we create a two dimensional array to
store marks of three students in three subjects −
<html>
<body>
<?php
$marks = array(
"chemistry" => 39
),
"chemistry" => 29
),
"chemistry" => 39
);
?>
</body>
</html>
Example
<html>
<body>
<?php
$cars=array("san","dan","tan","jan");
sort($cars);
$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
echo $cars[$x];
?>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br />";
}
</body>
</html>
Example
<html>
<body>
<?php
$cars=array("san","dan","tan","jan");
rsort($cars);
$clength = count($cars);
echo $cars[$x];
?>
</body>
</html>
The following example sorts the elements of the $numbers
array in descending numerical order:
Example
<html>
<body>
<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
?>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<body>
<?php
asort($age);
echo "<br>";
?>
</body>
</html>
Key=Joe, Value=43
Key=Peter, Value=45
Key=Ben, Value=67
Example
<!DOCTYPE html>
<html>
<body>
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
</body>
</html>
Key=Ben, Value=37
Key=Joe, Value=43
Key=Peter, Value=35
Example
<!DOCTYPE html>
<html>
<body>
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
arsort($age);
</body>
</html>
Sort Array (Descending Order),
According to Key - krsort()
The following example sorts an associative array in
descending order, according to the key:
Example
<!DOCTYPE html>
<html>
<body>
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
</body>
</html>