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

Php

This document is a student book for the PHP course for the first semester of the 2025-2026 academic year. It contains various chapters with code examples and outputs related to geometric shapes, programming exercises, and simulations. Each chapter includes PHP code snippets demonstrating different programming concepts and their outputs.

Uploaded by

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

Php

This document is a student book for the PHP course for the first semester of the 2025-2026 academic year. It contains various chapters with code examples and outputs related to geometric shapes, programming exercises, and simulations. Each chapter includes PHP code snippets demonstrating different programming concepts and their outputs.

Uploaded by

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

PHP

First Semester
2025-2026 Academic year
Student book

Tr Daw Sandar Khaing


Book by Kyaw Hlaing Soe
 Chapter - 3/Page – 49
//output
This circle has...
A radius of 4
A diameter of 8
A circumference of 25.132741228718
An area of 50.265482457437

//code

<?php

$num = 5;
echo "The original value is:" . $num . "<br/>";
$num++;
$num += 1;
$num = $num + 1;
echo "The result is:" . $num;$radius = 4;
$diameter = $radius * 2;
$circumference = M_PI * $diameter;
$area = M_PI * pow($radius, 2);
echo " This circle has... <br/> ";
echo "A radius of " . $radius . " <br/>";
echo "A diameter of " . $diameter . " <br/> ";
echo "A circumference of " . $circumference . " <br/> ";
echo "An area of " . $area . " <br/> ";

?>

 Chapter - 3/ parallelogram
//output

This parallelogram has...


The base of 8
The height of 5
The side of 3
An area of 40
A perimeter of 22

//code

<?php
$b = 8;
$h = 5;
$s = 3;
$area = $b * $h;
$perimeter = 2 * ($s + $b);
echo "This parallelogram has...<br/>";
echo "The base of " . $b . "<br/>";
echo "The height of " . $h . "<br/>";
echo "The side of " . $s . "<br/>";
echo "An area of " . $area . "<br/>";
echo "A perimeter of " . $perimeter . "<br/>";

?>

 Chapter – 3/ Rhombus
//output

This rhombus has...


The Diagonal of 11
The Diagonal of 22
The side of 33
An area of 121
A perimeter of 132

//code

<?php

$p = 11;
$q = 22;
$s = 33;
$area = 1 / 2 * $p * $q;
$perimeter = 4 * $s;
echo "This rhombus has...<br/>";
echo "The Diagonal of " . $p . "<br/>";
echo "The Diagonal of " . $q . "<br/>";
echo "The side of " . $s . "<br/>";
echo "An area of " . $area . "<br/>";
echo "A perimeter of " . $perimeter . "<br/>";

?>

 Chapter - 3/Page – 50/Exercise – 1


//output
The original value is:5
The result is:8

//code
<?php

$num = 5;
echo "The original value is:" . $num . "<br/>";
$num++;
$num += 1;
$num = $num + 1;
echo "The result is:" . $num;

?>

 Chapter – 3/Page – 57
//output

Good afternoon!

Did you know that 2025 is not a leap year?

//code

<?php
$hour = date("G");
$year = date("Y");
if ($hour >= 5 && $hour < 12) {
echo "<h1>Good morning!</h1>";
} elseif ($hour >= 12 && $hour < 18) {
echo "<h1>Good afternoon!</h1>";
} elseif ($hour >= 18 && $hour < 22) {
echo "<h1>Good evening!</h1>";
} else {
echo "<h1>Good night!</h1>";
}
$leapYear = false;
if ((($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400 == 0)) {
$leapYear = true;
}

echo "<p>Did you know that $year is" . ($leapYear ? "" : " not") . " a
leap
year?</p>";
?>

 Chapter – 4/Page – 59

//output

Selling a widget... done. There are 9 widgets left.


Selling a widget... done. There are 8 widgets left.
Selling a widget... done. There are 7 widgets left.
Selling a widget... done. There are 6 widgets left.
Selling a widget... done. There are 5 widgets left.
Selling a widget... done. There are 4 widgets left.
Selling a widget... done. There are 3 widgets left.
Selling a widget... done. There are 2 widgets left.
Selling a widget... done. There are 1 widgets left.
Selling a widget... done. There are 0 widgets left.
We're right out of widgets!

//code

<?php
$widgetsLeft = 10;
while ($widgetsLeft > 0) {
echo "Selling a widget... ";
$widgetsLeft--;
echo "done. There are $widgetsLeft widgets left. <br/> ";
}
echo "We're right out of widgets!";
?>

 Chapter – 4/Page – 66

//output

//code

<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
</head>
<body>

<?php
$mapSize = 10;
// Position the home and the pigeon
do {
$homeX = rand(0, $mapSize - 1);
$homeY = rand(0, $mapSize - 1);
$pigeonX = rand(0, $mapSize - 1);
$pigeonY = rand(0, $mapSize - 1);
} while ((abs($homeX - $pigeonX) < $mapSize / 2) && (abs($homeY -
$pigeonY) <$mapSize / 2));

do {
// Move the pigeon closer to home
if ($pigeonX < $homeX) {
$pigeonX++;
} elseif ($pigeonX > $homeX) {
$pigeonX--;
}

if ($pigeonY < $homeY) {


$pigeonY++;
} elseif ($pigeonY > $homeY) {
$pigeonY--;
}

// Display the current map


echo '<div class="map" style="width: ' . $mapSize . 'em;"><pre>';
for ($y = 0; $y < $mapSize; $y++) {
for ($x = 0; $x < $mapSize; $x++) {
if ($x == $homeX && $y == $homeY) {
echo '<span class="home">+</span>'; // Home
} elseif ($x == $pigeonX && $y == $pigeonY) {
echo '<span class="pigeon">%</span>'; // Pigeon
} else {
echo '<span class="empty">.</span>'; // Empty square
}
echo($x != $mapSize - 1) ? " " : "";
}
echo "\n";
}
echo "</pre></div>\n";
} while ($pigeonX != $homeX || $pigeonY != $homeY);
?>

</body>
</html>
 Chapter – 4/Page – 71

//output

//code

<!DOCTYPE html>
<html lang="en">
<head>
<title>Fibonacci Sequence</title>
<style type="text/css">
th { text-align: left; background-color: #999; }
th, td { padding: 0.4em; }
tr.alt td { background: #ddd; }
</style>
</head>
<body>

<h2>Fibonacci Sequence</h2>
<table cellspacing="0" border="0" style="width: 20em; border: 1px solid #666;">
<tr>
<th>Sequence #</th>
<th>Value</th>
</tr>
<tr>
<td>F<sub>0</sub></td>
<td>0</td>
</tr>
<tr class="alt">
<td>F<sub>1</sub></td>
<td>1</td>
</tr>

<?php
$iterations = 10;
$num1 = 0;
$num2 = 1;
for ($i = 2; $i <= $iterations; $i++) {
$sum = $num1 + $num2;
$num1 = $num2;
$num2 = $sum;
?>
<tr <?php if ($i % 2 != 0) echo 'class="alt"'; ?>>
<td>F<sub><?php echo $i; ?></sub></td>
<td><?php echo $num2; ?></td>
</tr>
<?php
}
?>
</table>

</body>
</html>

 Chapter – 4/ Page – 72/Exercise – 1


//output

//code

<!DOCTYPE html>
<html>
<head>
<title>Number Analysis</title>
<style>
table {
border-collapse: collapse;
width: 50%;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 8px;
text-align: center;
}
</style>
</head>
<body>

<h2>Number Analysis (1 to 10)</h2>

<table>
<tr>
<th>Number</th>
<th>Odd/Even</th>
<th>Prime</th>
</tr>
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i % 2 == 0) {
$odd_even = "Even";
} else {
$odd_even = "Odd";
}

$is_prime = "No";
if ($i > 1) { // 0 and 1 are not prime
$prime = true;
for ($j = 2; $j < $i; $j++) {
if ($i % $j == 0) {
$prime = false;
break;
}
}
if ($prime) {
$is_prime = "Yes";
}
}

echo "<tr>";
echo "<td>$i</td>";
echo "<td>$odd_even</td>";
echo "<td>$is_prime</td>";
echo "</tr>";
}
?>
</table>

</body>
</html>
 Chapter – 4/ Page – 72/Exercise – 2
//output

Homing Pigeon Simulator

Pigeon 1 starts at (0, 0)

Pigeon 2 starts at (10, 10)

Home is at (5, 5)

Simulation Steps:

 Pigeon 1 at (0, 0), Pigeon 2 at (10, 10)


 Pigeon 1 at (1, 1), Pigeon 2 at (9, 9)
 Pigeon 1 at (2, 2), Pigeon 2 at (8, 8)
 Pigeon 1 at (3, 3), Pigeon 2 at (7, 7)
 Pigeon 1 at (4, 4), Pigeon 2 at (6, 6)
 Pigeon 1 at (5, 5), Pigeon 2 at (5, 5)

Both pigeons have arrived at home!

//code

<!DOCTYPE html>
<html>
<head>
<title>Homing Pigeon Simulator</title>
</head>
<body>

<h2>Homing Pigeon Simulator</h2>

<?php

$home_x = 5;
$home_y = 5;

$pigeon1_x = 0;
$pigeon1_y = 0;

$pigeon2_x = 10;
$pigeon2_y = 10;

echo "<p>Pigeon 1 starts at ($pigeon1_x, $pigeon1_y)</p>";


echo "<p>Pigeon 2 starts at ($pigeon2_x, $pigeon2_y)</p>";
echo "<p>Home is at ($home_x, $home_y)</p>";

echo "<h3>Simulation Steps:</h3>";


echo "<ul>";

while (($pigeon1_x != $home_x || $pigeon1_y != $home_y) || ($pigeon2_x != $home_x


|| $pigeon2_y != $home_y)) {
echo "<li>Pigeon 1 at ($pigeon1_x, $pigeon1_y), Pigeon 2 at ($pigeon2_x,
$pigeon2_y)</li>";

if ($pigeon1_x < $home_x) {


$pigeon1_x++;
} elseif ($pigeon1_x > $home_x) {
$pigeon1_x--;
}

if ($pigeon1_y < $home_y) {


$pigeon1_y++;
} elseif ($pigeon1_y > $home_y) {
$pigeon1_y--;
}

if ($pigeon2_x < $home_x) {


$pigeon2_x++;
} elseif ($pigeon2_x > $home_x) {
$pigeon2_x--;
}

if ($pigeon2_y < $home_y) {


$pigeon2_y++;
} elseif ($pigeon2_y > $home_y) {
$pigeon2_y--;
}
}

echo "<li>Pigeon 1 at ($pigeon1_x, $pigeon1_y), Pigeon 2 at ($pigeon2_x,


$pigeon2_y)</li>";
echo "</ul>";

echo "<p>Both pigeons have arrived at home!</p>";


?>

</body>
</html>

 Chapter – 5/Page – 83

//output
//code

<!DOCTYPE html>
<html>
<head>
<title>Justifying Lines of Text</title>
</head>
<body>

<h1>Justifying Lines of Text</h1>

<?php
// The text to justify
$myText = <<<END_TEXT
But think not that this famous town has
only harpooneers, cannibals, and
bumpkins to show her visitors. Not at
all. Still New Bedford is a queer place.
Had it not been for us whalemen, that
tract of land would this day perhaps
have been in as howling condition as the
coast of Labrador.
END_TEXT;

$myText = str_replace("\r\n", "\n", $myText);


$lineLength = 40; // The desired line length
$myTextJustified = "";
$numLines = substr_count($myText, "\n");
$startOfLine = 0;

// Move through each line in turn


for ($i = 0; $i < $numLines; $i++) {
$originalLineLength = strpos($myText, "\n", $startOfLine) - $startOfLine;
$justifiedLine = substr($myText, $startOfLine, $originalLineLength);
$justifiedLineLength = $originalLineLength;

// Keep adding spaces between words until the desired line length is
reached
while ($i < $numLines - 1 && $justifiedLineLength < $lineLength) {
for ($j = 0; $j < $justifiedLineLength; $j++) {
if ($justifiedLineLength < $lineLength && $justifiedLine[$j] == "
") {
$justifiedLine = substr_replace($justifiedLine, " ", $j, 0);
$justifiedLineLength++;
$j++;
}
}
}

// Add the justified line to the string and move to the start of the next
line
$myTextJustified .= "$justifiedLine\n";
$startOfLine += $originalLineLength + 1;
}
?>

<h2>Original text:</h2>
<pre><?php echo $myText; ?></pre>

<h2>Justified text:</h2>
<pre><?php echo $myTextJustified; ?></pre>

</body>
</html>

 Chapter – 5/Page – 90
//output

Type Specifiers in Action


Binary: 1111011
Character: {
Decimal: 123
Scientific: 1.234500e+2
Float: 123.450000
Octal: 173
String: 123.45
Hex (lower case): 7b
Hex (upper case): 7B
//code

<!DOCTYPE html>
<html>
<head>
<title>Type Specifiers in Action</title>
</head>
<body>

<h1>Type Specifiers in Action</h1>

<?php
$myNumber = 123.45;
printf("Binary: %b <br/>", $myNumber);
printf("Character: %c <br/>", $myNumber);
printf("Decimal: %d <br/>", $myNumber);
printf("Scientific: %e <br/>", $myNumber);
printf("Float: %f <br/>", $myNumber);
printf("Octal: %o <br/>", $myNumber);
printf("String: %s <br/>", $myNumber);
printf("Hex (lower case): %x <br/>", $myNumber);
printf("Hex (upper case): %X <br/>", $myNumber);
?>

</body>
</html>

 Chapter – 5/Power Point Slide – 11


//code
<!DOCTYPE html>
<html>
<head>
<title>Locating Text with strpos() and strrpos()</title>
</head>
<body>

<h4>Locating Text with strpos() and strrpos()</h4>

<?php
// Define a string variable
$myString = "Hello, world!";

// Find the position of a substring


echo strpos($myString, "wor") . "<br/>"; // Displays '7'
echo strpos($myString, "xyz") . "<br/>"; // Displays '' (false, no output)

// Example with an offset parameter


echo strpos($myString, "o") . "<br/>"; // Displays '4'
echo strpos($myString, "o", 5) . "<br/>"; // Displays '8'

// Find the last occurrence of a substring


echo strrpos($myString, "o") . "<br/>"; // Displays '8'
?>

</body>
</html>

 Chapter – 5/Power Point Slide – 18


//code
<!DOCTYPE html>
<html>
<head>
<title>Dealing with Upper- and Lowercase</title>
</head>
<body>

<h4>Dealing with Upper- and Lowercase</h4>

<?php

$myString = "Hello, world!";

// Convert to lowercase
echo strtolower($myString) . "<br/>"; // Displays 'hello, world!'

// Convert to uppercase
$myString = "Hello, world!";
echo strtoupper($myString) . "<br/>"; // Displays 'HELLO, WORLD!'

// Capitalize the first letter of the string


$myString = "hello, world!";
echo ucfirst($myString) . "<br/>"; // Displays 'Hello, world!'

// Convert the first letter to lowercase


$myString = "Hello, World!";
echo lcfirst($myString) . "<br/>"; // Displays 'hello, World!'

// Capitalize the first letter of each word


$myString = "hello, world!";
echo ucwords($myString) . "<br/>"; // Displays 'Hello, World!'
?>

</body>
</html>

 Chapter – 5/Power Point Slide – 20


//output
<!DOCTYPE html>
<html>
<head>
<title>Formatting with printf() and sprintf()</title>
</head>
<body>

<h4>Formatting with printf() and sprintf()</h4>

<?php
$myNumber = 123.45;

printf("Binary: %b <br/>", $myNumber); // Converts to binary (integer part


only)
printf("Character: %c <br/>", $myNumber); // Converts to ASCII
character(integer part)
printf("Decimal: %d <br/>", $myNumber); // Decimal number (integer part
only)
printf("Scientific: %e <br/>", $myNumber); // Scientific notation
printf("Float: %f <br/>", $myNumber); // Floating-point number
printf("Octal: %o <br/>", $myNumber); // Octal number (integer part)
printf("String: %s <br/>", $myNumber); // String format
printf("Hex (lower case): %x <br/>", $myNumber); // Hexadecimal (lowercase)
printf("Hex (upper case): %X <br/>", $myNumber); // Hexadecimal (uppercase)
?>

</body>
</html>

 Chapter – 5/Power Point Slide – 21


//code
<!DOCTYPE html>
<html>
<head>
<title>Padding the Output</title>
</head>
<body>
<h4>Padding the Output</h4>

<?php

printf("%06d <br/>", 123); // Displays "000123"


printf("%06d <br/>", 4567); // Displays "004567"
printf("%06d <br/>", 123456); // Displays "123456"
printf("%06d <br/>", 12345678); // Displays "12345678", no truncation
printf("%15s<br/>", "Hi"); // Displays " Hi"
printf("%15s<br/>", "Hello"); // Displays " Hello"
printf("%15s<br/>", "Hello, world!"); // Displays " Hello, world!"
?>
</body>
</html>

 Chapter – 5/Power Point Slide – 22


//code
<!DOCTYPE html>
<html>
<head>
<title>Padding the Output (Cont)</title>
</head>
<body>

<h4>Padding the Output (Cont)</h4>

<?php

printf("%'#8s<br/>", "Hi"); // Displays "######Hi" (pads with # on the left)


printf("%'#-8s<br/>", "Hi"); // Displays "Hi######" (pads with # on the right)

// Padding strings with str_pad()


echo str_pad("Hello, world!", 20) . "<br/>"; // Displays "Hello, world! "
(pads with spaces)

// Padding with a specific character


echo str_pad("Hello, world!", 20, "*") . "<br/>"; // Displays "Hello, world!
*******"

// Padding with a repeating string


echo str_pad("Hello, world!", 20, "123") . "<br/>"; // Displays "Hello, world!
1231231"
?>

</body>
</html>

 Chapter – 5/Power Point Slide – 23


//code

<!DOCTYPE html>
<html>
<head>
<title>Specifying Number Precision</title>
</head>
<body>

<h4>Specifying Number Precision</h4>


<?php

printf("%f <br/>", 123.4567); // Displays "123.456700"


printf("%.2f <br/>", 123.4567); // Displays "123.46"
printf("%.0f <br/>", 123.4567); // Displays "123"
printf("%.10f <br/>", 123.4567); // Displays "123.4567000000"
printf("%012.2f <br/>", 123.4567); // Displays "000000123.46"
printf("%12.4f <br/>", 123.4567); // Displays " 123.4567"
printf("%.8s<br/>", "Hello, world!"); // Displays "Hello, w"
?>

</body>
</html>

 Chapter -5//Exercise – 1
//code
<?php
$month = 7;
$day = 4;
$year = 2025;

printf("%02d/%02d/%04d", $month, $day, $year); // Displays "07/04/2025"


?>

 Chapter 6/Page – 106


//output

//code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Outputting Arrays with print_r()</title>
</head>
<body>
<h1>Outputting Arrays with print_r()</h1>

<?php
$authors = array("Steinbeck", "Kafka", "Tolkien", "Dickens");
$myBook = array(
"title" => "The Grapes of Wrath",
"author" => "John Steinbeck",
"pubYear" => 1939
);

echo '<h2>$authors:</h2><pre>';
print_r($authors);
echo '</pre><h2>$myBook:</h2><pre>';
print_r($myBook);
echo '</pre>';
?>

</body>
</html>

 Chapter – 6/Page – 112


//output

//code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Using each() with a while loop</title>
</head>
<body>
<h1>Using foreach with a while loop</h1>
<dl>
<?php
$myBook = [
"title" => "The Grapes of Wrath",
"author" => "John Steinbeck",
"pubYear" => 1939,
];
foreach ($myBook as $key => $value) {
echo "<dt>$key</dt>";
echo "<dd>$value</dd>";
}
?>
</dl>
</body>
</html>

 Chapter – 6/Page – 116


//output

//code

<!DOCTYPE html>
<html>
<head>
<title>Altering Array Values with foreach </title>
</head>
<body>

<h1>Altering Array Values with foreach </h1>

<?php
$authors = ["Steinbeck", "Kafka", "Tolkien", "Dickens"];

foreach ($authors as &$val) {


if ($val == "Tolkien") {
$val = "Hardy";
}

echo $val . " ";


}

unset($val);

echo "<br/>";
print_r($authors);
?>
</body>
</html>

 Chapter – 6/Page – 119


//output

//code
<!DOCTYPE html>
<html>
<head>
<title>Looping Through a Two-Dimensional Array</title>
</head>
<body>

<h1>Looping Through a Two-Dimensional Array</h1>

<?php
$myBooks = [
[
"title" => "The Grapes of Wrath",
"author" => "John Steinbeck",
"pubYear" => 1939,
],
[
"title" => "The Trial",
"author" => "Franz Kafka",
"pubYear" => 1925,
],
[
"title" => "The Hobbit",
"author" => "J. R. R. Tolkien",
"pubYear" => 1937,
],
[
"title" => "A Tale of Two Cities",
"author" => "Charles Dickens",
"pubYear" => 1859,
],
];

$bookNum = 0;
foreach ($myBooks as $book) {
$bookNum++;
echo "<h2>Book #$bookNum:</h2>";
echo "<dl>";
foreach ($book as $key => $value) {
echo "<dt>$key</dt><dd>$value</dd>";
}
echo "</dl>";
}
?>

</body>
</html>

 Chapter – 6/Exercise – 1
//output

Books and Authors


 The Hobbit by Tolkien (Published: 1937)
 The Grapes of Wrath by Steinbeck (Published: 1939)
 A Tale of Two Cities by Dickens (Published: 1859)
 Paradise Lost by Milton (Published: 1667)
 Animal Farm by Orwell (Published: 1945)
 The Trial by Kafka (Published: 1925)

//code
<?php
$authors = array( "Steinbeck", "Kafka", "Tolkien", "Dickens", "Milton",
"Orwell" );
$books = array(
array(
"title" => "The Hobbit",
"authorId" => 2,
"pubYear" => 1937
),
array(
"title" => "The Grapes of Wrath",
"authorId" => 0,
"pubYear" => 1939
),
array(
"title" => "A Tale of Two Cities",
"authorId" => 3,
"pubYear" => 1859
),
array(
"title" => "Paradise Lost",
"authorId" => 4,
"pubYear" => 1667
),
array(
"title" => "Animal Farm",
"authorId" => 5,
"pubYear" => 1945
),
array(
"title" => "The Trial",
"authorId" => 1,
"pubYear" => 1925
),
);

// Loop $books array and add the authorName element


foreach ($books as $key => $book) {
// Use the authorId to fetch
$books[$key]["authorName"] = $authors[$book["authorId"]];
}

// Display
echo "<h1>Books and Authors</h1>";
echo "<ul>";
foreach ($books as $book) {
echo "<li><strong>" . $book['title'] . "</strong> by " . $book['authorName'] .
" (Published: " . $book['pubYear'] . ")</li>";
}
echo "</ul>";
?>

 Chapter – 6/Power Point Slide -5


//output

MaMa and KoKo are MgMg 's neighbours


//code
<?php
$names[0] = "MgMg";
$names[1] = "MaMa";
$names[2] = "KoKo";
echo $names[1] . " and " . $names[2] . " are " . $names[0] . " 's
neighbours";
?>

 Chapter – 6/Power Point Slide -11


//output

Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43

//code

<html>
<body>
<?php

$age = ["Peter" => 35, "Ben" => 37, "Joe" => 43];

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


echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
</body>
</html>

 Chapter – 6/Power Point Slide – 14


//output

Array ( [0] => a [1] => b [2] => c )


Array ( [0] => a [1] => b [2] => c [3] => d )

//code

<?php

$arraya = ['a', 'b', 'c'];

print_r($arraya);
echo("<br>");
foreach ($arraya as $key => $value) {
if ($value == 'b') {
$d = 'd';
array_push($arraya, $d);
print_r($arraya);
}
}

 Chapter – 6/Power Point Slide – 15


//output

Element 1
Element 2
Element 3
Element 2
Element 3

//code

<?php

$my_array = ["Element 1", "Element 2", "Element 3"];


foreach ($my_array as $name) {
echo "$name<br>";
}
unset($my_array[0]);
foreach ($my_array as $name)
{
echo "$name<br>";
}

 Chapter – 6/Power Point Slide – 16


//output

red
green

pink
yellow

//code

<?php

$colors = ["red", "green", "blue", "pink", "yellow"];


$colors[2] = "";
//unset($colors[3]);
foreach ($colors as $res) {
echo($res . "<br>");
}

 Chapter – 6/Power Point Slide – 19


//output

apple
banana
lemon
orange

//code

<?php

$fruits = ["lemon", "orange", "banana", "apple"];

sort($fruits);

foreach ($fruits as $key => $val) {


echo $val . "<br>";
}

 Chapter – 6/Power Point Slide – 20


//output

Austin
Sacramento
Salem

//code

<?php

$capitals['CA'] = "Sacramento";
$capitals['TX'] = "Austin";
$capitals['OR'] = "Salem";
sort($capitals);
foreach ($capitals as $ans => $value) {
echo $value . "<br>";
}

 Chapter – 6/Power Point Slide – 21


//output

BMW
Toyota
Volvo

//code

<?php

$cars = ["Volvo", "BMW", "Toyota"];


sort($cars);
$clength = count($cars);
for ($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo "<br>";
}

 Chapter – 6/Power Point Slide – 22


//output

2
4
6
11
22

//code

<?php

$numbers = [4, 6, 2, 22, 11];


sort($numbers);
$arrlength = count($numbers);
$arrlength = count($numbers);
for ($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}

 Chapter – 7/Page – 150


//output

Understanding Variable Scope


Hello, world!
The value of $hello is: 'undefined'
The value of $world is: 'undefined'

//code

<html>
<head>
<title>Understanding Variable Scope</title>

</head>
<body>
<h1>Understanding Variable Scope</h1>

<?php
function helloWithVariables()
{
$hello = "Hello, ";
$world = "world!";
return $hello . $world;
}

// Displaying the result


echo helloWithVariables() . "<br />";

echo "The value of \$hello is: 'undefined' <br />";


echo "The value of \$world is: 'undefined' <br />";
?>

</body>
</html>

 Chapter – 7/Page – 146


//output
Saying hello with style
Hello, world!

Hello, world!
Hello, world!

//code

<html>
<head>
<title>Saying hello with style</title>
</head>
<body>
<h1>Saying hello with style</h1>

<?php
function helloWithStyle($font, $size)
{
echo "<p style=\"font-family: $font; font-size: {$size}em;\">Hello,
world!</p>";
}

helloWithStyle("Helvetica", 2);
helloWithStyle("Times", 3);
helloWithStyle("Courier", 1.5);
?>

</body>
</html>

 Chapter – 7/Page – 155


//output

Sorting words in a block of text by length


The text:
But think not that this famous town has only harpooneers, cannibals, and bumpkins to show her
visitors. Not at all. Still New Bedford is a queer place. Had it not been for us whalemen, that
tract of land would this day perhaps have been in as howling condition as the coast of Labrador.
The sorted words:
a to at is it us of in as But not has and her Not all New Had for day the that this town only show
been land have think Still queer place tract would coast famous Bedford perhaps howling
bumpkins visitors whalemen Labrador cannibals condition harpooneers

//code

<html >
<head>
<title>Sorting words in a block of text by length</title>
</head>
<body>
<h1>Sorting words in a block of text by length</h1>

<?php
$myText = <<<END_TEXT
But think not that this famous town has
only harpooneers, cannibals, and
bumpkins to show her visitors. Not at
all. Still New Bedford is a queer place.
Had it not been for us whalemen, that
tract of land would this day perhaps
have been in as howling condition as the
coast of Labrador.
END_TEXT;

echo "<h2>The text:</h2>";


echo "<div style=\"width: 30em;\">$myText</div>";

$myText = preg_replace("/[,.]/", "", $myText);

$words = array_unique(preg_split("/[\s\n\r\t]+/", $myText));

// Sort words by length using anonymous function


usort($words, function ($a, $b) {
return strlen($a) - strlen($b);
});

echo "<h2>The sorted words:</h2>";


echo "<div style=\"width: 30em;\">";

foreach ($words as $word) {


echo "$word ";
}

echo "</div>";
?>
</body>
</html>
 Chapter – 7/Page – 161
//output

//code

<html>
<head>
<title>Fibonacci sequence using recursion</title>
<link rel="stylesheet" type="text/css" href="common.css" />
<style type="text/css">
th { text-align: left; background-color: #999; }
th, td { padding: 0.4em; }
tr.alt td { background: #ddd; }
</style>
</head>
<body>
<h2>Fibonacci sequence using recursion</h2>
<table cellspacing="0" border="0" style="width: 20em; border: 1px solid
#666;">
<tr>
<th>Sequence #</th>
<th>Value</th>
</tr>

<?php
$iterations = 10;

function fibonacci($n)
{
if ($n == 0 || $n == 1) {
return $n;
}
return fibonacci($n - 2) + fibonacci($n - 1);
}

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


?>
<tr<?php if ($i % 2 != 0) {
echo ' class="alt"';
}
?>>
<td>F<sub><?php echo $i; ?></sub></td>
<td><?php echo fibonacci($i); ?></td>
</tr>
<?php
}
?>

</table>
</body>
</html>

 Chapter – 8/Page – 177


//output

A Simple Car Simulator


I'm driving a red Volkswagen Beetle.

Stepping on the gas...


Current speed: 10 mph
Current speed: 20 mph
Current speed: 30 mph
Current speed: 40 mph
Current speed: 50 mph
Current speed: 60 mph
Current speed: 70 mph
Current speed: 80 mph
Current speed: 90 mph
Current speed: 100 mph

Top speed! Slowing down...


Current speed: 90 mph
Current speed: 80 mph
Current speed: 70 mph
Current speed: 60 mph
Current speed: 50 mph
Current speed: 40 mph
Current speed: 30 mph
Current speed: 20 mph
Current speed: 10 mph
Current speed: 0 mph

Stopped!

//code

<html>
<head>
<title>A Simple Car Simulator</title>
<link rel="stylesheet" type="text/css" href="common.css" />
</head>
<body>
<h1>A Simple Car Simulator</h1>

<?php
class Car {
public $color;
public $manufacturer;
public $model;
private $_speed = 0;

public function accelerate() {


if ($this->_speed >= 100) return false;
$this->_speed += 10;
return true;
}

public function brake() {


if ($this->_speed <= 0) return false;
$this->_speed -= 10;
return true;
}

public function getSpeed() {


return $this->_speed;
}
}

// Create a new Car object


$myCar = new Car();
$myCar->color = "red";
$myCar->manufacturer = "Volkswagen";
$myCar->model = "Beetle";

// Display car details


echo "<p>I'm driving a " . $myCar->color . " " . $myCar->manufacturer . " " .
$myCar->model . ".</p>";
// Accelerating the car
echo "<p>Stepping on the gas...<br />";
while ($myCar->accelerate()) {
echo "Current speed: " . $myCar->getSpeed() . " mph<br />";
}
echo "</p><p>Top speed! Slowing down...<br />";

// Braking the car


while ($myCar->brake()) {
echo "Current speed: " . $myCar->getSpeed() . " mph<br />";
}
echo "</p><p>Stopped!</p>";
?>
</body>
</html>

You might also like