tags:

views:

209

answers:

6

there are 10 variables. say $var1, $var2, $var3, $var4,....$var10

and a $count variable. what I am looking for is if all variables are set then $count = 10+1 or if 9 variables only set then $count=9+1 or if 8 variables only set then $count=8+1 and so on last up to 1 variable(for one variable is set then $count = 1+1).

I know do this with If, Else if and else but I need to write too much line of code.

Does any one know how to do this in brief code??

+3  A: 

Try this (untested):

$arr = array($var1, $var2, $var3, $var4, $var5, $var6, $var7, $var8, $var9, $var10);
$count = 1;
foreach ($arr as $v)
{
    if (isset($v))
        $count++;
}
Graphain
But where do you check if this variables are set or not??
mathew
Um `isset($v)` is my guess
PostMan
@mathew: See the call to `isSet`?
Borealid
``$var1, $var2,...``might not have been initialized. You can't put them into an array.
Willi
@Willi: sure you can. An array entry can be null, last i heard. Now, it may trigger a warning, so if you don't like that, yeah. Could cause a slight problem.
cHao
+2  A: 
<?php
  ...
$countarr = compact('var1', 'var2', ..., 'var10');
$count = count($countarr) + 1;
?>
Ignacio Vazquez-Abrams
+11  A: 

You can achieve this using variable variables in PHP:

$count = 1;
for ($i = 1; $i <= 10; $i++)
{
    if ( isset( ${'var'.$i} ) ) $count++;
}
Anax
Wow, I had no idea that any language could do that. Interesting
Falmarri
If I didn't know any better, I'd consider this more of a bug than a feature. :)
musicfreak
@musicfreak: http://stackoverflow.com/questions/234075/what-is-your-best-programmer-joke/234170#234170
Anax
Argh, variable variables - Slightly Less Evil Than Eval (TM).
Piskvor
where is the closing bracket?? getting syntax error, unexpected T_VARIABLE
mathew
Fixed now, thank you for pointing this.
Anax
A: 

Er... Are you aware that PHP allows to use loops...

<?php

$var1 = 100;
$var4 = 200;
$var9 = 600;

$count = 0;
for($i=1; $i<=10; $i++){
    if( isset(${"var$i"}) ){
        $count++;
    }
}

echo $count;

?>

... and arrays?

<?php

$var = array(100, 200, 600);
$count = count($var);
echo $count;

?>

Answer to Boreadlid's remarks

You should note that unsetting an array element is not the same as setting it to NULL. This code:

<?php

$data = array(10, 20, 30);
$data[1] = NULL;
unset($data[0]);
var_dump($data);

?>

... prints this:

array(2) {
  [1]=>
  NULL
  [2]=>
  int(30)
}

... so count($data) prints 2 because it is a two element array.

Whatever the OP is trying to accomplish, he should probably be using arrays where unused values are never set to begin with. The $var1, $var2, $var3 approach will work but it's not what a skilled programmer would do. (Of course, there's nothing wrong in being a newbie.)

Álvaro G. Vicario
I don't think `count` does what you think it does.
Borealid
@Borealid: So it doesn't return the number of elements in the array?
Álvaro G. Vicario
@Alvaro: it does return the number of elements in the array. But it will include ones which are `NULL`, or `false`. Or otherwise "unset".
Borealid
@Borealid: Why should values set to NULL or FALSE not be count as array items?
Álvaro G. Vicario
@Alvaro: Because in this particular problem, the author was looking for a way to determine if variables were set.
Borealid
I've just updated my answer.
Álvaro G. Vicario
A: 

oh c'mon … you can do a little bit of thinking (and reading documentation) yourself.

simply take the code answered to your previous question, and use isset(${var+$i}, ${tvar+$i}) in your loop

knittl
well, this answer seems now out of place. this is because this question was merged with a second (very similar) question, to which this answer fit perfectly
knittl
A: 

you have 20 vars (10x "var" and 10x "tvar"):

bool[] vars = new bool[10];
bool[] tvars = new bool[10];

i don't get your +1 part in $count... but what about:

int count = 1; //includes the always needed +1 part
for(int i=0;i<10;i++)
   if(vars[i] && tvars[i])
      count += i+1; //so 0 => 1

??

santa
that is not a valid php code
Sarfraz
this will do: 1, 2, 4, 7, 11, … (you probably meant `$count += 1`) and this is php, not c++ ;)
knittl
santa