--- William Piper <[EMAIL PROTECTED]> wrote:

> Hayden's Harness Attachment wrote:
> > Thanks. Your suggestions got me thinking and I seemed to fix the problem. I
> now get:
> > 
> > Parse error: syntax error, unexpected '=' in
> /usr/home/choroid/public_html/new/crf_header.php on line 37
> > 
> > for the code:
> > 
> > <?PHP
> > sitestyle = $_get[set] {
> > case 'layout_medium':
> > case 'layout_large':
> > case 'layout_small':
> > echo '<link rel="stylesheet" type="text/css" media="screen"
> > href="http://www.choroideremia.org/new/($sitestyle) '.css'" />';
> > $_session['sitestyle'];
> > switch ($sitestyle) {;
> >   break;
> > }
> > ?>
> > 
> > 
> > Angus MacKinnon
> > Infoforce Services
> > http://www.infoforce-services.com
> 
> Your case statement is out of order, do this instead:
> 
> <?PHP
> sitestyle = $_get[set] {
> switch ($sitestyle) {
>    case 'layout_medium':
>    case 'layout_large':
>    case 'layout_small':
>    echo '<link rel="stylesheet" type="text/css" media="screen" 
> href="http://www.choroideremia.org/new/($sitestyle) '.css'" />';
>    $_session['sitestyle'];
>    break;
> }
> ?>


That fixes the switch...case statement but I still wonder about the curly brace
block after the initial variable assignment.  Perhaps it's legal in PHP (I
haven't tested it) but it looks wrong to me.  I would normally do something
like this.  

Note also that $_GET must be capitalized or it is a completely different
variable that has nothing to do with the URL variables.  Put quotes around your
array keys to speed up execution slightly.

I prefer to use lower case <?php ... ?>

You must have a $ before the variable name when doing an assignment in PHP.

If you use double quotes around the string (and single quotes around HTML
properties which is legal), you can insert variables for evaluation.  You have
parens around the variable.  I suspect you saw a sample which used curly braces
around a variable to define the beginning and end in a double quoted string.

I prefer print over echo but it really doesn't matter on a simple program like
this.

$_SESSION must also be capitalized.  Variables are case sensitive in PHP
(built-in function names are not).  You can't merely refer to
$_SESSION['sitestyle'] in a program without an initial reference to
session_start().  Also, your program will fail with a "headers already sent"
error if your program outputs anything before you assign the value to
$_SESSION['sitestyle'].  I moved it before your echo to achieve this.  However,
if you have anything in the file before the opening PHP tag, even a space or a
blank line or piece of static HTML, you'll get the "headers already sent"
error.

With this in mind, you will probably want to include the <html><head> tags in a
print or echo before the one which sets the CSS style.

All three values for your switch...case statement will reach the same echo
statements.  If there is no default: block to follow, the break; is not needed.


<?php
  session_start();
  $sitestyle = $_GET['set'];
  switch ($sitestyle) 
  {
   case 'layout_medium':
   case 'layout_large':
   case 'layout_small':
     $_SESSION['sitestyle'] = $sitestyle;
     echo "<html><head>";
     echo "<link rel='stylesheet' type='text/css' media='screen' 
       href='http://www.choroideremia.org/new/$sitestyle.css' />";
     echo "</head><body>";
     break;
  }
?>


If I were teaching a class to see if people had learned anything about PHP
syntax after the period of instruction, the code you started with would be a
good test question since there are many areas with errors which would need
correction.  I hope we're not doing your homework for you. :)

James Keeline

ps.  Please use more informative subject lines than "Parse error".  This is the
most common error message in PHP and tells the potential reader nothing about
what your problem really is.  A better subject line would be:

Parse error while using switch...case to define external CSS page

Reply via email to