arrays question - safe code?

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
PitBullDogo
Forum Newbie
Posts: 3
Joined: Thu Apr 08, 2010 3:25 am

arrays question - safe code?

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: arrays question - safe code?

Post 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();}
}
(#10850)
PitBullDogo
Forum Newbie
Posts: 3
Joined: Thu Apr 08, 2010 3:25 am

Re: arrays question - safe code?

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: arrays question - safe code?

Post 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).
(#10850)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: arrays question - safe code?

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
PitBullDogo
Forum Newbie
Posts: 3
Joined: Thu Apr 08, 2010 3:25 am

Re: arrays question - safe code?

Post 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...
Post Reply