arrays question - safe code?
Posted: Thu Apr 08, 2010 3:34 am
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:
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:
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:
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:
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();}
}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();}
}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: