-3

I want to check if either of these checkboxes are checked and if either of them are i want the new assigned variable $open_monday to = "yes else "no".

   if (isset($_POST['open_monday_lunch'] or $_POST['opening_monday1'])) {

        $open_monday = "yes";

   }

   else { $open_monday = "no"; }

Is that the right way to do it? I have never used or before. I just get a blank pag when trying to run it as if the syntax is incorrect.

3
  • This is not how you use the or operator! do: isset($_POST['open_monday_lunch']) || isset($_POST['opening_monday1'])) You don't use it in the function call itself Commented Apr 30, 2015 at 9:34
  • possible duplicate of using AND/OR in if else PHP statement. Also, is it really that difficult to google? Or even turn on error reporting? Commented Apr 30, 2015 at 9:35
  • Your most obvious problem is that you have missed out some brackets. It should be if ( isset($_POST['open_monday_lunch']) or isset($_POST['opening_monday1']) ) { regardless of whether you use || or OR But you should also read this answer before moving from using '&&' and '||' to AND and OR stackoverflow.com/questions/2803321/and-vs-as-operator Commented Apr 30, 2015 at 9:43

2 Answers 2

2

Due to Operator precedence its always preferable to use && instead of and ,similarly || instead of or.please refer this link for additional info,

so try like this,

 if (isset($_POST['open_monday_lunch'] ) || isset( $_POST['opening_monday1'])) {

        $open_monday = "yes";

   }

   else { $open_monday = "no"; }
Sign up to request clarification or add additional context in comments.

7 Comments

Why should the OP "try this"? Please add an explanation of what you did and why you did it that way not only for the OP but for future visitors to SO.
Still showing as blank on this. what does || mean? Or?
@SpringheeledJack || is the same operator as or just with a higher precedence
@sgtBOSE I thought that they would recognize it, but apparently not :)
This code has still a syntax error in it! And it is accepted ?!
|
0

You can check it with -

if (!empty($_POST['open_monday_lunch']) || !empty($_POST['opening_monday1'])) {

2 Comments

empty() might not be necessary here or maybe even break the code if OP has a value attr. for the checkboxes with an empty value or false would it return false even if they are set
@Rizier123 thats the main point. If OP want to check for the same then can use this. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.