Just getting started and stumped parse error

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
User avatar
swirlee
Moderator
Moderator
Posts: 2257
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Mon Jul 21, 2003 12:17 pm

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: Select all

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


OR

Code: Select all

<?
   $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.
Image

pootergeist
New php-forum User
New php-forum User
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK
Contact:

Tue Jul 22, 2003 4:55 am

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.
avatar image based upon nasas apod (only updates if I post though)

User avatar
swirlee
Moderator
Moderator
Posts: 2257
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Tue Jul 22, 2003 8:15 am

jasperx wrote: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.
Image

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest