I have 2 while loops in an if then else if statement, the code works and all is good, but just a little question.
Take my first example, I declare on the while loop the true statement for it to execute,
if ($levelChange > 0) {
while ($levelChange != 0) {
echo '<p>You gained a level you are now ' . ($levelChange + $user['level']) . '</p>';
$levelChange--;
}
}
elseif ($levelChange < 0) {
while ($levelChange != 0) {
echo '<p>You just lost a level you are now ' . ($levelChange + $user['level']) . '</p>';
$levelChange++;
}
}
And my second example I don’t,
if ($levelChange > 0) {
while ($levelChange) {
echo '<p>You gained a level you are now ' . ($levelChange + $user['level']) . '</p>';
$levelChange--;
}
}
elseif ($levelChange < 0) {
while ($levelChange) {
echo '<p>You just lost a level you are now ' . ($levelChange + $user['level']) . '</p>';
$levelChange++;
}
}
They both work but which one is better to use and why, also if anyone knows which could be faster please enlighten me,
Thanks
PHP converts its arguments to boolean values where a conditional statement is expected. For numbers every non-zero value (
!= 0) evaluates to true. The PHP docs for the boolean type have a table with conversion rules for different types.Therefore,
if($x)is equivalent toif($x == TRUE). There isn’t any (measurable) performance difference between the two forms.