Jump to content

Basic Logic


icor1031

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!

Link to comment
https://forums.phpfreaks.com/topic/291140-basic-logic/
Share on other sites

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
https://forums.phpfreaks.com/topic/291140-basic-logic/#findComment-1491466
Share on other sites

  Quote

 

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!

Link to comment
https://forums.phpfreaks.com/topic/291140-basic-logic/#findComment-1491471
Share on other sites

  On 9/18/2014 at 2:19 AM, jazzman1 said:

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.

 

 

 

  On 9/18/2014 at 2:19 AM, jazzman1 said:

@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
https://forums.phpfreaks.com/topic/291140-basic-logic/#findComment-1491473
Share on other sites

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

Thanks! :pirate:

 

  On 9/18/2014 at 1:16 AM, Jacques1 said:

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
https://forums.phpfreaks.com/topic/291140-basic-logic/#findComment-1491475
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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