PHP English-2020 AL
PHP English-2020 AL
PHP code are executed on the server, and the result is returned to the browser as plain HTML
PHP files have extension ".php" PHP source code not visible by client and view source in browsers.
Syntax
<?php
?>
Multi-lines comments
They are generally used to provide pseudocode algorithms and more detailed explanations when necessary. The
multiline style of commenting is the same as in C. Here are the example of multi lines comments.
<?
/* This is a comment with multiline
Author : Mohammad Mohtashim
Purpose: Multiline Comments Demo
Subject: PHP
*/
print "An example with multi line comments";
?>
e Business Imaging Systems ebis
No.540 D, 'Sathosa Building, Old Road, Kottawa, Tel.0716-99 99 44 / 55, 0112780713
eg:
<html>
<head>
<title>Hello World</title>
</head>
<body>
<?php
$N1=20;
$N2=10;
$N3=50;
$avg=($N1+$N2+$N3)/3;
echo "Average of N1, N2, N3 = ",$avg;
?>
</body>
</html>
Arrays
An array is a special variable, which can hold more than one value at a time.
$Subject1 = "ICT";
$Subject2 = "Physics";
$Subject3 = "Combined Maths";
Introducing more subjects one by one is very difficult. The solution is to create an array!
An array can hold many values under a single name, and you can access the values by referring to an index
number.
Syntax
array();
eg:
<?php
$subjects=array("ICT", "Physics", "Combined Maths");
echo "$subjects[1]";
?>
e Business Imaging Systems ebis
No.540 D, 'Sathosa Building, Old Road, Kottawa, Tel.0716-99 99 44 / 55, 0112780713
since
$subjects[0] = "ICT";
$subjects[1] = "Physics";
$subjects[2] = "Combined Maths";
if statement
Syntax
if (condition) {
code to be executed if condition is true;
}
eg:
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
?>
if...else Statement
<?php
$t = date("H");
if...elseif....else Statement
<?php
$t = date("H");
Control Structures
Control Structures (Loops) are used to execute the same block of code again and again, as long as a certain
condition is true.
while - loops through a block of code as long as the specified condition is true
do...while - loops through a block of code once, and then repeats the loop as long as the 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
While
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
$x = 1; - Initialize the loop counter ($x), and set the start value to 1
$x <= 5 - Continue the loop as long as $x is less than or equal to 5
$x++; - Increase the loop counter value by 1 for each iteration
e Business Imaging Systems ebis
No.540 D, 'Sathosa Building, Old Road, Kottawa, Tel.0716-99 99 44 / 55, 0112780713
Do .... While
The do...while loop will always execute the block of code once, it will then check the condition, and repeat the
loop while the specified condition is true.
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
The example above 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:
For loop
The for loop is used when you know in advance how many times the script should run.
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
Foreach
The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.
Syntax
foreach ($array as $value) {
code to be executed;
}
Example
<?php
$colors = array("red", "green", "blue", "yellow");
PHP functions
<?php
function familyName($fname) {
echo "$fname Perera.<br>";
}
familyName("Gimantha");
familyName("Kulith");
familyName("Mithila");
familyName("Sisara");
familyName("Nelum");
?>
Output
Gimantha Perera.
Kulith Perera.
Mithila Perera.
Sisara Perera.
Nelum Perera.