-2

OK I'm returning to PHP after not using it for a couple of years and I'm trying to do a simple check of a $_POST variable.

I have:

if(isset($_POST['partydate'])) { $partydate = $_POST['partydate'] } else { $partydate = "No Date Selected" };

It's the only thing on that line but I keep getting the following when the page runs:

Parse error: syntax error, unexpected '}' in C:\xampp\htdocs....... on line 3

What is the obviously VERY simple thing I am overlooking here?!

7
  • I think you'd appreciate the conditional operator: $partydate = isset($_POST['partydate']) ? $_POST['partydate'] : "No Date Selected"; Commented Feb 20, 2013 at 23:10
  • 1
    @SteAp, your edit actually fixes the bad code that is the point of the question, so answers don't make sense anymore. Commented Feb 20, 2013 at 23:18
  • Oh, I'm sorry for that. Did I really added the two ';' signs? Should I rewrite it to make it a one-liner again? Commented Feb 20, 2013 at 23:20
  • Yes! As @SirDarius says you've actually made a bad question even worse! Commented Feb 20, 2013 at 23:21
  • @DuckInCustard In your next post, please indent source-code. Without indentation, it is very hard to read. Commented Feb 20, 2013 at 23:23

4 Answers 4

1

One-liners are bad for code readability. If displayed better, your code becomes:

if (isset($_POST['partydate'])) {
    $partydate = $_POST['partydate']
}
else { 
    $partydate = "No Date Selected"
}
;

So as you can see, you are missing semi-colons in your if and else blocks. Proper code is:

if (isset($_POST['partydate'])) {
    $partydate = $_POST['partydate'];
}
else { 
    $partydate = "No Date Selected";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant!! I. Am. An. Idiot!! Was just trying to keep everything compact as I'm checking about 10 variables so didn't want to have 6 or so lines for each check. An in the process have made myself look a bit of a tit! Will make proper use of the ternary operator as suggested by a couple of others. Thank you all.
1

Use the ternary if if you really want to use a one-liner:

$partydate = isset($_POST['partydate']) ? $_POST['partydate'] : "No Date Selected";

1 Comment

Thank you. Forgot about the ternary operator. Will do this thank you. Have accepted @SirDarius as the answer though as he actually explains what is wrong with my original code.
0

Try this for size, with proper placement of ";"

if(isset($_POST['partydate'])) { 
    $partydate = $_POST['partydate'];
} else { 
   $partydate = "No Date Selected";

}

Comments

0

You have miss two

;

after the assignment of the string try this code:

if(isset($_POST['partydate'])) { 
      $partydate = $_POST['partydate']; 
} else { 
     $partydate = "No Date Selected"; 
};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.