Why does not check for emptiness?

make a request to the database, so as not to describe the whole answer, it just gives out the number, if it is then it displays
the problem is that in the case of unsuccessful search, when there are no suitable objects to display the numbers , nothing prints, and you should print "no records" or "0"
doing such a test
if ($data['name'] == $value9['name']): ?>
 <?php if ($value9['cnt'] == "") {
 echo "0";
 } else {
 echo $value9['cnt'];
 } ?>

but does not display 0 , nothing at all displays , also with empty($value9['cnt']) and $value9['cnt'] < 0
what could be the problem?
April 3rd 20 at 17:27
3 answers
April 3rd 20 at 17:29
echo (((isset($value9['cnt'])) AND ((int)$value9['cnt'] > 0)) ? $value9['cnt'] : '0');
Me, a ternary entry if constantly scares the hell out of his recitement. If the development is not the main profile and additional third-party tool, then coming back to it every couple of months completely forget the logic of construction of the line, blunt, Google, break it into components...

May have to human readable cheatsheet for all occasions or a versatile way to print large and hang on a wall in front of the eyes? - Niko.Mull commented on April 3rd 20 at 17:32
@Niko.Mull, frankly, do not understand what this entry may scare, especially in the scale of projects of this time) if in short - put yourself "if ? true : false" - Salma commented on April 3rd 20 at 17:35
@Niko.Mullwhat to think, it is read as text: the condition is met? Yes : no; - jannie.Johns commented on April 3rd 20 at 17:38
@jannie.Johns, on easy-to-follow examples, maybe. And in the middle of the code when the conditions of five, and true and false are themselves complex odnotrahniki... And if the nested conditions... And if the attachments are my blocks with colons/question marks...

Here, the savings on three line breaks in a classic if...else I do not understand. - Niko.Mull commented on April 3rd 20 at 17:41
@Niko.Mull, here it is necessary to understand when to use the short entry, and when not - Salma commented on April 3rd 20 at 17:44
@Niko.Mull, so it basically is a simple odnotrahniki, where the letters save on herodotou the logic of this is from the evil Pinocchio. It mainly bring in for the condition to toe with empty and the value of the variable with the existence of suitable, for him even 7 additional syntax is shortened - $a ?? 'b'; as is common, the Hughes case. - jannie.Johns commented on April 3rd 20 at 17:47
April 3rd 20 at 17:31
It is necessary not to suffer, and to think logically.

nothing prints, and you should print "no records" or "0"

If the condition if ($value9['cnt'] == "") fails, this means that $value9['cnt'] is not an empty string or similar value.
To find out what zracenje really need to do var_dump()
where there is something , is a string of numbers , but if not then there is a completely empty - Erick36 commented on April 3rd 20 at 17:34
@Erick36, I mean, you don't know what there lies in fact, but continue to assert that there is "absolutely empty." Well, OK. Torture continue - Shanna.Cummings27 commented on April 3rd 20 at 17:37
April 3rd 20 at 17:33
1) Learn to use var_dump, right here "in any unclear situation - do vardump".

2) use the syntax if(): ... endif - moveton, he in addition to excessive, and not all IDA normally perceive, and the visualization is broken. In short, tip of the day - do not use. And you have it is still not closed.

3) what gives you the opening / closing tags in each row? Or is it purely aesthetic pleasure of looking at bleeding eyes colleagues reading your code?

4) if ($value9['cnt'] == "") this is not a test for emptiness, although in most cases it will work, but it is definitely bad code, which will be rolling into a log GTA V on garage door for these inspections is empty();

5) if ($data['name'] == $value9['name']) won't work if $value9['name'] is empty, respectively, ALL of the code inside this test will not work.
2) But when there is a mixture of hypertext markup language pykhy and, separating conditions, loops, thus much clearer than brackets, which visually catch there "somewhere in the thread below" already harder - Lourdes.Mayert71 commented on April 3rd 20 at 17:36
amendment. for "such checks" is if (!$value9['cnt'])
empty() used to checks if (isset($value9['cnt']) && !$value9['cnt']) - Shanna.Cummings27 commented on April 3rd 20 at 17:39
@Lourdes.Mayert71,
which visually catch there "somewhere in the thread below" already harder
IDA is well aware of the "bracket" blocks and correct their moves, as well as highlight the beginning/end of block for the braces, but does this alternate syntax. The choice is obvious. - jannie.Johns commented on April 3rd 20 at 17:42
@Shanna.Cummings27, checking if(!$value9['cnt']) is generally acceptable ONLY in case if the value is Boolean and known in advance what it exactly is (and just Boolean). In this case, it nifiga not clear, at this empty(). - jannie.Johns commented on April 3rd 20 at 17:45
No. empty() is irrelevant to the coercion of types. but only to check for variable existence.
I understand the logic behind this statement, but it is not true. empty() implicitly casts the types likewiseas it does if($var) or if ($var == ""). That is, there is simply added a meaningless call. - Shanna.Cummings27 commented on April 3rd 20 at 17:48
@Shanna.Cummings27, there is a difference, and in this case the logic is very clear - the variable may not exist, and can have the value cast to a Boolean. In the case of not existing we at least get out of warning if we didn't empty. - jannie.Johns commented on April 3rd 20 at 17:51
You are absolutely right. But it is quite another question which has nothing to do with checking for an empty value which the author - Shanna.Cummings27 commented on April 3rd 20 at 17:54
@Shanna.Cummings27don't understand what the dispute. The author incorrectly checked the value, in fact it checks "what is in variable" instead of "if variable is not empty it". So check genovo in all senses. Including if($var) will give warning . - jannie.Johns commented on April 3rd 20 at 17:57
Okay, maybe we're just different I understand the task of the author.
just empty should be used not at all always, but only if zavedomo it is known that the variable may not exist. - Shanna.Cummings27 commented on April 3rd 20 at 18:00

Find more questions by tags PHP