Just getting started and stumped parse error

A

Anonymous

Guest
Your array declaration is wrong. If you specify a key for one element, you have to specify a key for all of them. So you have two choices:

Code:
<?
   $browser = array (
      'MSIE 5',
      'MSIE 4',
      'MSIE 3',
      'MSIE',
      'Mozilla'
   );
?>

OR

Code:
<?
   $browser = array (
      0 => 'MSIE 5',
      1 => 'MSIE 4',
      2 => 'MSIE 3',
      3 => 'MSIE',
      4 => 'Mozilla'
   );
?>

I personally recommend the first.

You should also consider using foreach() instead of while() for iterating through an entire array.

I also personally recommend not using this script in the first place. It's counter to common sense to turn away users based on their platform, and is the hallmark of arrogant and lazy web design. Not to mention you're going to turn away every user with this script, considering that all major browser have "Mozilla" or "MSIE" in the user-agent string.
 
exactly what error are you getting?
I assume BBedit has line numbering so presume you have the right line, though you cannot (I repeat cannot) get a parse error for a commented out line, so something is wrong somewhere.

on an aside - use $_SERVER['USER_AGENT']; rather than the old archaic way - eventually you'll need to upgrade you variable access calls anyway, so best start now.
 
jasperx said:
the basis for my array assignment is p. 118 of the O'Reilly text on PHP(Lerdorf and Tatroe).
"You can specify an initial string with => and then a list of values. The values are inserted into the array starting with that key, with subsequent values having sequential keys:...."

You're right, this was my mistake.

And concerning parse errors on commented lines, often PHP will report a parse error many lines after, but never before, where the actual problem is. This is a frequent problem with bracket mismatches -- just last night I was writing a script with several nested conditionals and PHP reported a parse error about 7 lines below the actual error -- an extra bracket had slipped in by mistake.

Anyway, I also copied and pasted your code and it ran fine, without incident.
 
Back
Top