C.4 Chapter 5
C.4.1 Exercise 1:
function html_img($url, $alt = '', $height = 0, $width = 0) {
print '<img src="' . $url . '"';
if (strlen($alt)) {
print ' alt="' . $alt . '"';
}
if ($height) {
print ' height="' . $height . '"';
}
if ($width) {
print ' width="' . $width . '"';
}
print '>';
}
C.4.2 Exercise 2:
function html_img2($file, $alt = '', $height = 0, $width = 0) {
print '<img src="' . $GLOBALS['image_path'] . $file . '"';
if (strlen($alt)) {
print ' alt="' . $alt . '"';
}
if ($height) {
print ' height="' . $height . '"';
}
if ($width) {
print ' width="' . $width . '"';
}
print '>';
}
C.4.3 Exercise 3:
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)
C.4.4 Exercise 4:
Using sprintf( ) is necessary to ensure that
one-digit hex numbers (like 0) get padded with a leading 0.
function build_color($red, $green, $blue) {
$redhex = dechex($red);
$greenhex = dechex($green);
$bluehex = dechex($blue);
return sprintf('#%02s%02s%02s', $redhex, $greenhex, $bluehex);
}
You can also rely on sprintf( )'s
built-in hex-to-decimal conversion with the %x
format character:
function build_color($red, $green, $blue) {
return sprintf('#%02x%02x%02x', $red, $green, $blue);
}
|