Page 1 of 1

arrays question - safe code?

Posted: Thu Apr 08, 2010 3:34 am
by PitBullDogo
pickle | Please use [ syntax=php ], [ syntax=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hello all! I'm new and have a question, please bear with me (and forgive my English)... :)

I have a function that takes an array as an argument, and checks an element in an array, eg:

Code: Select all

function foo($arr)
{
     if($arr['num']==5)
     {DoSomething();}
     else
     {DoSomethingElse();}
}
A need arose to use the same functionality but while passing a variable containing a single number into the same function.
I used the same function in the same way, passing a variable, and it works OK, as if php say "OK, you're not an array, and you don't have a member with the key 'num', so you can't give me that - give me what you've got, then?" - and it gives the appropriate value.

I am curious whether this is a valid way to use a function in pHp? I am essentially a C++ programmer and this is, of course, unthinkable in C++, is it allowed in pHp? I could of course write a new function, like this:

Code: Select all

function fooVal($val)
{
     if($val==5)
     {DoSomething();}
     else
     {DoSomethingElse();}
}
which would soothe my mind, but I'm not happy with having two functions doing essentially the same thing, if I can get away with only one?

Please help! :)


pickle | Please use [ syntax=php ], [ syntax=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: arrays question - safe code?

Posted: Thu Apr 08, 2010 3:49 am
by Christopher
You could do something like:

Code: Select all

function foo($val)
{
if (is_array($val)) {
    if (isset($val['num'])) {
         $val = $val['num'];
    } else {
        // error
    }
}
if($val==5)
{DoSomething();}
else
{DoSomethingElse();}
}

Re: arrays question - safe code?

Posted: Thu Apr 08, 2010 5:03 am
by PitBullDogo
Hehey,good idea - didn't think of that! :) It's exactly what I'll do! :)

I'd still like to know - is such interchangability "allowed" in pHp?

Re: arrays question - safe code?

Posted: Thu Apr 08, 2010 11:58 am
by Christopher
PitBullDogo wrote:I'd still like to know - is such interchangability "allowed" in pHp?
Sure ... within the bounds of good programming design. It would probably be best to step back and see why you can pass $arr['num'] into a function that takes a scalar. So perhaps the problem should be solved outside this function.

Because in PHP variables are all essentially strings, arrays or objects, there is more if this kind of type checking than in languages with strict typing (and syntax to deal with typing).

Re: arrays question - safe code?

Posted: Thu Apr 08, 2010 12:05 pm
by AbraCadaver
It doesn't work so "allowed" is irrelevant:

Code: Select all

function foo($arr)
{
   if($arr['num'] == 5) {
      echo 'yes';
   } else {
      echo 'no';
   }
}
foo(5);
Outputs: no

I would have expected a "Notice: Undefined index" but I didn't get one.

Re: arrays question - safe code?

Posted: Fri Apr 09, 2010 3:45 am
by PitBullDogo
It won't work if you do foo(5),
try:

$val = 5;
foo($val);

I fixed it as suggested above, still works! Thanks everyone - I will steer clear from such stuff. :) C++ programming makes one "stiff", but I guess I'd rather be safe than sorry...