0% found this document useful (0 votes)
32 views68 pages

Unit 2-clg

The document discusses PHP control structures and arrays. It describes conditional statements like if, elseif, else and switch statements. It also covers loops like while, do-while, for and foreach loops. The document explains how to create indexed arrays, associative arrays and multidimensional arrays in PHP and how to sort arrays.

Uploaded by

sff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views68 pages

Unit 2-clg

The document discusses PHP control structures and arrays. It describes conditional statements like if, elseif, else and switch statements. It also covers loops like while, do-while, for and foreach loops. The document explains how to create indexed arrays, associative arrays and multidimensional arrays in PHP and how to sort arrays.

Uploaded by

sff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

Unit -2 PHP Scripting

PHP Control Structures

What is a control structure?


Code execution can be grouped into categories as shown below
•Sequential – this one involves executing all the codes in the order in which
they have been written.
•Decision – this one involves making a choice given a number of options. The
code executed depends on the value of the condition.
In PHP, there are two primary types of Control
Structures:
Conditional Statements and
Control Loops.
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
PHP - The if Statement
<?php
$age = 18;

if ($age < 18) {


echo “Minor!";
}
?>
PHP - The if...else Statement
<?php
$t = date("H");

if ($t < "20") {


echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
PHP - The if...elseif...else Statement

We can consider the elseif statement as an extension to the if-


else construct.
If you've got more than two choices to choose from, you can use
the elseif 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!";
}
?>
PHP - The switch Statement
Switch… case is similar to the if then… else control structure.
It only executes a single block of code depending on the value of the condition.
If no condition has been met then the default block of code is executed.
switch(condition){
case value:
//block of code to be executed break;
case value2:
//block of code to be executed break;
default:
//default block code
break; }
•“switch(…){…}” is the control structure block code
•“case value: case…” are the blocks of code to be executed depending on the value of the condition
•“default:” is the block of code to be executed when no value matches with the condition
•<?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
a certain number of times. So, instead of adding several almost equal code-lines in a script,
we can use loops.
Loops are used to execute the same block of code again and again, as long as a certain conditi
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
The PHP while Loop
The while loop executes a block of code as long as the specified condition is true.
Syntax
while (condition is true) {
code to be executed;
}

<?php
$x = 1;

while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
<?php
$x = 0;

while($x <= 100) {


echo "The number is: $x <br>";
$x+=10;
}
?>
PHP do while Loop
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 = 6;

do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
PHP for Loop

The for loop is used when you know in advance how many times the script should run.

Syntax
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}

Parameters:
•init counter: Initialize the loop counter value
•test counter: Evaluated for each loop iteration. If it evaluates to
TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
•increment counter: Increases the loop counter value
PHP foreach Loop

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.
<!DOCTYPE html>
<html>
<body>

<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {


echo "$value <br>";
}
?>

</body>
</html>
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $val) {


echo "$x = $val<br>";
}
?>
PHP Break and Continue
It was used to "jump out" of a switch statement.
The break statement can also be used to jump out of a loop.
This example jumps out of the loop when x is equal to 5:

<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 5) {
break;
}
echo "The number is: $x <br>";
}
?>
PHP Continue
The continue statement breaks one iteration (in the loop),
if a specified condition occurs, and continues with the next iteration in the loop.

This example skips the value of 5:


<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 5) {
continue;
}
echo "The number is: $x <br>";
}
?>
PHP Arrays
An array stores multiple values in one single variable:

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Create an Array in PHP

In PHP, the array() function is used to create an array:


array();

In PHP, there are three types of arrays:


•Indexed arrays - Arrays with a numeric index
•Associative arrays - Arrays with named keys
•Multidimensional arrays - Arrays containing one or more arrays
The count() Function

The count() function is used to return the length (the number of elements) of an array:

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
PHP Indexed Arrays
There are two ways to create indexed arrays:
The index can be assigned automatically (index always starts at 0), like this:
$cars = array("Volvo", "BMW", "Toyota");
or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
<?php

// One way to create an indexed array


$name_one = array("Zack", "Anthony", "Ram", "Salim", "Raghav");

// Accessing the elements directly


echo "Accessing the 1st array elements directly:\n";
echo $name_one[2], "\n";
echo $name_one[0], "\n";
echo $name_one[4], "\n";

?>
// Second way to create an indexed array
$name_two[0] = "ZACK";
$name_two[1] = "ANTHONY";
$name_two[2] = "RAM";
$name_two[3] = "SALIM";
$name_two[4] = "RAGHAV";

// Accessing the elements directly


echo "Accessing the 2nd array elements directly:\n";
echo $name_two[2], "\n";
echo $name_two[0], "\n";
echo $name_two[4], "\n";
PHP Associative Arrays
Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array:
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
or:
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
<?php

// One way to create an associative array


$name_one = array("Zack"=>"Zara", "Anthony"=>"Any",
"Ram"=>"Rani", "Salim"=>"Sara",
"Raghav"=>"Ravina");

?>
// Second way to create an associative array
$name_two["zack"] = "zara";
$name_two["anthony"] = "any";
$name_two["ram"] = "rani";
$name_two["salim"] = "sara";
$name_two["raghav"] = "ravina";

// Accessing the elements directly


echo "Accessing the elements directly:\n";
echo $name_two["zack"], "\n";
echo $name_two["salim"], "\n";
echo $name_two["anthony"], "\n";
echo $name_one["Ram"], "\n";
echo $name_one["Raghav"], "\n";
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
PHP - Multidimensional Arrays
A multidimensional array is an array containing one or more arrays.
PHP supports multidimensional arrays that are two, three, four, five, or more
levels deep. However, arrays more than three levels deep are hard to
manage for most people.
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
$emp = array
(
array(1,"sono",400000),
array(2,"john",500000),
array(3,"rahul",300000)
);
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);

echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";


echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
?>
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);

for ($row = 0; $row < 4; $row++) {


echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++) {
echo "<li>".$cars[$row][$col]."</li>";
}
echo "</ul>";
}
?>
PHP Sorting Arrays

•sort() - sort arrays in ascending order


•rsort() - sort arrays in descending order
•asort() - sort associative arrays in ascending order, according to the value
•ksort() - sort associative arrays in ascending order, according to the key
•arsort() - sort associative arrays in descending order, according to the value
•krsort() - sort associative arrays in descending order, according to the key
Sort Array in Ascending Order - sort()
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
?>
Sort Array in Descending Order - rsort()

The following example sorts the elements of the $numbers array in


descending numerical order:

<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
?>
Sort Array (Ascending Order), According
to Value - asort()
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
?>
Sort Array (Ascending Order),
According to Key - ksort()
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
?>
exit( ) Function

The exit() function only terminates the execution of the script.


The shutdown functions and object destructors will always be
executed even if exit() function is called.
The message to be displayed is passed as a parameter to the
exit() function and it terminates the script and displays the
message.
The exit() function is an alias of the die() function.
<?php
//declaring variables
$a=5;
$b=5.0;

if($a==$b)
{ //terminating script with a message using exit()
exit('variables are equal');
}
else
{
//terminating script with a message using exit()
exit('variables are not equal');
}
?>
die() Function:
In PHP, die() is the same as exit(). A program’s result will be an empty screen.
Use die() when there is an error and have to stop the execution.

Syntax:
Die(message);
Or
Die();
Return keyword
The return keyword ends a function and, optionally,
uses the result of an expression as the return value of the function.
If return is used outside of a function,
it stops PHP code in the file from running. If the file was included using include,
include_once, require or require_once,
the result of the expression is used as the return value of the include statements.
<?php
function add($x) {
return $x + 1;
}

echo “summation is " . add(7);


?>
ER diagram
ER Model stands for Entity Relationship Model is a high-level
conceptual data model diagram. ER model helps to systematically
analyze data requirements to produce a well-designed database.
The ER Model represents real-world entities and the relationships
between them. Creating an ER Model in DBMS is considered as a
best practice before implementing your database.
Following are the main components and
its symbols in ER Diagrams:
•Rectangles: This Entity Relationship Diagram symbol represents entity types
•Ellipses : Symbol represent attributes
•Diamonds: This symbol represents relationship types
•Lines: It links attributes to entity types and entity types with other relationship types
•Primary key: attributes are underlined
•Double Ellipses: Represent multi-valued attributes
Cardinality
Cardinality
Different types of cardinal relationships are:
•One-to-One Relationships
•One-to-Many Relationships
•May to One Relationships
•Many-to-Many Relationships
Step 1) Entity Identification
Step 2) Relationship Identification
Step 3) Cardinality Identification
Step 4) Identify Attributes
1.One-to-one:
2.One-to-many:
3. Many to One
4. Many to Many:
Data Flow Diagrams

A Data Flow Diagram (DFD) is a traditional visual


representation of the information flows within a system. A neat
and clear DFD can depict the right amount of the system
requirement graphically. It can be manual, automated, or a
combination of both.
It shows how data enters and leaves the system, what
changes the information, and where data is stored.
0-level DFDM
1-level DFD
2-Level DFD

You might also like