I have the following code from a tutorial however even after looking at a couple of tutorials I can’t completely understand the exact workings of it despite it being extremely basic.
if(mysql_num_rows($result)) {
while($term = mysql_fetch_assoc($result)) {
$terms[] = array('term'=>$term);
}
}
I can’t get my head behind what the IF statement is actually testing here. I know that the MySQL_num_rows function is counting the number of rows returned by my database query and returns an integer (in the case of this query there is only a single row), but what does that mean for the IF statement. I’m assuming it’s testing to make sure that it is not null, is this correct? Even if it is what is the exact logic behind this.
Sorry for the beginners question, just starting out.
In an
ifstatement, the value is converted to a boolean before it checks.So,
mysql_num_rowsreturns a number. If it’s0, it gets converted tofalse, and1(or higher) becomestrue. Then theifruns or not depending on the boolean conversion.From the PHP docs:
When converting to boolean, the following values are considered
FALSE:Every other value is considered
TRUE(including any resource).