The following course text material is Copyright KMR Enterprises All Rights Reserved. You are welcome to download and copy the course text material and use for your own personal study/use. You are not permitted to post, publish and/or redistribute the course text in any other format, online or offline without express written permission from KMR Enterprises. You are welcome to and encouraged to share your own course to-do assignments online (your blog, website, etc) and link back to the relevant course material here at Webmaster Cookbook. Thank you and happy coding to you!

Lesson #2b: Working with Date & Time and using conditionals

Switch / Case

Instead of using a bunch of if elseif and else clauses for a single item comparison, it would be less code to use a switch / case statement. The syntax for this goes as follows:

switch($today)
{
  case "day_of_the_week":
  // it is true so do this
  break; // escape the switch since we had a true result
  default:
     // when nothing is true do this
}

Now can you rewrite the code above using switch case instead of if elseif else? Try it on your own :)

Escaping PHP to output HTML

One thing about php that is nice that inside a loop you can escape to HTML. This is very useful if you don't have to interpret any code. Let's look at the code for this text in HTML:

<p><strong><small><font face="Arial">Escaping to output HTML< /font></small></strong></p>

<p><small><font face="Arial">One thing about php that is nice that inside a loop you can

escape to HTML. This is very useful if you don't have to interpret any code. Let's look at

the code for this text in HTML:</font></small></p>

Now let's say we only wanted this to print this code if it was Saturday? We could do this using the following code:

<?php
$today = date("l");
if($today = "Saturday")  { // begin loop, everything below will ONLY show if the day of the week is Saturday
?>

<p><strong><small><font face="Arial">Escaping to output HTML< /font></small></strong></p>

<p><small><font face="Arial">One thing about php that is nice that inside a loop you can

escape to HTML. This is very useful if you don't have to interpret any code. Let's look at

the code for this text in HTML:</font></small></p>

<?php
} // end loop
?>

You might be wondering, what if you have to insert a variable inside the HTML, can you still escape to HTML? Sure. I'd just enclose the variable as follows inside the HTML like this:

<?php echo($variablename) ?>

Front Page or other HTML editor users should use the following so your code doesn't get munged:

<script language="php">echo($variablename);</script>

This is a technique you should employ when outputting large chunks of HTML inside a conditional loop.

TO-DO ASSIGNMENT #2: Write a script to print to the browser a different URL each day of the week using the switch/case statement and comparing to the numeric day of the week (0-6), not the textual (Sunday-Saturday) day of the week. Use only one print() function to do this and make these urls active hyperlinks with the url title hyperlinked and the url description not hyperlinked. Employ the use of concatenation to create the single print function instead of enclosing in interpreted double quotes to demonstrate your understanding of concatenating strings.

This concludes lesson #2 of 16 in the PHP 101 scripting course. Next lesson - 3A | Home

Copyright 2000-2008 KMR Enterprises All Rights Reserved