Finding out which value is higher

Asked

Viewed 493 times

3

Follow the code below:

<?php
        $timeZone = new DateTimeZone('UTC');
        $dataInicio = new DateTime('2016-06-14', $timeZone);//dataFinal do evento
        $dataFinal = new DateTime('2016-06-25', $timeZone);//dataFinal do evento
        $diadasemana = "16/06";
        if (($diadasemana >= $dataInicio->format('d/m')) && ($diadasemana <= $dataFinal->format('d/m'))) {
            //16/06 => //14/06 && 16/06 < 25/06
            echo 'Entrei no IF';
        } else {
            echo 'não somos igauis';
        }
//retorno Entrei no IF

How PHP can interpret which value is higher?

  • diadasemana is equal to date and smaller than final date. And you’re asking exactly that. Then it is logical that it enters in the first condition and execute the line 'I entered in the IF'

  • @Reginaldorigo this is obvious, I’m hoping that if possible explain to me how php can understand which is bigger or smaller. Ex : //16/06 => /14/06 && 14/06 < 25/06

  • 1

    How do you know that 16 is bigger than 14 and that it’s smaller than 25? Counting. Right? PHP too.

  • 1

    You can stick to your code, it’s not a good idea. It’s better to use new DateTime < new DateTime('-1 day'). is better to use the PHP engine itself

  • @Reginaldorigo beauty, said another obvious thing. Dude, if you’re going to be ignorant like that, don’t comment. You are neither paid nor required to do so. I am not just comparing before the / but the entire string. Pay attention

  • I’m sorry if I seemed ignorant. I didn’t mean to be rude. It turns out that the question was too obvious and there was no alternative but to be obvious. Anyway: sorry about that.

Show 1 more comment

1 answer

6


Going by parts

The code runs in parts. First it calculates some parts to get a boolean result:

$dataInicio->format('d/m') //resulta em "14/06"

"16/06" >= "14/06" //resulta em true porque são iguais

$dataFinal->format('d/m') //resulta em "25/06"

"14/06" <= "25/06" //resulta em true porque 1 é menor que 2, só olha o primeiro caractere

The operator && is a and, then the result will be true where the two operands are true, and false in all other situations. This is boolean algebra basic.

PHP treats the number 0 as false and the other numbers as true.

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Note the result of the logical sub-expression that gives 1, which is the same as true.

Flow of if

As the end result is a true the flow of code enters the first block of code, ignoring the second, after all they are exclusionary. After executing the first block it goes to the next instruction to the complete block of the if/else. If the result had been false, the second block (after the else) would have been run following for the next instruction, but the first block would have been completely ignored.

The if is precisely an execution flow control command. Its function is to decide which block to execute (there are cases where there is only one block, or there may be several blocks) according to the value of the conditional expression placed in it.

The calculation of the parts within the if (the condition), although very common, is practically an accident. This calculation could have been made out of the if and only the result of it being used.

A logical expression (boolean, which results in false or true) generates a result like any other, it is possible to save in variable or do what you want, as well as to do with numbers, texts and other types of objects. In the case of PHP this is even more obvious because in the background the result is up to a number 0 or 1 to handle the two possible results, respectively. Some people do not understand this and end up thinking that the boolean can only be used inside a if, while or something like that. That is far from the truth. It is a given like any other.

That’s why I always say that programming is about understanding how each minimal part of the code works and not just reproducing codes found on the Internet or elsewhere. It’s important to be curious, ask what you don’t know, how it was done here.

Comparison of strings

Note that compare numbers as strings can be problematic. So if you compare "5/5" to "20/5", the second will be smaller, because the character "2" comes before the character "5". The comparison is made character by character, it does not attempt to interpret the text and treat it as a number or date. It only works if the scales are equal, the positions of the digits are the same and the numerical text starts from the largest to the smallest (see below).

In his example the comparisons made are 16/06 with 14/06 (the first character is the same, but you can’t be sure that everything is the same, the second character is larger than the second of the other text, you can be sure that everything is bigger and doesn’t need to continue) and then in the other comparison: 1 (first character of "14/06") with 2 (first character of "25/06"), the comparison is optimized, you can already know that 1 is less than 2 and whatever comes after it makes no difference anymore.

The comparison is made in short-circuit mode, that is, when he can already have a definitive result he does not continue to compare. If you want to know if two texts are equal, and the comparison of the first character of a text with the first character of the other is already different, there is no reason to continue comparing.

The same occurs when searching for the smallest. If the first character already establishes which is the smallest, there is no reason to continue evaluating the rest.

Comparison of numbers and other objects

If it were a comparison with numbers, then the comparison would be more mathematical, comparing bits, something that the processor already knows how to do and does not need any special algorithm. Actually the bit comparison is done very similar to what I showed in string, of course it is done much more efficiently, probably in a single processor step.

In fact even the comparison of the characters in the background are numbers, the comparison will occur bit by bit (according to the processor), after all the concept of character is already a more abstract concept yet. There will be a number that we see as a character according to a table previously known to us, but internally they are just bits. The difference between comparing a pure number and a ceiling that has numbers is that the first is done atomically by the processor (most of the time, this can be a little more complicated) and the text needs to compare each digit individually, which can change its greatness depending on its position in the text.

If the objects compared were dates the comparison would probably be numerical as well. Internally dates are numbers, usually it’s the amount of seconds or another time scale from a given time. So it’s pretty easy.

Completion

Your code can give unwanted results easily. When you really need to compare dates or other types of objects like text it’s easy to solve this kind of problem, it’s pure mathematics.

Put the most significant part in front. In date the month must come before the day, since month is something bigger, has more meaning than day. Year would come before month. If it had schedule, it would come after day, and the order would be hour, minute, second and eventually fractional part.

Of course both texts need to be in the same format to work.

Remember that if you have any extra type of formatting, such as the separation bar, you should always be in the same place. Ideally I shouldn’t even have this, if possible.

In this case the biggest problem is the day being in front of the month. If you can guarantee that would always be used a format('m/d') the comparison would even work always.

  • I could understand, thank you. Actually I only asked for doubt. I will not apply anything.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.