Link to home
Start Free TrialLog in
Avatar of Kinderly Wade
Kinderly WadeFlag for United States of America

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
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

function getPwd ($type = 'ADMIN', $user = 'ADMINISTRTOR')
{
     if(empty($type))
    {
         $type = 'ADMIN';
    }
     if(empty($user))
    {
         $user = 'ADMINISTRATOR';
    }
    -- mysql connection to Datatbase and fetch necessary info in order to return the results back
     return $result;
}

Open in new window

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:
if (!isset($userType))
{
$userType = '';
}
if (!isset($userName)
{
$userName = '';
}
getpwd($userType, $userName);

Open in new window

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:

function getPwd ($type = '', $user = '')
{
   if(empty($type)) $type="ADMIN";
   if(empty($user)) $user="ADMINISTRATOR";
   ...
}

Open in new window


This does seem like a security risk - calling a generic function to get an admin password from the database!
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kinderly Wade

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Not to polemize, but what's the difference between my comment ID 40621743 end the accepted one ID 40622393? I'm missing something...