Jump to content

Basic Logic


icor1031
Go to solution Solved by Jacques1,

Recommended Posts

This is what I want:

 

If comparisonTimeH isn't zero, I want it to check if comparisonTimeH is zero, and comparisonTimeM is larger than or equal to 2. If so, I want it to pass and run the script.

 

 

 

This is what I have, and it's not right. It runs the else statement, echo test 2.

    if ($comparisonTimeH != 0 || $comparisonTimeH = 0 AND $comparisonTimeM >= 1) {
        echo "test";
    }
    
    else {
        echo "test2";
    }

    echo "<br />" . $comparisonTimeH . " " . $comparisonTimeM;

My last echo, the one outside of the if statements - it returns 0 and 12. Thus, both conditions that I'm trying to get, are true.

My if's logic is clearly wrong, and I can't figure out what operators to use. If I change the AND to &&, it breaks and my final echo doesn't return comparisonTimeH (it's blank).

 

Please teach me, thanks!

Edited by icor1031
Link to comment
Share on other sites

  • Solution

You're using the assignment operator “=” instead of the equality operator “==”, and you've confused yourself with exotic precedence rules.

 

Fix the operator, and use parentheses to define the order of evaluation. When you find yourself juggling with different variations of the logical “and” operator, that's a sure sign you need to stop.

 

The condition can also be simplified to

$comparisonTimeH != 0 || $comparisonTimeM >= 1

If $comparisonTimeH != 0 is false, then $comparisonTimeH == 0 is automatically true, so no need to write that down.

Edited by Jacques1
Link to comment
Share on other sites

 

You're using the assignment operator “=” instead of the equality operator “==”

 

He wants to use assignment operator there,it's a tricky question. 

 

In the first example, we've got - "true" in the second one - "false".

$var1 = FALSE || TRUE;
$var2 = FALSE OR TRUE;

The answer is precedence.

 

@OP, never ever mix up the logical operators written in pure English words with those one written with symbols. If you want to separate the logic in your application always use parentheses!

Edited by jazzman1
Link to comment
Share on other sites

He wants to use assignment operator there

 

No, he or she doesn't. Read the question again: The goal is to make an equality check.

 

 

 

@OP, never ever mix up the logical operators written in pure English words with those one written with symbols. If you want to separate the logic in your application always use parentheses!

 

Yes, I already said that.

Link to comment
Share on other sites

Yes, I was confused with the precedence. And it was quite a silly mistake to use assignment :-\ .

Thanks! :pirate:

 

You're using the assignment operator “=” instead of the equality operator “==”, and you've confused yourself with exotic precedence rules.

 

Fix the operator, and use parentheses to define the order of evaluation. When you find yourself juggling with different variations of the logical “and” operator, that's a sure sign you need to stop.

 

The condition can also be simplified to

$comparisonTimeH != 0 || $comparisonTimeM >= 1

If $comparisonTimeH != 0 is false, then $comparisonTimeH == 0 is automatically true, so no need to write that down.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.