Enjoy the little things, for one day you may look back and realize they were the big things.

A Small Revision

PHP supports types of looping techniques?

  • A. 2
  • B. 3
  • C. 4
  • D. 5

PHP supports types of looping techniques?

  • A. 2
  • B. 3
  • C. 4
  • D. 5

How many main parameter are used in for loop?

  • A. 2
  • B. 3
  • C. 4
  • D. 1

How many main parameter are used in for loop?

  • A. 2
  • B. 3
  • C. 4
  • D. 1

There are three main parameters to the code, namely the initialization, the test condition and the counter.

do-while loop is an control loop

?

  • A. exit
  • B. exist
  • C. easy
  • D. entry

do-while loop is an control loop

?

  • A. exit
  • B. exist
  • C. easy
  • D. entry

while loop is an _____ control loop ?

  • A. exit
  • B. exist
  • C. easy
  • D. entry

while loop is an _____ control loop ?

  • A. exit
  • B. exist
  • C. easy
  • D. entry

What will be the output of the following PHP code?

<?php

for ($num = 1; $num <= 10; $num += 2) {

echo “$num “;

}

?>

  • A. 1 3 5 7 9
  • B. 1 2 3 4 5
  • C. 9 7 5 3 1
  • D. Error

What will be the output of the following PHP code?

<?php

for ($num = 1; $num <= 10; $num += 2) {

echo “$num “;

}

?>

  • A. 1 3 5 7 9
  • B. 1 2 3 4 5
  • C. 9 7 5 3 1
  • D. Error

What will be the output of the following PHP code?

<?php

$num = 20;

while ($num < 12) {

$num += 2;

echo $num, “\n”;

} ?>

  • A. Error
  • B. No Output
  • C. infinite loop
  • D. Only one garbage value

What will be the output of the following PHP code?

<?php

$num = 20;

while ($num < 12) {

$num += 2;

echo $num, “\n”;

} ?>

  • A. Error
  • B. No Output
  • C. infinite loop
  • D. Only one garbage value

PHP ARRAYS

PHP arrays

  • Arrays are complex variables that allow us to store more than one value or a group of values under a single variable name.

Types of Arrays in PHP

There are three types of arrays that you can create. These are:

  • Indexed array — An array with a numeric key.
  • Associative array — An array where each key has its own specific value.
  • Multidimensional array — An array containing one or more arrays within itself.

Indexed Arrays

  • An indexed or numeric array stores each array element with a numeric index.

<?php

$courses = array(“PHP”, “Laravel”, “Node js”);

echo “I like ” . $courses[0] . “, ” . $courses[1] . ” and ” .

$courses[2];

echo “<br>”;

echo count($courses);

?>

OUTPUT:

I like PHP, Laravel and Node js 3

Loop Through an Indexed Array(for loop)

<?php

$courses = array(“PHP”, “Laravel”, “Node js”);

$courseslength = count($courses);

for($x = 0; $x <$courseslength; $x++) {

echo $courses[$x];

echo “<br>”;

}

?>

OUTPUT: PHP

Laravel Node js

Loop Through an Indexed Array(PHP foreach Loop)

  • The foreach loop is used to iterate over arrays.
  • It is used to loop through each key/value pair in an array.

<?php

$courses = array(“PHP”, “Laravel”, “Node js”);

// Loop through colors array

foreach($courses as $course){

echo $course . “<br>”;

}

?>

OUTPUT:

PHP

Laravel Node js

Associative Array

  • Associative arrays are arrays that use named keys that you assign to them.
  • We can associate name with each array elements in PHP using => symbol.
  • The keys assigned to values can be arbitrary and user defined strings.

Associative Array(contd.)

<?php

$courses = array(“INT220″=>”PHP”, “INT221″=>”Laravel”, “INT222″=>”Node js”);

echo “INT 220 is “.$courses[‘INT220’].”. INT 221 is “.$courses[‘INT221’].”. INT222 is “.$courses[‘INT222’];

?> OUTPUT:

INT 220 is PHP. INT 221 is Laravel. INT222 is Node js

Associative Array(contd.)

<?php

$courses[“INT220”] = “PHP”;

$courses[“INT221”] = “Laravel”;

$courses[“INT222”] = “Node js”;

// Printing array structure print_r($courses);

?>

OUTPUT:

Array ( [INT220] => PHP [INT221] => Laravel [INT222] => Node js )

Loop Through an Associative Array(for each loop)

<?php

$courses =

array(“INT220″=>”PHP”,”INT221″=>”Laravel”,”INT222″=>”Node

js”);

foreach($courses as $course => $value) { echo “Key=”.$course.”,”.”Value=”.$value; echo “<br>”;

}

?>

OUTPUT:

Key=INT220, Value=PHP Key=INT221, Value=Laravel Key=INT222, Value=Node js

Loop Through an Associative Array(for loop)

<?php

$courses = array(‘INT220’=>’PHP’,’INT221’=>’Laravel’,’INT222’=

>’Node js’);

$keys = array_keys($courses);

$values = array_values($courses);

for($x=0; $x<count($courses); $x++) {

echo “Key=”.$keys[$x].’,’.”Value=”.$values[$x]. “<br>”;

}

?>

OUTPUT: Key=INT220,Value=PHP Key=INT221,Value=Laravel Key=INT222,Value=Node js

Do you know?

A lot of people confuse the world wide web and the internet as if they are the same thing, but they are not.

The internet is a huge network of computers all over the world, which are connected together.

The world wide web – or web – is a collection of web pages found on this network of computers.

Multidimensional Arrays

  • The multidimensional array is an array in which each element can also be an array and each element in the sub-array can be an array or further contain array within itself and so on.

Multidimensional Arrays(contd.)

OUTPUT:

<?php

$result = array(

array(“Manoj”,7.8,”pass”),

array(“Aditya”,8.5,”pass”),

array(“Anuj”,4.9,”fail”)

Manoj—-CGPA is: 7.8 and his status is pass

Aditya—-CGPA is: 8.5 and his status is pass

Anuj—-CGPA is: 4.9 and his status is fail

);

echo $result[0][0]. ” CGPA

“.$result[0][2].”<br>”;

is: ” . $result[0][1].” and his status is
echo $result[1][0]. ” CGPA

“.$result[1][2].”<br>”;

is: ” . $result[1][1].” and his status is
echo $result[2][0]. ” CGPA

“.$result[2][2];

is: ” . $result[2][1].” and his status is
?>

Multidimensional Arrays(contd.)

<?php

$result = array(

array(

“name” => “Manoj”,

“cgpa” => 7.8, “status” => “pass”

),

array(

“name” => “Aditya”, “cgpa” => 8.5, “status” => “pass”

OUTPUT:

Manoj—-CGPA is: 7.8 and his status is pass Aditya—-CGPA is: 8.5 and his status is pass Anuj—-CGPA is: 4.9 and his status is fail

),

array(

“name” => “Anuj”,

“cgpa” => 4.9, “status” => “fail”

)

);

echo $result[0][“name”]. ” CGPA is: ” . $result[0][“cgpa”].” and his status is “.$result[0][“status”].”<br>”;

echo $result[1][“name”]. ” CGPA is: ” . $result[1][“cgpa”].” and his status is “.$result[1][“status”].”<br>”;

echo $result[2][“name”]. “—-CGPA is: ” . $result[2][“cgpa”].” and his status is “.$result[2][“status”];

?>

Loop Through an Multidimensional Array(for loop)

<?php

$result = array (

array(“Manoj”,7.8,”pass”),

array(“Aditya”,8.5,”pass”),

array(“Anuj”,4.9,”fail”)

);

for ($row = 0; $row < 3; $row++) { echo “<h4>Row number $row</h4>”; for ($col = 0; $col < 3; $col++) {

echo $result[$row][$col].”<br>”;

}

}

?>

OUTPUT:

Row number 0 Manoj

7.8

Pass

Row number 1 Aditya

8.5

Pass

Row number 2 Anuj

4.9

fail

Loop Through an Multidimensional Array(foreach loop)

<?php

$result = array (

array(“Manoj”,7.8,”pass”),

array(“Aditya”,8.5,”pass”),

array(“Anuj”,4.9,”fail”)

);

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

echo “<h4>Row number $row</h4>”;

foreach ($result[$row] as $resul) {

echo $resul.”<br>”;

}

}

?>

OUTPUT:

Row number 0 Manoj

7.8

Pass

Row number 1 Aditya

8.5

Pass

Row number 2 Anuj

4.9

fail

Loop Through an Multidimensional Array(foreach loop)

<?php

$books =

array(“C++” => array(“name” => “Beginning with C”,”copies” =>8), “PHP” => array(“name” => “Basics of PHP”,”copies” => 10), “Laravel” => array(“name” => “MVC”,”copies” => 3)

);

$keys = array_keys($books);

for($i = 0; $i < count($books); $i++) { echo “<h1>$keys[$i]</h1>”;

foreach($books[$keys[$i]] as $key => $value) {

echo $key . ” = ” . $value . “<br>”;

}

}

?>

C++

name = Beginning with C copies = 8

PHP

name = Basics of PHP copies = 10

Laravel name = MVC copies = 3

Array values are keyed by values (called indexed arrays) or using values (called associative arrays). Of course, these key methods can be combined as well.

  • Float, string
  • Positive number, negative number
  • Even number, string
  • Integer, string

Array values are keyed by values (called indexed arrays) or using values (called associative arrays). Of course, these key methods can be combined as well.

  • Float, string
  • Positive number, negative number
  • Even number, string
  • Integer, string

What will the following script output?

<?php

$array = array (1, 2, 3, 5, 8, 13, 21, 34,

55);

$sum = 0;

for ($i = 0; $i < 5; $i++) {

$sum += $array[$i];

}

echo $sum;

?>

There are three different kind of arrays:

  • Numeric array, String array, Multidimensional array
  • Numeric array, Associative array, Dimensional array
  • Numeric array, Associative array, Multidimensional array
  • Const array, Associative array, Multidimensional array

There are three different kind of arrays:

  • Numeric array, String array, Multidimensional array
  • Numeric array, Associative array, Dimensional array
  • Numeric array, Associative array, Multidimensional array
  • Const array, Associative array, Multidimensional array

 

[pdf_note link=”https://drive.google.com/file/d/1KwfphF9iPTGPzI5MuNdIOxw0on7eUgRZ/view”]