5.6 Exercises
Write a function to print out an HTML
<img> tag. The function should accept a
mandatory argument of the image URL and optional arguments for
alt text, height, and width. Modify the function in the previous exercise so that the filename
only 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
/data/images/, then the src
attribute of the printed <img> tag would be
/data/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 a new server. Just change the global variable — for
example, from /data/images/ to
http://images.example.com/. What does the following code print out? $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";
}
function restaurant_check($meal, $tax, $tip) {
$tax_amount = $meal * ($tax / 100);
$tip_amount = $meal * ($tip / 100);
return $meal + $tax_amount + $tip_amount;
}
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.
|