Checking Variables!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Checking Variables!

Post by Perfidus »

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?
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post by Dr Evil »

Will this list of variables get any longer ?
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

as soon as I get the value of the first that matches condition, I do not need them anymore.
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post by Dr Evil »

What I mean is will there be a :
$mar1t
$mar2t
$mar3t
$mar4t
$abr1t
$abr2t
$abr3t
$abr4t
...

?
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

sounds like a good use for an array instead of lots of variables.
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post by Dr Evil »

I havent found a answer yet, but that is the way I was going, yes. Do you have a suggestion ?
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

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(); 
  } 
}
?>
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post by Dr Evil »

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;
?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

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.
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post by Dr Evil »

Thanks McGruff.

I discover something interesting here everyday !!!
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

That level of precision isn't necessarily required - depends what the range of possible values actually are.

$_POST can easily catch you out though - everything is a string even if it's a number...
Post Reply