C.1 Chapter 2
C.1.1 Exercise 1:
The
opening PHP tag should be
<?php. There should not be a space between
<? and php. The string 'I'm fine' should either be enclosed in
double quotes ("I'm fine") or
the apostrophe should be escaped ('I\'m fine'). The closing PHP tag should be ?>, not
??>.
C.1.2 Exercise 2:
$hamburger = 4.95;
$milkshake = 1.95;
$cola = .85;
$food = 2 * $hamburger + $milkshake + $cola;
$tax = $food * .075;
$tip = $food * .16;
$total = $food + $tax + $tip;
print "Total cost of the meal is \$$total";
C.1.3 Exercise 3:
$hamburger = 4.95;
$milkshake = 1.95;
$cola = .85;
$food = 2 * $hamburger + $milkshake + $cola;
$tax = $food * .075;
$tip = $food * .16;
printf("%1d %9s at \$%.2f each: \$%.2f\n", 2, 'Hamburger', $hamburger, 2 * $hamburger);
printf("%1d %9s at \$%.2f each: \$%.2f\n", 1, 'Milkshake', $milkshake, $milkshake);
printf("%1d %9s at \$%.2f each: \$%.2f\n", 1, 'Cola', $cola, $cola);
printf("%25s: \$%.2f\n", 'Food and Drink Total', $food);
printf("%25s: \$%.2f\n", 'Total with Tax', $food + $tax);
printf("%25s: \$%.2f\n", 'Total with Tax and Tip', $food + $tax + $tip);
C.1.4 Exercise 4:
$first_name = 'James';
$last_name = 'McCawley';
$full_name = "$first_name $last_name";
print $full_name;
print strlen($full_name);
C.1.5 Exercise 5:
$i = 1; $j = 2;
print "$i $j";
$i++; $j *= 2;
print "$i $j";
$i++; $j *= 2;
print "$i $j";
$i++; $j *= 2;
print "$i $j";
$i++; $j *= 2;
print "$i $j";
|