-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.

  • 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 – Rizier123 Apr 30 '15 at 09:34
  • possible duplicate of [using AND/OR in if else PHP statement](http://stackoverflow.com/questions/4405795/using-and-or-in-if-else-php-statement). Also, is it really that difficult to google? Or even turn on error reporting? – Huey Apr 30 '15 at 09: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` http://stackoverflow.com/questions/2803321/and-vs-as-operator – RiggsFolly Apr 30 '15 at 09:43

2 Answers2

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"; }
Ayyanar G
  • 1,545
  • 1
  • 11
  • 24
0

You can check it with -

if (!empty($_POST['open_monday_lunch']) || !empty($_POST['opening_monday1'])) {
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
  • `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 Apr 30 '15 at 09:45
  • @Rizier123 thats the main point. If OP want to check for the same then can use this. :) – Sougata Bose Apr 30 '15 at 09:50