0

I have the following snippet:

// Some post is made and I have access to it using Input::get('name_of_field');
if (Input::has('optionalField'))
{
    $thisVariable = myMagicalFunction();
}

// more operation
// being performed

ultimateFunction($thisVariable);

return true;

As you can see, $thisVariable may sometimes not be set, and so the ultimateFunction($thisVariable) may sometimes return error saying the variable is undefined. I can of course use

if (isset($thisVariable)) ultimateFunction($thisVariable);

But is there a way to force pass the variable anyhow without checking if it is set? I will then check if it is set inside ultimateFunction.

4 Answers 4

4

The simplest way is to just declare the variable before it is used:-

$thisVariable = null;
if (Input::has('optionalField'))
{
    $thisVariable = myMagicalFunction();
}

// more operation
// being performed

ultimateFunction($thisVariable);

return true;
Sign up to request clarification or add additional context in comments.

2 Comments

I know, I wanted to skip that one line! I guess I shouldn't then.
@Kousha you definitelly shouldn't. The best approach is to create PHP code that have no warnings or notices.
0

You can suppress the error with @, so:

ultimateFunction(@$thisVariable);

But it's better to check first. I personally use @ all the time. It's bad practice.

1 Comment

I down voted this for recommending the use of error suppression. It is bad practice and worse advice.
0

It is at best a hack to suppress your error this way. You are ignoring warnings and writing code which is not optimized. Even though it saves you a bit of time, I strongly suggest you avoid doing this.

Ideally, create a service container for $_GET and $_POST which performs this check for you. It can return null if it does not exist.

Alternatively, a procedural answer is something like this:

function formGet($input) {
    if(isset($_GET[$input])) {
        return $_GET[$input];
    }
    return false;
}

Now you can simply input your data using formGet($input).

Comments

-1

It is possible to use PHP's error control operator (at-sign):

// ...
ultimateFunction(@$thisVariable);

There will be null force-passed as the ultimateFunction's first argument and inside function you will be able to normally check with isset() or empty() whether variable was passed.

But if it is possible, that does not mean you should use it. As @degenerate said too, it is really bad practice, because PHP parser expands the code above using @ operator to code snippet like following:

$old = error_reporting(0);
ultimateFunction($thisVariable);
error_reporting($old);

Additionally use of this operator might slower the application.

6 Comments

I down voted this for recommending the use of error suppression. It is bad practice and worse advice.
@vascowhite I have edited the post adding mention about it. Don't it additionally slower application, too?
Pointing it out as bad practice does not make the recommendation to use it any better.
@vascowhite And how can I edit the post to not be a recommendation of best practice? May I prepend Use with You can?
In my opinion it should not be offered as a solution at all as it isn't really. The error is still there, it is just suppressed. I think that this answer and the other saying the same should be deleted.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.