0

I have the following code:

if ($Type != "DEA" and $VA != "Allowed" and $VolSess != 1) {
     $max_rows = max($CMSReg_num_rows);
     if ($max_rows == 0) {
        mail($to, $subject, $body);
        header('Location: '.bloginfo('home_url').'/profile');
    }
}

The problem I have is that that an email is sent despite the if-statement being false, and only an email is sent. The rest of the code is not executed, i.e. no redirect. And when I comment out the mail() function, it does not send the email.

And when I add this code:

if ($VA == "Allowed") {
echo "VA = " . $VA;
}
if ($VolSess == 1) {
echo "VolSess = " . $VolSess;
}

I get this output:

VA = Allowed VolSess = 1 

So I know that the condition in the if statement is false.

Ali
  • 33
  • 2
  • 8
  • try `var_dump()` for both... – Praveen Kumar Purushothaman Feb 27 '14 at 20:53
  • there is no mention of `VS = ` in the code. I don't think we are getting the whole picture. – Samuel Cook Feb 27 '14 at 20:54
  • 2
    dump all the variables from if, and you'll find the solution – user4035 Feb 27 '14 at 20:57
  • Where's the echo for `$Type`? Also, use `===` for your tests. `== 0` is ambiguous with `== FALSE`. Also, I always use `&&` and `||`, but doubt that affects things. – Krista K Feb 27 '14 at 20:57
  • Sorry, VS was supposed to be VolSess. And when i use var_dump() on both, this is what i get:" string(7) "Allowed" int(1) " @Chris K: $Type is in the URL, so I didn't echo it, but it's equal to "EA", not "DEA" – Ali Feb 27 '14 at 20:58
  • 2
    You shouldn't quote a string that only contains a variable. Instead of writing `"$Type"`, just write `$Type`. – Mike Feb 27 '14 at 20:59
  • @Mike: I did that, no difference. That was probably wrong on my part but it still doesnt make a difference to the if statement – Ali Feb 27 '14 at 21:02
  • @Ali I know it doesn't make a difference. If I thought it would solve your problem I would have put it in an answer. It's just not good practice to quote variables like that. – Mike Feb 27 '14 at 21:04
  • 1
    @Ali quit guessing what's going wrong and listen to the first comment above and dump the variables and work through the logic yourself. – Mike Feb 27 '14 at 21:15

4 Answers4

3

AND has a different order of precedence compared to &&. So your expression does not evaluate as you expect it to.

("$Type" != "DEA" and $VA != "Allowed" and $VolSess != 1)

should be

(("$Type" != "DEA") and ($VA != "Allowed") and ($VolSess != 1))

or

("$Type" != "DEA" && $VA != "Allowed" && $VolSess != 1)

for it to work as you expect it. This is one of those tiny mistakes/bugs that's easy to overlook.

CodeAngry
  • 12,760
  • 3
  • 50
  • 57
  • I changed "and" to "&&", still the same issue and I tried putting it in brackets before, didn't make a difference – Ali Feb 27 '14 at 21:06
  • 1
    This doesn't even make sense. *ALL* the operators are `and`, so it doesn't matter whether you use `&&` or `and`. – Mike Feb 27 '14 at 21:08
  • `die;` after `header('Location: '.bloginfo('home_url').'/profile', 302);`. Also add `ini_set('display_errors', true); error_reporting(-1);` and tell us if you see a error. – CodeAngry Feb 27 '14 at 21:09
  • @Ali I don't know... Use `echo __LINE__, '
    ', PHP_EOL;` on each line of your script and see what happens, where the script goes.
    – CodeAngry Feb 27 '14 at 21:18
0

Try:

if ($Type != 'DEA' && $VA != 'Allowed' && $VolSess != 1) 
{
  $max_rows = max($CMSReg_num_rows);
  if ($max_rows === 0)
  {
    mail($to, $subject, $body);
    header('Location: ' . bloginfo('home_url') . '/profile');
  }
}

EDIT The above works, but so does the oringal question code... The problem is elsewhere.

<?php

$Type = 'foo';
$VA = 'Allowed';
$VolSess = 1;

if ($Type != 'DEA' and $VA != 'Allowed' and $VolSess != 1)
{
  $max_rows = 0;
  if ($max_rows === 0)
  {
    echo 'Orig True';
  }
}
else
{
  echo 'fine?';
}

if ($Type != 'DEA' && $VA != 'Allowed' && $VolSess != 1)
{
  $max_rows = 0;
  if ($max_rows === 0)
  {
    echo 'Second True';
  }
}
else
{
  echo 'fine?';
}

?>

Both print 'fine?' Implying your error is elsewhere in your code.

J. A. Streich
  • 1,683
  • 10
  • 13
  • @Fabio using `&&` instead of `and` – Mike Feb 27 '14 at 21:04
  • There are three changes, the important one to the question is the 'and' and '&&' switch. Changing that will solve his problem b/c of order of operations. In addition switch $max_rows === 0, will guard against accidental equivalency with null and an empty string. Lastly, double quotes are scanned for special escapes and PHP variables and slows down processing, so I switched to single quotes. – J. A. Streich Feb 27 '14 at 21:08
  • 1
    @J.A.Streich "Lastly, double quotes are scanned for special escapes and PHP variables and slows down processing, so I switched to single quotes". That's not really true anymore. See http://stackoverflow.com/questions/13620/speed-difference-in-using-inline-strings-vs-concatenation-in-php5 – Mike Feb 27 '14 at 21:10
  • @Mike Nice to know. I think I'll probably keep my convention, as I still hold on ++i in C/C++ and ++$i in php. – J. A. Streich Feb 27 '14 at 21:31
  • @J.A.Streich they both printed `fine?`, so the problem is somewhere else then, I'll try to figure it out. Thanks for the help – Ali Feb 27 '14 at 21:34
  • @Ali -- Post more of the or the whole file, if you can. There is something else going on here. – J. A. Streich Feb 27 '14 at 21:36
  • http://pastebin.com/k23bd5g9 This is the whole file if you wanna take a look at it – Ali Feb 27 '14 at 21:43
0

try do an else after...

elseif($VA == "Allowed"){}
splifo
  • 45
  • 5
0
  • Try using the WordPress wp_mail().
  • die; after header() and also add 302 as a second argument to the header() function.
  • Enable error reporting with ini_set('display_errors', true); error_reporting(-1); on top of your PHP code.

Tell us what you see after making these changes.

CodeAngry
  • 12,760
  • 3
  • 50
  • 57
  • I added `die;`, no difference, `wp_mail` didn't work either. And `error_reporting` gave me a whole bunch of errors (there are other files linked to this file as well, and it gave errors in those files too), so I'll go through them and try and fix them. Thanks a lot for the input – Ali Feb 27 '14 at 21:31