--- Hayden's Harness Attachment <[EMAIL PROTECTED]> wrote:

> From the web site:
> 
> http://php.net/manual/en/control-structures.switch.php
> 
> I came up with the code:
> 
> <?PHP
> if ((!$sitestyle) || $sitestyle == 'layout' ) {
> echo '<a id="switch" href="../switcher.php?set=layout_large">Increase Font
> Size</a>';
> echo '<a id="switch" href="../switcher.php?set=layout_small">Decrease Font
> Size</a>';
> echo '<a id="switch" href="../switcher.php?set=layout_medium">Default Font
> Size</a>';
> }
> 
> switch ((!$sitestyle) || $sitestyle == 'layout' ):
> case layout_medium:
> echo '<a id="switch" href="../switcher.php?set=layout_medium">Default Font
> Size</a>';
> break;
> case "layout_large":
> echo '<a id="switch" href="../switcher.php?set=layout_large">Increase Font
> Size</a>';
> break;
> case "layout_small":
> echo '<a id="switch" href="../switcher.php?set=layout_small">Decrease Font
> Size</a>';
> break;
> default:
> echo '<a id="switch" href="../switcher.php?set=layout_medium">Default Font
> Size</a>';
> endswitch;
> ?>
> 
> Is the above proper code?
> 
> I have a main CSS file and a small css file (layout_medium.css as a default
> file. A file called "Switcher.php" which establishes a cookie with
> Layout_medium, Lyout_larg and Layout_small. The web pages Defaults to
> layout_medium and has two buttons in the header;
> 
> Button1 - Increase Font Size - layout_large.css, and
> 
> button 2 - Decrease Font Size - layout_small.css.
> 
> Angus MacKinnon


The switch() statement may be written as you have it but is clearer like this:

switch()
{
 case val1:
   code
   code
   break;
 case val2:
   code
   code
   break;
 default:
   code
   code
}

If the indents came through to your email, note how they improve the
readability.  Most PHP control structures can use curly braces {} so it also
helps to use them.

The value in the parens of switch() is what is compared with each of the static
values in the case statements.  In your example you have:

  (!$sitestyle) || $sitestyle == 'layout'

Since you have an OR condition the entire expression will be true or false. 
This may be fine in an if statement or a while statement but will not work the
way you want.

I don't see where $sitestyle is defined so I don't know what values are
expected.  It appears that perhaps you are trying to use the value of the GET
variable "set" which contains values like "layout_large", "layout_small", or
"layout_medium" according to your links.

With installations of PHP from the last several years, register_globals are off
so you will need to obtain the value from the $_GET superglobal before you use
it:

$set = $_GET['set'];

if ((!$sitestyle) || $sitestyle == 'layout' )
{
 switch ($set)
 {
  case "layout_medium":
   echo '<a id="switch" href="../switcher.php?set=layout_medium">Default Font
Size</a>';
   break;

  case "layout_large":
   echo '<a id="switch" href="../switcher.php?set=layout_large">Increase Font
Size</a>';
   break;

  case "layout_small":
   echo '<a id="switch" href="../switcher.php?set=layout_small">Decrease Font
Size</a>';
   break;

  default:
   echo '<a id="switch" href="../switcher.php?set=layout_medium">Default Font 
Size</a>';
 }
}


Something else I spotted as I was reformatting your code was that your first
case statement did not have quotes around the value.  This would cause a
problem as well because PHP might try to compare it with a constant which had
never been defined.

Whenever you have something with a repeating pattern, it is best to make either
a function or a variable pattern.  In this case I might do something with
printf() since you are sending the output to the browser.  Looking at it, you
have two things that change in your string:  part of the link and the label of
the link.

$fmt = '<a id="switch" href="../switcher.php?set=layout_%s">%s Font  Size</a>';

Then, in each of your case statements you can use:

printf($fmt, "medium", "Default");
printf($fmt, "large",  "Increase");
printf($fmt, "small",  "Decrease");

To apply this to my code example above:

$set = $_GET['set'];
$fmt = '<a id="switch" href="../switcher.php?set=layout_%s">%s Font  Size</a>';


if ((!$sitestyle) || $sitestyle == 'layout' )
{
 switch ($set)
 {
  case "layout_medium":
   printf($fmt, "medium", "Default");
   break;

  case "layout_large":
   printf($fmt, "large",  "Increase");
   break;

  case "layout_small":
   printf($fmt, "small",  "Decrease");
   break;

  case "layout_medium":
  default:
   printf($fmt, "medium", "Default");
 }
}

Since you are using the same output for "layout_medium" or the default, these
can be grouped together to a single statement.

I have not included the first if() statement since it seemed redundant.  I was
mainly focusing on the switch...case statement.

It is best to get into the habit of using php in lower case for the opening
tag.  There are some aspects of PHP which will vary depending on the version
and operating system (ie Windows vs Linux).  Good habits will minimize the
surprises that can be caused if your code moves to a new environment.

James

Reply via email to