skip to Main Content

This question have a similar question here and here, too. I could have posted this as a comment but I don’t have the privilege yet so I posted this. I have also found similar question with no answer that satisfy my requirements. I will do the codes do the further talking.

I c/c++ you can do like this:

void CallMe(int *p = NULL)
{
    if ( p!= NULL )
        *p = 0;
}

CallMe(); // Calling the function without arguments the function will do nothing. But...

int n;
CallMe(&n);
// Here, you are passing a pointer to your n that will be filled by the CallMe function.
For c/c++ programmer, you know what to expect on those two calls.

Now, I want to do it in PHP. And when I do my searching, the following are most popular methods that are suggested in the search result: isset, is_null and empty. I did my trial-and-error but nothing works like that c/c++ sample.

function CallMe(&$p=null)
{
    if ($p==null)
        return;

    if(!isset($p))
        return;

    if (is_null($p))
        return;

    if (empty($p))
        return;
    $p = time();
    return 'Argument was set to ' . $p;
}

CallMe();

On the first call where no argument is supplied, all argument evaluations will result to true and the function will do nothing. Let’s try with an argument.

$a = 1;
CallMe($a);

Of source, the function will now make some sense out of that $a variable. But…

$a = null;
CallMe($a);

In this last call, the function will behave as if no argument is given.

Now, what I want to achieve is the function to be able to check (like the c/c++ sample) if an argument was supplied. Or, is there a way we could pass a pointer to a variable in PHP?

2

Answers


  1. There is an old function you may call to check the number of function arguments that is actually supplied by user: func_num_args()

    function CallMe(&$p=null)
    {
        if (func_num_args() === 0)
        {
            // no argument supplied. do nothing.
            return;
        }
        $p = time();
        return 'Argument was set to ' . $p;
    }
    
    echo "Try supplying no argument:n";
    echo "---n";
    var_dump(CallMe());
    echo "n";
    
    echo "Try supplying a variable with a value of null:n";
    echo "---n";
    $a = null;
    var_dump($a);
    var_dump(CallMe($a));
    var_dump($a);
    

    Output:

    Try supplying no argument:
    ---
    NULL
    
    Try supplying a variable with a value of null:
    ---
    NULL
    string(30) "Argument was set to 1715217065"
    int(1715217065)
    
    Login or Signup to reply.
  2. The main difference is C / C++ has a distinct pointer type, while PHP does not. From a function perspective, there is no way to distinguish if $p is:

    1. NULL; or
    2. a variable (e.g. $a) with a NULL value

    There used to be run time pass-by-reference in PHP, but it was removed back in PHP 5. Which means the lack of reference / pointer type is probably a design choice of the language. And it’s unlikely to change in the foreseeable future.

    If you have multiple optional value, the closest thing I can think of is to use a value holder object instead of raw variable.

    class Inter
    {
            private int $i;
    
            public function set($val)
            {
                    $this->i = $val;
                    return $this;
            }
    
            public function get(): int
            {
                    if (!isset($this->i)) {
                            throw new Exception('No value is set');
                    }
                    return $this->i;
            }
    }
    
    function CallMe(?Inter $p=null, ?Inter $q=null) {
            if ($p !== null) {
                    $p->set(time());
            }
            if ($q !== null) {
                    $q->set(time());
            }
    }
    
    $a = new Inter();
    $b = new Inter();
    $b->set(1);
    $c = new Inter();
    CallMe(p: $a);
    CallMe(q: $b);
    
    var_dump($a->get());
    var_dump($b->get());
    var_dump($c->get());
    

    In PHP, object instances are ALWAYS passed by reference. Hence there is no need to explicitly add a & to it.

    Output:

    int(1715219664)
    int(1715219664)
    PHP Fatal error:  Uncaught Exception: No value is set in /home/foobar/test.php:16
    Stack trace:
    #0 /home/foobar/test.php(40): Inter->get()
    #1 {main}
      thrown in /home/foobar/test.php on line 16
    

    This is not exactly what you asked for, but this is the closest I can think of.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search