Appendix A. Solutions to Exercises
Exercise Solution
The trick here is to make a second call to date()
, passing in the appropriate characters to display the current date. Here's how you might write the script (including comments where appropriate):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Hello</title> <link rel="stylesheet" type="text/css" href="common.css" /> </head> <body> <h1> <?php // Get the current time in a readable format $currentTime = date( "g:i:s a" ); // Get the current date in a readable format $currentDate = date( "M j, Y" ); // Display greeting, time and date to the visitor echo "Hello, world! The current time is $currentTime on $currentDate"; ?> </h1> </body> </html>
Exercise 1 Solution
Write a PHP script such as this:
<?php $x = 5; $x = $x + 1; $x += 1; $x++; echo $x; ?>
Exercise 2 Solution
Write a PHP script such as this:
<?php $x = 3; $y = 4; echo "Test 1 result: " . ($x == $y) . "<br />"; echo "Test 2 result: " . ($x > $y) . "<br />"; echo "Test 3 result: " . ($x <= $y) . "<br />"; echo "Test 4 result: " . ($x != $y) . "<br />"; ?>
Exercise 1 Solution
You could write this script many ways. The following solution creates a for
loop to count the numbers, then uses the ?
(ternary) operator and a switch
construct to determine if each number is odd, even, or prime.
<!DOCTYPE html PUBLIC "-//W3C//DTD ...
Get Beginning PHP 5.3 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.