Solved

passing parameter into function

Posted on 2015-02-20
7
70 Views
Last Modified: 2015-02-20
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
0
Comment
Question by:Kinderly Wade
7 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 40621716
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

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 40621743
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

0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40621969
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!
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 40622349
From the sound of this question, I think you might want to step back from this issue and learn about basic PHP security and PHP client authentication.  If you write a script that can get a password, you're putting your clients at risk of having their passwords exposed!

You can provide default values for any function arguments in the function definition.
http://iconoun.com/demo/temp_kinderly_wade.php

<?php // demo/temp_kinderly_wade.php

/**
 * Show how to provide default values for a function definition
 * SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28621260.html
 */
error_reporting(E_ALL);


function show_arguments($arg1='DEFAULT VALUE', $arg2='ANOTHER DEFAULT VALUE')
{
    echo PHP_EOL . $arg1;
    echo PHP_EOL . $arg2;
}

// WITH SPECIFIED ARGUMENTS
show_arguments('Kinderly', 'Wade');
echo '<br>';

// WITH DEFAULT ARGUMENTS
show_arguments();
echo '<br>';

// WITH A MIX OF ARGUMENTS
show_arguments('Kinderly');
echo '<br>';

Open in new window

0
 

Author Comment

by:Kinderly Wade
ID: 40622388
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.
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 250 total points
ID: 40622393
Look at the code I've already posted. Set your variables to an empty string and then pass them in:

$type = '';
$user = '';

$password = getPwd($type, $user);

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 40622678
Not to polemize, but what's the difference between my comment ID 40621743 end the accepted one ID 40622393? I'm missing something...
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question