Php
Php
First Semester
2025-2026 Academic year
Student book
//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
//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
//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/>";
?>
//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!
//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
//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--;
}
</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>
//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>
<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
Home is at (5, 5)
Simulation Steps:
//code
<!DOCTYPE html>
<html>
<head>
<title>Homing Pigeon Simulator</title>
</head>
<body>
<?php
$home_x = 5;
$home_y = 5;
$pigeon1_x = 0;
$pigeon1_y = 0;
$pigeon2_x = 10;
$pigeon2_y = 10;
</body>
</html>
Chapter – 5/Page – 83
//output
//code
<!DOCTYPE html>
<html>
<head>
<title>Justifying Lines of Text</title>
</head>
<body>
<?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;
// 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
<!DOCTYPE html>
<html>
<head>
<title>Type Specifiers in Action</title>
</head>
<body>
<?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>
<?php
// Define a string variable
$myString = "Hello, world!";
</body>
</html>
<?php
// Convert to lowercase
echo strtolower($myString) . "<br/>"; // Displays 'hello, world!'
// Convert to uppercase
$myString = "Hello, world!";
echo strtoupper($myString) . "<br/>"; // Displays 'HELLO, WORLD!'
</body>
</html>
<?php
$myNumber = 123.45;
</body>
</html>
<?php
<?php
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Specifying Number Precision</title>
</head>
<body>
</body>
</html>
Chapter -5//Exercise – 1
//code
<?php
$month = 7;
$day = 4;
$year = 2025;
//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>
//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>
//code
<!DOCTYPE html>
<html>
<head>
<title>Altering Array Values with foreach </title>
</head>
<body>
<?php
$authors = ["Steinbeck", "Kafka", "Tolkien", "Dickens"];
unset($val);
echo "<br/>";
print_r($authors);
?>
</body>
</html>
//code
<!DOCTYPE html>
<html>
<head>
<title>Looping Through a Two-Dimensional Array</title>
</head>
<body>
<?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
//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
),
);
// 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>";
?>
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43
//code
<html>
<body>
<?php
$age = ["Peter" => 35, "Ben" => 37, "Joe" => 43];
//code
<?php
print_r($arraya);
echo("<br>");
foreach ($arraya as $key => $value) {
if ($value == 'b') {
$d = 'd';
array_push($arraya, $d);
print_r($arraya);
}
}
Element 1
Element 2
Element 3
Element 2
Element 3
//code
<?php
red
green
pink
yellow
//code
<?php
apple
banana
lemon
orange
//code
<?php
sort($fruits);
Austin
Sacramento
Salem
//code
<?php
$capitals['CA'] = "Sacramento";
$capitals['TX'] = "Austin";
$capitals['OR'] = "Salem";
sort($capitals);
foreach ($capitals as $ans => $value) {
echo $value . "<br>";
}
BMW
Toyota
Volvo
//code
<?php
2
4
6
11
22
//code
<?php
//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;
}
</body>
</html>
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>
//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 "</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);
}
</table>
</body>
</html>
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;