0

i have this situation"

foreach($front->getRequest()->getParams() as $key => $value){
  if ($value == '1'){
  $$key = $value;  
  }
}
echo $test1; // test1 = 1
echo $test2; // test2 = 1
....

this will give me back one or more $test = 1 where the $$key = $test and $value = 1

i want to see how many actually come back. and i was thinking to do something like: print_r(count($key)) or print_r(count($value)) but it doesn't tell me how many results are there

any ideas?

thanks

4
  • Never ever do anything like variable variables :( This is just bad. Use arrays for such things. Commented Nov 1, 2011 at 20:58
  • 1
    Variable variables have their place. Though the keys should be verified or prefixed or something before being used like this. Otherwise you're just reinventing register_globals, without even the little protection PHP offers with it. Commented Nov 1, 2011 at 21:02
  • 3
    example.com?_POST=hahaha_Im_in_ur_phps_messing_with_yur_codez!... boom goes your site. Commented Nov 1, 2011 at 21:03
  • i am using zend, he is protecting me :) Commented Nov 1, 2011 at 21:14

1 Answer 1

2

Why not just keep a counter?

$count = 0;
foreach($front->getRequest()->getParams() as $key => $value){
    if ($value == '1'){
        $$key = $value;  
        $count++;
    } 
}
echo $count;
Sign up to request clarification or add additional context in comments.

3 Comments

success, so simple, i am familiar with this method and I've used a lot, just nu coffee today i guess :)
:) Do keep an eye out for the security issues everyone is mentioning though.
i am using zend, he is protecting me :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.