1

I am noticing some weird behavior in my script, not sure why its occurring though.

if ($date != 'NOW()' ||  $date != 'NULL')
{
   // throw error      
} else {
   // run functions
}

I have a IF statement similar to the above in one of my functions, i pass the value 'NULL' from my function which it is receiving fine. But when doing the comparison with the IF statement, it doesn't work. Its always skipping to else statement.

UPDATE:
Below is the complete code for my function, i hope it helps.

public function setLastLoginDate( $date )
{
    if (isset($date) && !empty($date))
    {
        if ($date != 'NOW()' ||  $date != 'NULL' || is_datetime(convertDateTimeToSql($date)) == false)
        {
            $this->errors['user_last_login'] = 'invalid date specified.';
        } else {
            this->properties['user_last_login'] = $date;
        }
    }
}

LAST UPDATE
Using && does work instead of ||, but i never intended to use &&. if thats the way it is suppose to be then i guess i have to reRead about that.

Isn't && to check if two variables return true? like if (is_string($foo) && strlen($foo) > 1) ? but i want to check if the variable contains either of the values

But this also does the trick for me:

if ( $date == 'NOW()' ||  $date == 'NULL' || is_datetime(convertDateTimeToSql($date)) == true )
{
    $this->properties['user_last_login'] = $date;
} else {
    $this->errors['user_last_login'] = 'invalid date specified.';
}

Did some testing and its working as i want it to with the above. Is there something wrong using it as above?

Thanks

6
  • I am basically assigning a string here, and comparing it to a string. This has to work but not sure why. I am checking if the date variable is a string with the value 'NOW()', i pass this value from my script when i know it is needed. What it should do is assign the variable the value 'NOW()' i later run a prepare function, when it assigns a value for the db field date it sends the mysql function 'NOW()' to it. Commented Apr 3, 2012 at 14:20
  • btw, i have similar behavior in another function. But that one is working fine for some unknown reason. Commented Apr 3, 2012 at 14:21
  • updated with more code to better explain. Commented Apr 3, 2012 at 14:27
  • added the prepare function now. Commented Apr 3, 2012 at 14:33
  • 1
    No, there's nothing wrong with the way you're doing now, because now you're checking if it is equal to one value OR equal to another value. Previously you checked if it was NOT equal to one value AND NOT equal to another value. Big difference when it comes to the logic! Commented Apr 5, 2012 at 19:31

7 Answers 7

7

You ask if a condition is not a value or not another value, this will always return true. Probably you wanted to use an and instead (&& instead of ||).

Sign up to request clarification or add additional context in comments.

6 Comments

I am wanting to check if $date variable is this or that.
@Zubair1 - You instead check if $date is not this or not that.
@Zubair1 - After seeing your update, i think you really wanted to use or. But then you should check for equality with ==, not check for unequality with !=.
lol, i missed this last comment of yours, you're spot on about what i wanted, and the solution as well. But why is it that i have to use it with == instead of '!='? do you have something to read about it?
@Zubair1 - The PHP documentation has good examples about these comparison operators. In your examples you ask for different things, the first example wants to make sure there is no text like 'NOW()' and then will execute an action. Your update example wants to make sure there is text like 'NOW()' and then give out an error (will not execute the action).
|
4

Check your if-statement conditions. Are you sure you want to compare that way? As I see it, date cannot be 'NOW()' and 'NULL' at the same time, which means that your if-statement would always come true.

Also add some parenthesis.

Maybe this is better?

if (($date != 'NOW()') && ($date != 'NULL'))
{
   // run functions
} else {
   // throw error
}

1 Comment

I am basically assigning a string here, and comparing it to a string. This has to work but not sure why. I am checking if the date variable is a string with the value 'NOW()', i pass this value from my script when i know it is needed. What it should do is assign the variable the value 'NOW()' i later run a prepare function, when it assigns a value for the db field date it sends the mysql function 'NOW()' to it.
4

Your statement will never resolve to false because a string cannot be two different things. Make the condition &&.

So your statement will look something like this:

if( $date! = 'NOW()' && $date != 'NULL' ){
   // run functions
}else{
   // throw error
}

2 Comments

I am not doing a NULL comparison if you haven't noticed, its a string comparison, its the same even if i try passing 'NOW()'
Then debug it by die('test') in each condition and test that
2

You say that the if always skips directly to the else but there's no way that part of the script will ever run. The reason is, as others have pointed out, you're using an OR where you should be using an AND. The $date variable can never be both 'NULL' and 'NOW()' at the same time which means that the if will always return true and do the "invalid date" part.

Here's why:

$date = 'NULL'

The check is effectively done in three steps (these may not be in correct order),

A:

$date != 'NOW()' // This returns true because $date is 'NULL'

B:

$date != 'NULL' // This returns false because $date is not 'not equal' to 'NULL'

C:

if (A || B) // If either one or both A and B is true then this is true.
            // Since $date can never be both NULL and NOW() at the same time
            // one of A or B is guaranteed to be true so the if() will always return true.

What you need is:

C:

if (A && B) // If BOTH A and B are true then this is true.

Comments

1

if ($date != 'NOW()' || $date != 'NULL')

This logic catches everything. Are you sure it's skipping to else? It should in fact never use else; $date can never equal both 'NOW()' and 'NULL'.

Comments

0

Besides that || you have in your if condition which probably should be && check also if it really jumps to the else part. That's not possible.

In your pseudo code you mentioned that you throw error in else part but that doesn't correspond with your actual code you posted later. There you have the error part immediately after if.

Maybe you just swapped this lines and you still think it goes to the else part.

Comments

-2

NOW() is a mysql function, not a PHP one. Also remove the quotes.

5 Comments

I am doing this in a function to prepare a MySQL Query.
@zubair1 - would you please care to explain the downvote? Is it not your responsibility to put this information in the question itself? or do you expect me to read your mind....
I don't mean to be rude, but i provided as much detail as possible, but you assumed me to be stupid by saying NOW() is a mysql function, i'm clearly using that in a string.. it doesn't matter if its a mysql function i'm showing a string, you clearly did not read through everything before asnwering or did not comprehend it.
plus, i forgot to mention, first you're telling me its not a PHP function then you're asking me to remove the quotes to make it behave like a PHP function. lol.
The problem was in the logic, not in the use of NOW(). That's the reason why I'm also downvoting. It's ok to use 'NOW()' as a string.

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.