Solved

passing parameter into function

Posted on 2015-02-20
7
71 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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