hl-hosting.eu

Strict comparisons in PHP methods

Same register, favor strict comparisons: "===" or "! ==". This allows the type match to be included in the comparison. And therefore to compare similar things well.

Strpos returns the position of "Bryan" in $ sentence and will return false if it does not find "Bryan". Since "Bryan" is at the beginning, strpos returns 0. And in php website development, 0 and false, in a Boolean operation, it's the same! So there is no display.

So with “! ==” instead of just “! =”. Here PHP will do a strict comparison, involving the type. And so 0 is no longer the same as false. Again, using the strict comparison 100% of the time, you strengthen and secure your code. Some errors (like the one shown in the example) will also be easier to identify.

Do without the "ifs" I have already talked about doing without else in programming. But it turns out that we can also do without yews! In Advent of Code, it was often a question of loop and counter, let's take the code (simplified)

Depending on whether my condition is met, I must increment by 1. So, I can transform the result of my comparison into an integer and increment it to $ count. true becomes 1, I increment by 1. false becomes 0, I increment by 0, so $ count doesn't change.

Once again, we played with the typing, of course!

The "sscanf" function

Discovering this function changed my life!

In the exercise of day 4, you had to process such strings: "eyr: 2037".

Sscanf therefore makes it possible to describe the format of the character string to be dissected. And with this logic of multiple attribution, you no longer need to manipulate "$ data 0" or the like, which have very little meaning semantically.

Multichange of values One of the first exercises in algorithm is to invert the values ​​of 2 variables, thus creating 1 intermediate variable. If we have to invert the values ​​of + variables, it can become more heavy