Created
January 29, 2025 02:56
-
-
Save Mwamitovi/3fd6e6aa31927efeb52e70e27a246566 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Chapter-5 | |
// Question-1 | |
// 1. Write a function to return an HTML <img /> tag. | |
// The function should accept a mandatory argument of the image URL and optional arguments for alt text, height, and width. | |
// Answer-1 | |
function html_img($url, $alt = null, $height = null, $width = null) | |
{ | |
$html = '<img src="' . $url . '"'; | |
if (isset($alt)) { | |
$html .= ' alt="' . $alt . '"'; | |
} | |
if (isset($height)) { | |
$html .= ' height="' . $height . '"'; | |
} | |
if (isset($width)) { | |
$html .= ' width="' . $width . '"'; | |
} | |
$html .= '/>'; | |
return $html; | |
} | |
// Question-2 | |
// 2. Modify the function in the previous exercise so that only the filename is passed to the function in the URL argument. | |
// Inside the function, prepend a global variable to the filename to make the full URL. | |
// For example, if you pass photo.png to the function, and the global variable contains /images/, | |
// then the src attribute of the returned <img> tag would be /images/photo.png. | |
// A function like this is an easy way to keep your image tags correct, even if the images move to a new path or server. | |
// Just change the global variable—for example, from /images/ to http://images.example.com/. | |
// Answer-2 | |
function html_img2($file, $alt = null, $height = null, $width = null) | |
{ | |
if (isset($GLOBALS['image_path'])) { | |
$file = $GLOBALS['image_path'] . $file; | |
} | |
$html = '<img src="' . $file . '"'; | |
if (isset($alt)) { | |
$html .= ' alt="' . $alt . '"'; | |
} | |
if (isset($height)) { | |
$html .= ' height="' . $height . '"'; | |
} | |
if (isset($width)) { | |
$html .= ' width="' . $width . '"'; | |
} | |
$html .= '/>'; | |
return $html; | |
} | |
// Question-3 | |
// 3. Put your function from the previous exercise in one file. | |
// Then make another file that loads the first file and uses it to print out some <img /> tags. | |
// Answer-3 | |
// The html_img2() function from the previous exercise is saved in this file | |
include "html-img2.php"; | |
$image_path = '/images/'; | |
print html_img2('puppy.png'); | |
print html_img2('kitten.png', 'fuzzy'); | |
print html_img2('dragon.png', null, 640, 480); | |
// Question-4 | |
// 4. What does the following code print out? ?> | |
<?php | |
function restaurant_check($meal, $tax, $tip) | |
{ | |
$tax_amount = $meal * ($tax / 100); | |
$tip_amount = $meal * ($tip / 100); | |
return $meal + $tax_amount + $tip_amount; | |
} | |
$cash_on_hand = 31; | |
$meal = 25; | |
$tax = 10; | |
$tip = 10; | |
while (($cost = restaurant_check($meal, $tax, $tip)) < $cash_on_hand) { | |
$tip++; | |
print "I can afford a tip of $tip% ($cost)\n"; | |
} | |
?><?php | |
// Answer-4 | |
// I can afford a tip of 11% (30) | |
// I can afford a tip of 12% (30.25) | |
// I can afford a tip of 13% (30.5) | |
// I can afford a tip of 14% (30.75) | |
// Question-5 | |
// 5. Web colors such as #ffffff and #cc3399 are made by concatenating the hexadecimal color values for red, green, and blue. | |
// Write a function that accepts decimal red, green, and blue arguments and returns a string containing | |
// the appropriate color for use in a web page. For example, if the arguments are 255, 0, and 255, | |
// then the returned string should be #ff00ff. You may find it helpful to use the built-in function dechex(), | |
// which is documented at http://www.php.net/dechex. | |
// Answer-5 | |
/* Using dechex(): */ | |
function web_color1($red, $green, $blue) | |
{ | |
$hex = [dechex($red), dechex($green), dechex($blue)]; | |
// Prepend a leading 0 if necessary to 1-digit hex values | |
foreach ($hex as $i => $val) { | |
if (strlen($i) == 1) { | |
$hex[$i] = "0$val"; | |
} | |
} | |
return '#' . implode( | |
'' | |
, | |
$hex | |
); | |
} | |
/* You can also rely on sprintf()'s %x format character to do hex-to-decimal conversion: */ | |
function web_color2($red, $green, $blue) | |
{ | |
return sprintf('#%02x%02x%02x', $red, $green, $blue); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment