PHP is a server scripting language means code does not need to be compiled before it gets used.
PHP is a widely-used open source general-purpose language.
To learn PHP you should know basics of HTML(Hypertext Language).
PHP is extremely easy for a newcomer and offers very advanced features to a professional programmer.
Where PHP Use:
PHP can be used on the various operating system(Microsoft Windows, Unix variants, Linux, etc.).
PHP also supports almost all servers(Apache, IIS and many others).
Also, PHP supports the wide range of databases(DBA, CUBRID, DB++, Mongo, MYSQL, etc.).
This is the strongest feature in PHP.
Therefore, you can choose any operating system and any server with PHP.
PHP script is used in following 3 main areas:
Server-side scripting
Commond-line scripting
Desktop application
PHP Overview
The PHP code is enclosed in start() processing instructions.
Each separate instruction must end with a semicolon (;).
echo is same as printf() in C language which uses for printing outputs.
Whatever you want to print on the screen of a web browser insert it in quotation marks(” “).
In PHP we don’t have to give data type of variable, PHP automatically converts it.
In other languages like C, C++, Java, etc. it is necessary to give data type of variable.
In associative array, we can store one value with only one key.
But if we want to store one or more keys for one value.
This can be done using multi-dimensional arrays.
A multi-dimensional array can be two-dimensional or three-dimensional array depending on indices.
A two-dimensional array is known as array of arrays.
A three-dimensional array is known as array of arrays of arrays.
Using multi-dimensional array you can store data in table form.
Let’s see following example:
Subject
Marks
Grade
Mathematics
23
D
Science
45
C
English
65
B
Drawing
80
A
Now this above table can be stored using multi-dimensional array as.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$Scorecard=array
(
array("Mathematics",23,"D"),
array("Science",45,"C"),
array("English",65,"B"),
array("Drawing",80,"A"),
);
echo"Marks in subject ".$Scorecard[0][0]." is ".$Scorecard[0][1]." with grade ".$Scorecard[0][2]."<br>";
echo"Marks in subject ".$Scorecard[1][0]." is ".$Scorecard[1][1]." with grade ".$Scorecard[1][2]."<br>";
echo"Marks in subject ".$Scorecard[2][0]." is ".$Scorecard[2][1]." with grade ".$Scorecard[2][2]."<br>";
echo"Marks in subject ".$Scorecard[3][0]." is ".$Scorecard[3][1]." with grade ".$Scorecard[3][2]."<br>";
echo"Following are countries names: $country[0], $country[1], $country[2]";
?>
Output:
1
Following are countries names:India,Australia,Switzerland
1
2
3
4
5
6
<?php
$country[0]="India";
$country[1]="Australia";
$country[2]="Switzerland";
echo"Following are countries names: $country[0], $country[1], $country[2]";
?>
Output:
1
Following are countries names:India,Australia,Switzerland
Here, $country is a name of variable.
0,1,2 are the index numbers of the elements.
India, Australia, Switzerland are the values assigned to array elements.
PHP forloop is the most complex loops in PHP. They behave like their C counterparts.
Syntax of PHP for loop:
Following is given a general syntax of for loop in PHP:
1
2
3
4
for(initialization;condition;increment/decrement)
{
//statements;
}
In the beginning, loop condition is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed.
If it evaluates to FALSE, the execution of the loop ends.
All three expression, initialization, condition, increment/decrement can be empty or multiple.
If the condition is empty then loop will execute infinite considering it is true.
If the expressions are multiple then they are separated by commas.
PHP do-while loop are very similar to while loop.
Only one difference in do-while, loop condition is checked at end of each iteration.
Whereas, in while loop condition is checked at beginning of loop.
Also, in do-while, loop is execute at onces without checking condition.
Output of do-while loop and while loop is same.
Syntax of PHP do-while
Following is given a general syntax of do-while loop in PHP:
Following is given a general syntax of while loop in PHP:
1
2
3
4
5
6
while(condition)
{
// statement;
}
The multiple statements inside while loop get executed only when expression is true.
These multiple statements enclosed within the curly braces or using the alternate syntax:
Initially if while condition is true then only while statements will execute otherwise they will not execute at even once.
1
2
3
4
while(condition):
statement
...
endwhile;
Flowchart of PHP while loop
Example
1
2
3
4
5
6
7
8
<?php
$a=0;
while($a<=5)
{
echo"a = $a<br>";
$a++;
}
?>
Output:
1
2
3
4
5
6
a=0
a=1
a=2
a=3
a=4
a=5
Or you can use while loop as following:
It will print same output as above example only we use endwhile instead curly braces { }.
switch is similar with multiple if structure.
Sometimes you want to compare same expression with different multiple values, and execute a different statements depending on value. This can be done using switch statement.
How switch work
A switch structure execute line by line. If the case value matches the value of switch expression then only the statements will execute.
In a switch statement, the condition is evaluated only once and the result is compared to each case statement.
Syntax of switch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
switch(condition)
{
casevalue1:statement1;
break;
casevalue2:statement2;
break;
casevalue3:statement3;
break;
default:statementN;
}
?>
Flowchart of switch
Example of switch –
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
$m=6;
switch($m)
{
case1:echo"You born in January";
break;
case2:echo"You born in February";
break;
case3:echo"You born in March";
break;
case4:echo"You born in April";
break;
case5:echo"You born in May";
break;
case6:echo"You born in June";
break;
case7:echo"You born in July";
break;
case8:echo"You born in August";
break;
case9:echo"You born in September";
break;
case10:echo"You born in October";
break;
case11:echo"You born in November";
break;
case12:echo"You born in December";
break;
default:echo"Please enter valid month number";
}
?>
Output
1
You born inJune
PHP continues to execute the statements until the end of the switch block, or it sees a break statement.
If there is no break keyword at end of case’s statements, PHP will execute the statements of the following case.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
$m=6;
switch($m)
{
case1:echo"You born in January";
case2:echo"You born in February";
case3:echo"You born in March";
case4:echo"You born in April";
case5:echo"You born in May";
case6:echo"You born in June";
case7:echo"You born in July";
case8:echo"You born in August";
case9:echo"You born in September";
case10:echo"You born in October";
case11:echo"You born in November";
case12:echo"You born in December";
default:echo"Please enter valid month number";
}
?>
Output
1
2
3
4
5
6
7
8
You born inJune
You born inJuly
You born inAugust
You born inSeptember
You born inOctober
You born inNovember
You born inDecember
Please enter valid month number
Switch case can be a string. For example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
$d=date("D");
switch($d){
case"Mon":
echo"Today is Monday";
break;
case"Tue":
echo"Today is Tuesday";
break;
case"Wed":
echo"Today is Wednesday";
break;
case"Thu":
echo"Today is Thursday";
break;
case"Fri":
echo"Today is Friday";
break;
case"Sat":
echo"Today is Saturday";
break;
case"Sun":
echo"Today is Sunday";
break;
default:
echo"Enter valid day";
}
?>
case statement can be empty, which simply passes control to next case statement.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$a=3;
switch($a)
{
case0:
case1:
case2:echo"You choose 3";
break;
case3:echo"You choose 4";
break;
}
?>
Default case is a special case. If any case value wasn’t matched with switch expression result then default case will execute.
This keyword uses both keywords if and else.
In this structure, in case the first if expression evaluates to FALSE.
It will execute alternative elseif expression.
else if is same as elseif.
Syntax of elseif
1
2
3
4
5
6
7
8
9
10
11
12
if(expression)
{
// statements
}
elseif
{
//statements
}
else
{
//statements
}
Flowchart of else if
There may be several elseifs within the same if statement. The first elseif expression (if any) that evaluates to TRUE would be executed. In PHP, you can also write ‘else if’ (in two words) and the behavior would be identical to the one of ‘elseif’ (in a single word). The syntactic meaning is slightly different (if you’re familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.
The elseif statement is only executed if the preceding if expression and any preceding elseif expressions evaluated to FALSE, and the current elseif expression evaluated to TRUE.
Example of elseif-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
$a=10;
$b=10;
if($a<$b)
{
echo"a is bigger than b";
}
elseif($a>$b)
{
echo"b is bigger than a";
}
else
{
echo"a is equal to b";
}
?>
/* output: a is equal to b */
There may be several elseif within the same if statement