aphextwix
aphextwix

Reputation: 1858

Unexpected Result from User Defined Function - PHP

I'm trying to write a simple function which takes two arguments, adds them together and returns the result of the calculation.

Before performing the calculation the function checks whether either of the two arguments are undefined and if so, sets the argument to 0.

Here's my function:

Function - PHP

function returnZeroAdd ($arg, $arg2)
{
    if(!isset($arg))
    {
        $arg = 0;
    }

    if(!isset($arg2))
    {
        $arg2 = 0;
    }

    echo $arg + $arg2;
}

I've tried to execute it like so :

returnZeroAdd($bawtryReturnCount, $bawtryFReturnCount);

But this throws up an undefined variable $bawtryFReturnCount error.

I do not know why the function isn't setting $bawtryFReturnCount) to 0 before performing the calculation thereby negating the 'undefined variable' error.

Can anybody provide a solution?

Upvotes: 0

Views: 69

Answers (5)

vathsala p
vathsala p

Reputation: 26

You have to define the variable before pass it to an function. for example

     $bawtryReturnCount=10;
     $bawtryFReturnCount=5;
   define the two variable with some value and pass it to that function.
     function returnZeroAdd ($arg=0, $arg2=0)
     {

       echo $arg + $arg2;
     }

if you define a function like this means the function takes default value as 0 if the argument is not passed. for example you can call the functio like this returnZeroadd();// print 0 returnZeroadd(4);// print 4 returnZeroadd(4,5);// print 9 or you can define two variables and pass it as an argument and call like this.

     $bawtryReturnCount=10;
     $bawtryFReturnCount=5;
     returnZeroadd($bawtryReturnCount, $bawtryFReturnCount);

Upvotes: 0

Adam Sinclair
Adam Sinclair

Reputation: 1649

If you really want to make the check inside the function you could pass the arguments by reference:

function returnZeroAdd (&$arg, &$arg2)
{
    if(!isset($arg))
    {
        $arg = 0;
    }

    if(!isset($arg2))
    {
        $arg2 = 0;
    }

    echo $arg + $arg2;
}

However this will potentially modify your arguments outside the function, if it is not what you intend to do then you need this:

function returnZeroAdd (&$arg, &$arg2)
{
    if(!isset($arg))
    {
        $localArg = 0;
    }
    else
    {
        $localArg = $arg;
    }

    if(!isset($arg2))
    {
        $localArg2 = 0;
    }
    else
    {
        $localArg2 = $arg2;
    }

    echo $localArg + $localArg2;
}

You can now pass undefined variables, it won't throw any error.

Alternatively you might want to give a default value to your arguments (in your case 0 seems appropriate):

function returnZeroAdd ($arg = 0, $arg2 = 0)
{
    echo $arg + $arg2;
}

Upvotes: 0

GolezTrol
GolezTrol

Reputation: 116110

You cannot do this the way you want. As soon as you use an undefined variable, you will get this error. So the error doesn't occur inside your function, but already occurs in the call to your function.

1. Optional parameters

You might make a parameter optional, like so:

function returnZeroAdd ($arg = 0, $arg2 = 0)
{
    return $arg + $arg2;
}

This way, the parameter is optional, and you can call the function like this:

echo returnZeroAdd(); // 0
echo returnZeroAdd(1); // 1
echo returnZeroAdd(1, 1); // 2

2. By reference

But I'm not sure if that is what you want. This call will still fail:

echo returnZeroAdd($undefinedVariable);

That can be solved by passing the variables by reference. You can then check if the values are set and if so, use them in the addition.

<?php
function returnZeroAdd (&$arg, &$arg2)
{
    $result = 0;
    if(isset($arg))
    {
        $result += $arg;
    }

    if(isset($arg2))
    {
        $result += $arg2;
    }

    return $result;
}

echo returnZeroAdd($x, $y);

Note that you will actually change the original value of a by reference parameter, if you change it in the function. That's why I changed the code in such a way that the parameters themselves are not modified. Look at this simplified example to see what I mean:

<?php
function example(&$arg)
{
    if(!isset($arg))
    {
        $arg = 0;
    }

    return $arg;
}

echo example($x); // 0
echo $x // also 0

Of course that might be your intention. If so, you can safely set $arg and $arg2 to 0 inside the function.

Upvotes: 2

Zar
Zar

Reputation: 6882

Your function could be rewritten as:

function returnZeroAdd ($arg = 0, $arg2 = 0)
{
    echo $arg + $arg2;
}

You missunderstand how variables work. Since $bawtryFReturnCount isn't defined when you call the function; you get a warning. Your isset-checks performs the checks too late. Example:

$bawtryReturnCount = 4;
$bawtryFReturnCount = 0;

returnZeroAdd($bawtryReturnCount, $bawtryFReturnCount);

Will not result in an error.

Upvotes: 0

Ivan Yonkov
Ivan Yonkov

Reputation: 7034

The error is not thrown by the function itself, as the function is not aware of the global scope. The error is thrown before even the function is executed, while the PHP interperter is trying to pass $bawtryFReturnCount to the function, one does not find it, and throws error, however, it's not a fatal one and the execution is not stopped. THerefore, the function is executed with a non-set variable with default value of null, where I guess, isset will not work, as the arguments are mandatory, but not optional. A better check here will be empty($arg), however the error will still be present.

Because the functions are not and SHOULD NOT be aware of the global state of your application, you should do these checks from outside the functions and then call it.

if (!isset($bawtryReturnCount)) {
    $bawtryReturnCount = 0
}

returnZeroAdd($bawtryReturnCount);

Or assign default values to the arguments in the function, making them optional instead of mandatory.

Upvotes: 0

Related Questions