0% found this document useful (0 votes)
29 views

PHP English-2020 AL

PHP is a widely-used open source scripting language suited for web development. PHP code is executed on the server and returns HTML to the browser. PHP files have a ".php" extension. Comments in PHP begin with either # for single-line or /* */ for multi-line comments. PHP supports variables, arrays, conditional statements like if/else, and loops like while, do-while, for, and foreach to control program flow. Functions allow code reuse and organization in PHP programs.

Uploaded by

shenura
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

PHP English-2020 AL

PHP is a widely-used open source scripting language suited for web development. PHP code is executed on the server and returns HTML to the browser. PHP files have a ".php" extension. Comments in PHP begin with either # for single-line or /* */ for multi-line comments. PHP supports variables, arrays, conditional statements like if/else, and loops like while, do-while, for, and foreach to control program flow. Functions allow code reuse and organization in PHP programs.

Uploaded by

shenura
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

e Business Imaging Systems ebis

No.540 D, 'Sathosa Building, Old Road, Kottawa, Tel.0716-99 99 44 / 55, 0112780713

PHP (Hypertext Preprocessor)


PHP is a widely-used open source, server side and general-purpose scripting language that is especially suited for
web development and can be embedded into HTML. PHP is a server scripting language, and a powerful tool for
making dynamic and interactive Web pages. PHP files can contain text, HTML, CSS, JavaScript, and PHP code.

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.

Writing code using PHP

Syntax
<?php
?>

Starting with <?php and ending with ?>.

Commenting PHP Code


A comment is the portion of a program that exists only for the human reader and stripped out before displaying
the programs result. There are two commenting formats in PHP −
Single-line comments − They are generally used for short explanations or notes relevant to the local code. Here
are the examples of single line comments.
<?
# This is a comment, and
# This is the second line of the comment

// This is a comment too. Each style comments only


print "An example with single line comments";
?>

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

PHP - Variable Types


Here are the most important things to know about variables in PHP.
 All variables in PHP are denoted with a leading dollar sign ($).
 The value of a variable is the value of its most recent assignment.
 Variables are assigned with the = operator, with the variable on the left-hand side and the expression to
be evaluated on the right.

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.

If you have a list of items (a list of subjects names), for example),

$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

The above code displays only "Physics"

since
$subjects[0] = "ICT";
$subjects[1] = "Physics";
$subjects[2] = "Combined Maths";

PHP Conditional Statements


In PHP we have the following conditional statements:

if statement - executes some code if one condition is true


if...else statement - executes some code if a condition is true and another code if that condition is
false
if...elseif....else statement - executes different codes for more than two conditions
switch statement - selects one of many blocks of code to be executed

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 ($t < "20") {


echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
e Business Imaging Systems ebis
No.540 D, 'Sathosa Building, Old Road, Kottawa, Tel.0716-99 99 44 / 55, 0112780713

if...elseif....else Statement

<?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!";
}
?>

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.

In PHP, we have the following loop types:

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");

foreach ($colors as $value) {


echo "$value <br>";
}
?>
e Business Imaging Systems ebis
No.540 D, 'Sathosa Building, Old Road, Kottawa, Tel.0716-99 99 44 / 55, 0112780713

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.

You might also like