Mariano
Mariano

Reputation: 221

best way to pass a variable number of arguments to a php function

What is the best way to pass a variable number of arguments to a php function? I mean, suppose i have the following:

function my_func($a, $b, $c) {
    $q = 'SELECT ' . $a . ' FROM ' . $b . ' WHERE status IS NULL';
}

my_func('id', 'table');
my_func('id', 'table', ' AND x = 1');

I've read about func_get_arg(), but if i call func_get_arg(2) in the first situation, i will get a, Argument 2 not passed to function error.

IMPORTANT: this query is not performed with user-passed arguments, so no injection hazzards! It is performed with controlled arguments given by me and its function is to check if that value is valid within a combination of foreign keys! So please no sarcastic 'injection paradise' comments, thank you.

Upvotes: 2

Views: 119

Answers (2)

arma
arma

Reputation: 4124

Well i do not know if it's best, but i like to pass the array as argument and then work with it in my function. Here is one example:

function my_query($query = array())
{
    // select and from are required to exist
    if(!empty($query) && array_key_exists('select', $query) && array_key_exists('from', $query))
    {
        $q  = "select {$query['select']}";
        $q .= " from {$query['from']}";

        foreach($query as $key => $val)
        {
            // Don't want to include select and from once again (also do not unset before in case need to run all of this once again)
            if($key != 'select' && $key != 'from')
            {
                // Search if key has underscore and replace it with space for valid query
                if(strpos($key, '_') !== false)
                    $key = str_replace('_', ' ', $key);

                // Build query with spaces and all
                $q .= " " . $key . " " . $val;
            }
        }

        // Run query here using $q
    }
}

And you can pass in array as you like:

$query = array(
    'select'    => '*',
    'from'      => 'users',
    'where'     => 'age > 25',
    'order by'  => 'id'
);

// Or 
$query = array();

$query['select']    = '*';
$query['from']  = 'users';
$query['where'] = 'age > 25';
$query['order_by']  = 'id';

my_query($query);

// Would return us something like this
string(46) "select * from users where age > 25 order by id"

But using this you have to maintain right order in your array or write ordering and validation code in your function.

Upvotes: 2

Since you have mentioned that your function does not deal with user-passed arguments.. I am suggesting this..

FYI : I just used an echo inside that for demonstration purposes.. you can change that later.

<?php
function my_func() {

    echo $q = 'SELECT ' . func_get_arg(0) . ' FROM ' . func_get_arg(1) . ' WHERE status IS NULL';
}

my_func('id', 'table');

The above displays...

SELECT id FROM table WHERE status IS NULL

The arguments start from 0 index, so you should probably do.. func_get_arg(1) to get the second argument.

Upvotes: 1

Related Questions