Kinderly Wade
asked on
passing parameter into function
Dear experts,
I have a function in php that looks like this:
function getPwd ($type = 'ADMIN', $user = 'ADMINISTRTOR')
{
-- mysql connection to Datatbase and fetch necessary info in order to return the results back
return $result;
}
--- here is my function call:
getPwd('USER', 'ADAM'); --> returns the password for user Adam
getPwd(); --> returns the password for admin Administrator
My question is that what if I set the variables to getPwd fuction like this?
getPwd($inputType, $userName);
I wish to know how can I still get the return type of administrator's password. If I pass two variables as null, I will not get the default value as in 'ADMIN' or 'ADMINISTRATOR'. Is there a way for me to use the above function and still get the correct return from the function with the default input value as 'ADMIN' and 'ADMINISTRATOR'? Thanks
I have a function in php that looks like this:
function getPwd ($type = 'ADMIN', $user = 'ADMINISTRTOR')
{
-- mysql connection to Datatbase and fetch necessary info in order to return the results back
return $result;
}
--- here is my function call:
getPwd('USER', 'ADAM'); --> returns the password for user Adam
getPwd(); --> returns the password for admin Administrator
My question is that what if I set the variables to getPwd fuction like this?
getPwd($inputType, $userName);
I wish to know how can I still get the return type of administrator's password. If I pass two variables as null, I will not get the default value as in 'ADMIN' or 'ADMINISTRATOR'. Is there a way for me to use the above function and still get the correct return from the function with the default input value as 'ADMIN' and 'ADMINISTRATOR'? Thanks
But let me ask: why do you should call a function with fake parameters?
Anyway, if $userType and or $userName are empty you'll still get administrator password; if they are unset you'll get a notice about an undefined variable.
Personally, I'd do the check in the script instead of within the function:
Anyway, if $userType and or $userName are empty you'll still get administrator password; if they are unset you'll get a notice about an undefined variable.
Personally, I'd do the check in the script instead of within the function:
if (!isset($userType))
{
$userType = '';
}
if (!isset($userName)
{
$userName = '';
}
getpwd($userType, $userName);
Check if the variables are empty within the function. if you do this, then you might as well remove the default values in the arguments - they'll serve no purpose:
This does seem like a security risk - calling a generic function to get an admin password from the database!
function getPwd ($type = '', $user = '')
{
if(empty($type)) $type="ADMIN";
if(empty($user)) $user="ADMINISTRATOR";
...
}
This does seem like a security risk - calling a generic function to get an admin password from the database!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi all,
Is it possible that I can pass an empty value into the function which will give me the default arguments? (Default arguments are 'ADMIN' and 'ADMINISTRATOR'). I've tried this:
$type;
$user;
function getPwd($type, $user);
and get the return pwd of 'ADMINISTRATOR' as return result?
Hi Ray, nice to see your post and thanks Chris and Marco for the posts.
Is it possible that I can pass an empty value into the function which will give me the default arguments? (Default arguments are 'ADMIN' and 'ADMINISTRATOR'). I've tried this:
$type;
$user;
function getPwd($type, $user);
and get the return pwd of 'ADMINISTRATOR' as return result?
Hi Ray, nice to see your post and thanks Chris and Marco for the posts.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Not to polemize, but what's the difference between my comment ID 40621743 end the accepted one ID 40622393? I'm missing something...
Open in new window