PHP Unit-2
PHP Unit-2
Website FCAIT
SEM V
Development BCA
Using PHP &
MySQL
2
Index
●
Conditional Statements
●
Looping
●
Functions
●
Arrays
3
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
4
The if Statement
●
The if statement executes some code if one
condition is true.
Syntax
if (condition) {
code to be executed if condition is true;
}
<?php
$t = 10;
5
The if..else Statement
6
The if..else Statement
<?php
$t = 10;
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
7
The if...elseif....else Statement
8
The if...elseif....else Statement
<?php
$t = 10;
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
9
The switch Statement
<?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!";
}
?>
11
Loops
●
PHP while loops execute a block of code
while the specified condition is true.
●
In PHP, we have the following looping
statements:
●
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
●
12
The while loop
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
} ?> }
13
The do..while loop
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);?>
14
For Loops
15
For each loops
●
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;
}
●
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.
16
For each loops
<?php
$colors = array("red", "green", "blue",
"yellow");
17
Variables Scope in PHP
●
In PHP, variables can be declared
anywhere in the script.
●
The scope of a variable is the part of
the script where the variable can be
referenced/used.
●
PHP has three different variable
scopes:
– local
– global
– static
18
Global Variable
●
A variable declared outside a function has a GLOBAL
SCOPE and can only be accessed outside a function
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
19
Local Variable
●
A variable declared within a function has a LOCAL
SCOPE and can only be accessed within that
function:
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
20
Local Variables
●
You can have local variables with the
same name in different functions,
because local variables are only
recognized by the function in which
they are declared.
21
PHP The global Keyword
●
The global keyword is used to access a global variable
from within a function.
●
To do this, use the global keyword before the variables
(inside the function):
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
?>
22
Static Variables
●
Normally, when a function is
completed/executed, all of its
variables are deleted.
●
However, sometimes we want a
local variable NOT to be deleted. We
need it for a further job.
●
To do this, use the static keyword
when you first declare the variable
23
Static Variables
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
Then, each time the function is called, that variable will still
have the information it contained from the last time the
function was called.
Note: The variable is still local to the function.
24
What is Function in PHP?
●
A function is a block of statements that
can be used repeatedly in a program.
●
A function will not execute immediately
when a page loads.
●
A function will be executed by a call to
the function.
25
Creating User Defined Functions
●
function functionName() {
code to be executed;
}
●
A function name can start with a letter
or underscore (not a number).
●
Function names are NOT case-sensitive.
26
Example
●
<?php
function Msg() {
echo "Hello world!";
}
27
Function with One argument
●
Information can be passed to functions
through arguments.
●
An argument is just like a variable.
●
Arguments are specified after the
function name, inside the parentheses.
You can add as many arguments as you
want, just separate them with a
comma.
28
Example
<?php
function Name($fname) {
echo "$fname <br>";
}
Name("Kohli");
Name("Dhoni");
Name("Chahal");
Name("Rohit");
Name("Hardik");
?>
29
With Two Argument
●
<?php
function Name($fname, $team) {
echo "$fname $year <br>";
}
Name("Dhoni", "India");
Name("Butller", "England");
Name("Warner", "Australia");
?>
30
Call by reference
<?php
function adder(&$str2)
{
$str2 .= 'Call By Reference';
}
$str = 'Hello ';
adder($str);
echo $str;
?>
31
Default Argument
●
<?php
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}
setHeight(150);
setHeight(); // will use the default value of 50
setHeight(125);
setHeight(180);
?>
32
Return Value
●
<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}
33
Arrays in PHP
●
An array is a data structure that
stores one or more similar type of
values in a single value.
●
For example if you want to store 100
numbers then instead of defining
100 variables its easy to define an
array of 100 length.
●
There are three different kind of
arrays and each array value is
accessed using an ID which is called
array index.
34
Array in PHP
●
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.
35
Numeric / Indexed Array
●
Also known as indexed array.
●
These arrays can store numbers,
strings and any object but their
index will be represented by
numbers.
●
By default, the array index starts
from zero.
●
In an indexed or numeric array, the
indexes are automatically assigned
and start with 0, and the values can
be any data type.
36
Numeric / Indexed Array
37
Associative Array
●
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.
●
NOTE: Don't keep associative array inside double
quote while printing, otherwise it would not return
any value.
38
Associative Array
39
Multidimensional Array
●
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.
●
The dimension of an array indicates the
number of indices you need to select an
element.
●
For a two-dimensional array you need
two indices to select an element.
●
For a three-dimensional array you need
three indices to select an element.
40
Multidimensional Array
$contacts = array(
array("name" => "Peter Parker",
"email" => "[email protected]"),
array("name" => "Clark Kent",
"email" => "[email protected]"),
array("name" => "Harry Potter",
"email" => "[email protected]")
);
41
Two dimensional Arrays
●
A two-dimensional array is an array
of arrays (a three-dimensional array
is an array of arrays of arrays).
NAME STOCK SOLD
VOLVO 22 18
BMW 15 13
XUV 5 2
LAND ROVER 17 15
42
Two dimensional Arrays
$cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
array("XUV",5,2),
array("Land Rover",17,15)
);
43
Two dimensional Arrays
<?php
?>
44
Two dimensional Arrays
<?php
for ($row = 0; $row < 4; $row++) {
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
}
?>
45
PHP - Sort Functions For Arrays
●
sort() - sort arrays in ascending order
●
rsort() - sort arrays in descending order
●
asort() - sort associative arrays in
ascending order, according to the value
●
arsort() - sort associative arrays in
descending order, according to the value
●
ksort() - sort associative arrays in
ascending order, according to the key
●
krsort() - sort associative arrays in
descending order, according to the key
46
print_r function
●
The print_r() function is used to print
human-readable information about
a variable.
Syntax
print_r(var_name, return_output)
47
print_r function
48