Checking Variables!
Moderator: General Moderators
Checking Variables!
Hello!
I have a group of variables and I have to check if they are !=0:
$ene1t
$ene2t
$ene3t
$ene4t
$ene5t
$feb1t
$feb2t
$feb3t
As soon as one condition is true (!=0) I want to take the value of this one, doesn't matter if more of them matches the condition, I only need the value of the first one.
How can I arrange that?
I have a group of variables and I have to check if they are !=0:
$ene1t
$ene2t
$ene3t
$ene4t
$ene5t
$feb1t
$feb2t
$feb3t
As soon as one condition is true (!=0) I want to take the value of this one, doesn't matter if more of them matches the condition, I only need the value of the first one.
How can I arrange that?
This can show us if a variable matches condition, but what I need is to echo the the first variable that matches. (It doesnt matter if 5 variables matches or not, just the first one that does)
Code: Select all
<?php
$variables = array($ene1t, $ene2t, $ene3t, $ene4t, $ene5t, $feb1t, $feb2t,$feb3t );
for ($i =0; $i < count($variables); $i++)
{
if (!is_null($variables[$i]))
{
echo "some stuff";
break();
}
}
?>You can try counting backwards:
Code: Select all
<?php
$variables = array($ene1t, $ene2t, $ene3t, $ene4t, $ene5t, $feb1t, $feb2t,$feb3t );
for ($i = count($variables)-1; $i >= 0; $i--)
{
if ($variables[$i]!=0)
{
$result= $variables[$i];
}
}
echo $result;
?>Note the difference between if($var != 0) and if($var !== 0). Nulls, boolean false, "integer strings", and an empty string could give false positives.
Code: Select all
$var = 1; // both true
$var = 0; // both false
$var = '0'; // != false, !== true (eg POST values are string type)
$var = ''; // != false, !== true
$var = true; // both true
$var = false; // != false, !== true
$var = null; // != false, !== true
Last edited by McGruff on Tue Aug 09, 2005 4:56 pm, edited 2 times in total.