Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Just getting started and stumped parse error

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Just getting started and stumped parse error

Postby jasperx » Mon Jul 21, 2003 11:49 am

I am trying to put my first script in a page... hello world is working and so is phpinfo(). The script was grabbed off a browser detect posting and I thought it would be fun to play with... I have done asp scripting but this has me baffled.
The parse error is happening on the line which starts $browser
I am using BBEdit to edit... nice format and color help.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>

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

      $punish = 0;
        while (list ($key, $val) = each ($browser)) {
                if (strstr ($HTTP_USER_AGENT, $val)) {
                      $punish = 1;
                }
        }
       if ($punish) {

                echo "
We're sorry, your browser is older than dirt.
Please get something a tad newer. Thank you.                      
                        ";
               exit;
        }

?>


</body>
</html>
jasperx
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Jul 21, 2003 11:39 am

Postby swirlee » 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.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby jasperx » Mon Jul 21, 2003 7:12 pm

Thanks for the reply...
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:...." But hey... why not give it a whirl so I changed the file to this:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<?php
  $browser = array (
      'MSIE 5',
      'MSIE 4',
      'MSIE 3',
      'MSIE',
      'Mozilla'
   );
      $punish = 0;
        while (list ($key, $val) = each ($browser)) {
                if (strstr ($HTTP_USER_AGENT, $val)) {
                      $punish = 1;
                }
        }
       if ($punish) {

                echo "
We're sorry, your browser is older than dirt.
Please get something a tad newer. Thank you.                      
                        ";
               exit;
        }
?>

</body>
</html>
The results are exactly the same parse error on the same line ($browser= ....). So there must be something else wrong. Really, really wrong. In fact I can comment out the offending line with // or /* */ and still get the error. I am starting to wonder if there is something wrong with my use of <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">... like it won't work with php?
If I can get the thing to parse I will enthusiastically try the foreach language... this is not my code... it is just something I picked up to play with... and yes I agree it would be silly/inappropriate, immature, etc, etc to use this in a real application... it is simply a vehicle I am using to learn scripting with php... and right now it has me in the ditch.
jasperx
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Jul 21, 2003 11:39 am

it works for me

Postby net_virgin » Tue Jul 22, 2003 4:38 am

I have just copied your code into a new php page, ran it and as expected it told me ie6 was out of date.

although would agree to use the foreach, slightly neater:

foreach ($browser as $key=>$val){
if (strstr ($HTTP_USER_AGENT, $val)) {
$punish = 1;
}
}
net_virgin
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue Jul 22, 2003 2:52 am

Postby pootergeist » 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.
pootergeist
New php-forum User
New php-forum User
 
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK

Postby swirlee » 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.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

parse error.... check for gremlins or zoombies or whatever!

Postby jasperx » Tue Jul 22, 2003 4:52 pm

The mystery is solved.... but it leaves me with more questions. The problem was that my page file contained some invisible characters which were causing the problem. I had copied/pasted the script into an editor.. I think it was BBEdit and I never saw the rascals. After hearing that the script ran fine, I discovered some options to zap gremlins and optimize in BBEdit. This all seems a bit mysterious. And since I have been able to use my enter key on script files at least some of the time I really don't know how I caused the problem or how to avoid it. I have been told that vim will show me the characters... I can use vim but it isn't nearly as much fun as BBEdit.
jasperx
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Jul 21, 2003 11:39 am


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.