Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse error 3

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
Can someone see why I get the following error:

Parse error: parse error, unexpected $


<?php
session_start();

if (isset($_SESSION['DB6'])) {unset ($_SESSION['DB6']); }

elseif (isset($_SESSION['DB12'])) {unset ($_SESSION['DB12']); }

elseif (isset($_SESSION['DB22'])) {unset ($_SESSION['DB22']); }

elseif (isset($_SESSION['DB32'])) {unset ($_SESSION['DB32']); }

elseif (isset($_SESSION['DB34L'])) {unset ($_SESSION['DB34L']); }

elseif (isset($_SESSION['DB40'])) {unset ($_SESSION['DB40']); }

elseif (isset($_SESSION['DB64'])) {unset ($_SESSION['DB64']); }

elseif (isset($_SESSION['DB94'])) {unset ($_SESSION['DB94']); }

elseif (isset($_SESSION['DB124'])) {unset ($_SESSION['DB124']); }

else {//do nothing;}


header("Location: newmenu.htm");
header("Connection: close");
?>

I straightened the layout of code to make it easier to see
Many thanks
 
I can't see what it could've been. Can you tell us which line is the one that is bothering php? From the looks of things, it should work.
 
Many thanks. It points to line 28, which is blank?

26 header("Connection: close");
27 ?>
28

Strange?
 
It's a simple error.

"Unexpected $" means "I got to the end of the file, but I expected there to be more code".

PHP most commonly gives this error when parentheses or curly braces don't balance.

Considering you have this line:

else {//do nothing;}

"//" means "comment from here to the end of the line", so the closing curly brace is not part of your code.

Try:

else {/*do nothing;*/}

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Ah ha, thanks very much. I remmed out lines, ie // and found the line:

//else {//do nothing;}

to be the culprit. So you are correct and your code worked perfectly. Many thanks.
 
Your code can be cleaned up a bit..

You don't need the elseif statements, just make them normal if statements. You don't need the curly braces if there is only one statement in the if block.

I would get rid of the if statements completely and write the code as
Code:
foreach ($_SESSION as $k=>$v)
   switch ($k) {
      case 'DB6':
      case 'DB12':
      case 'DB22':
      case 'DB32':
      case 'DB34L':
      case 'DB40':
      case 'DB64':
      case 'DB94':
      case 'DB124':
         unset ($_SESSION[$k]);
         break;
   }

I think the above code is cleaner, easier to understand, and easier to update -- you just need to add one line per key when you have more session variables to unset.

Ken
 
Thankyou Ken, yes thats much cleaner. Have a star as well. I will look out next time for your speech on elseif sleipner, gues I missed a treat?

When I get a bit of breathing space, I must look into the use of arrays. I am used to do loops for most of this but I don't know whats out there. Can fields on a form be an array?, as well as session variables. If so It would be much easier writing code, and look cleaner. Actually looking at Kens code it looks as a form of loop, can you put something in - case 'DB6' HERE? :

Thanks again for the help.


 
I don't know whats out there
Try as a start:

Can fields on a form be an array?
See:
as well as session variables
As I have explained to you before, a session variable can hold anything. Even objects can be stored in sessions -- the only caveate being that the class from which the object is instantiated must be defined before your script invokes session_start().

can you put something in - case 'DB6' HERE? :
I don't understand your question. Perhaps the PHP online manual will answer your question:


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Sorry for delay, thanks sleipnir for the links.

I am trying to convert things to arrays, hopefully to reduce the code and be more efficient.

I am getting an error on one of these items, probably on
the session variable. DBC is the array. I have tried alternative code but still fails. ?? Thanks


if ($_POST[DBC[1]>0) $_SESSION[DBC[1]]
 
ZOR:
Don't thank me for some links in one sentence and then in the another point out that you haven't bothered to read them because it's irritating and patronizing. The whole reason I keep posting links in answer to your questions is that I see the gaps in your understanding of PHP and know that were you to actually read the links I post, you could fill in those gaps.

The answer to your question, which should be, "How to I reference the elements of mulidimensional arrays in PHP?", is answered in the very first link I provided in my last post in this thread. Again, read:

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
if ($_POST['DBC'][1]>0) $_SESSION['DBC'][1]

Thankyou.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top