Jump to content

PHP if and or statement not working and I am not sure why - Can anyone help me with this


Go to solution Solved by jodunno,

Recommended Posts

Hi,

I am having a problem with this if and or statement. It is a part of my form validation. But if give the same error regardless


The way I need it to work is like this:
$p, $d, $c and $o can all be Yes or No
If $p is Yes I then need to check if $d, $c or $o is Yes
If not it give an the error

This is the current code I have.
I have tried the same code without brackets before the or part, but it give the same problem

if ($p == "Yes" && ($d != "Yes" || $c != "Yes" || $o != "Yes")) {
    echo "error";
}

Any help or fix would be great, I am sure it something silly, or maybe I got it the wrong way around or something silly like that.

Thanks

your written statement is ambiguous. please post some examples showing what result you want for different input combinations. specifically, what is the 'successful' case, which can then be complemented to produce the error case? what do you want when $p is not Yes? is that an error or does it mean that you don't care about the other three values?

Edited by mac_gyver
  • Thanks 1
Posted (edited)
1 hour ago, mac_gyver said:

your written statement is ambiguous. please post some examples showing what result you want for different input combinations. specifically, what is the 'successful' case, which can then be complemented to produce the error case? what do you want when $p is not Yes? is that an error or does it mean that you don't care about the other three values?

The way I need it to work is:
If $p is No, then ignore and continue
If $p is Yes I then need to check if one of the following $d, $c or $o is Yes
If not it give an the error

Edited by CBG

the 'successful' case is: $d == "Yes" || $c == "Yes" || $o == "Yes"

because you are using negative logic to produce an error, the complement of this is: $d != "Yes" && $c != "Yes" && $o != "Yes"

// what i think you want
if ($p == "Yes" && $d != "Yes" && $c != "Yes" && $o != "Yes")
{
	echo "error";
}

 

  • Thanks 1
Posted (edited)
17 minutes ago, mac_gyver said:

the 'successful' case is: $d == "Yes" || $c == "Yes" || $o == "Yes"

because you are using negative logic to produce an error, the complement of this is: $d != "Yes" && $c != "Yes" && $o != "Yes"

// what i think you want
if ($p == "Yes" && $d != "Yes" && $c != "Yes" && $o != "Yes")
{
	echo "error";
}

 

Would this give the option to do under the following examples below?
Should of done this in the begin

Here are the examples I need the statement to do:

Example 1 - p No - Do nothing else
p No
Continue and do nothing

Example 2 - p Yes, o Yes - Continue
p Yes - Now change if one fo the following is Yes: d or c or o if not give an error
d No
c No
o Yes
Continue and do nothing

Example 3 - p Yes, c Yes - Continue
p Yes - Now change if one fo the following is Yes: d or c or o if not give an error
d No
c Yes
o No
Continue and do nothing

Example 4 - p Yes, d Yes - Continue
p Yes
d Yes
c No
o No
Continue and do nothing

Example 5  - p Yes, d,c and o are No - give an error 
p Yes
d No
c No
o No
Give an error

 

Edited by CBG
  • Solution

Hi CBG,

Logical switches do not work like spoken words and it is easy to misunderstand them. What you really want to check is that if any one of d,c,o is yes, then error. But PHP is obeying your instructions by stopping when anyone of them is No. You need to switch the OR to an AND and it will catch the one Yes that is not allowed:

	<?php
$p = "Yes"; //yes is different than Yes
$d = "No";
$c = "No";
$o = "No";
	if ($p === "Yes" && $d !== "Yes" && $c !== "Yes" && $o !== "Yes") {
    echo "error";
    exit;
}   echo 'no errors';
?>
	

which will display an error if all three (d,c,o) are No. Otherwise, the code will display no errors.

but what are you doing when a lowercase y is used? yes instead of Yes. maybe you should also use lowercase and compare with strtolower().

  • Thanks 1
Posted (edited)
27 minutes ago, jodunno said:

Hi CBG,

Logical switches do not work like spoken words and it is easy to misunderstand them. What you really want to check is that if any one of d,c,o is yes, then error. But PHP is obeying your instructions by stopping when anyone of them is No. You need to switch the OR to an AND and it will catch the one Yes that is not allowed:

 

	<?php
$p = "Yes"; //yes is different than Yes
$d = "No";
$c = "No";
$o = "No";
	if ($p === "Yes" && $d !== "Yes" && $c !== "Yes" && $o !== "Yes") {
    echo "error";
    exit;
}   echo 'no errors';
?>
	

 

which will display an error if all three (d,c,o) are No. Otherwise, the code will display no errors.

but what are you doing when a lowercase y is used? yes instead of Yes. maybe you should also use lowercase and compare with strtolower().

Thanks for the help, to you both, it is very much appreciated.

It will always be a capital Y in Yes as it the value  is hardcoded in the form on the checkbox in the form.
If it not a capital Y, it need to fail anyway, as it means something gone wrong somewhere.
I will give this a try in a bit, and mark up the solution.

Edited by CBG
3 hours ago, CBG said:

Would this give the option to do under the following examples below?

yes. which you can determine by testing.

2 hours ago, CBG said:

the checkbox in the form

unchecked checkboxes are not set in the submitted form data. your code should only be concerned if the checkbox data isset() or is (not) !isset(). the logic would be -

if (isset($p) && !isset($d) && !isset($c) && !isset($o))
{
	echo "error";
}

 

Posted (edited)
14 minutes ago, mac_gyver said:

yes. which you can determine by testing.

unchecked checkboxes are not set in the submitted form data. your code should only be concerned if the checkbox data isset() or is (not) !isset(). the logic would be -

if (isset($p) && !isset($d) && !isset($c) && !isset($o))
{
	echo "error";
}

 

I have a set of if else for each of d, c and o to change the answer to either be Yes or No to check for in the code, using the below.
It check the value from the form checkbox, this is working fine.
 

		if ($d == "D") {
			$d = "Yes";
		} else {
			$d = "No";
		}
		if ($c == "C") {
			$c = "Yes";
		} else {
			$c = "No";
		}
		if ($o == "O") {
			$o = "Yes";
		} else {
			$o = "No";
		}

 

Edited by CBG

You're limiting yourself by trying to do everything at once. Take it in logical steps:

	if(
		$d != 'Yes'
		&& $c != 'Yes'
		&& $o != 'Yes'
	) {
		echo 'Nope';
		return false;
	}
	if($p != 'Yes'){
		echo 'Nope';
		return false;
	}
	echo 'Yup';
	return true;

If all the three additional variables is not 'Yes', kill it. If any of the three is 'Yes', check the value of $p (you can switch these around if you want, it shouldn't matter). If $p is not 'Yes', kill it. Otherwise, you know $p is 'Yes' and at least one of the other three variables is 'Yes', so your criteria is met and you can continue with your logic.

Edited by maxxd

Hi CBG,

@mac_gyver actually posted the solution that you have chosen. And i want to say that mac_gyver and maxxd are better programmers and they have more experience than i do in this field. However, i disagree with any solutions that create a bunch of if branches because every step that a program takes can really slow it down. I definitely disagree that you cannot check it all at once. Personally, i recommend using the tools in the PHP toolbox to your advantage. I would do the following and be done with it:

<?php

$p = 'Yes';
$d = 'No';
$c = 'No';
$o = 'No';
$pinary = [$p] === ['Yes'];
$nerror = [$d, $c, $o] === ['No','No','No'];

if ($p && $nerror) { echo 'error'; exit; }
echo 'No errors detected';

?>
	

then i would unset $pinary and $nerror to spare memory (even though 100% of coders would tell you not to do that). My point, is that you can check it all at once but you could do it differently. I would define an error and simply check if it is true.

Edited by jodunno
board code keeps formatting my code. I had to backspace the added spaces.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.