Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pickab00's avatar

Can not use variable outside of If statement

My variable can not be used outside of the if statement. Here is my controller:

public function getStuff(){

    if ($insert->free_meal == 1){
                $push = 'Guest is also eligible for a Free Meal';
        }

    return $push;   

}

It is giving me variable not found

0 likes
16 replies
jlrdw's avatar

Uou are not passing anything to function here:

public function getStuff(){

The function don't have a clue about

$insert->free_meal
pickab00's avatar

"Undefined variable: push"

Thats all I can Give you really because that is the specific error. Basically I am trying to use the if statements variable outside of the statement but which is inside of the method.

ftiersch's avatar

As @jlrdw said, your if condition is never executed so $push is never defined.

Define an empty variable $push = null before your if and it should work.

pickab00's avatar

@JLRDW - I forgot to paste that code in. Sorry. Here it is.

public function getStuff(Request $request){

//Do some savings of data here
$insert = new Stuff;
$insert->free_meal = $request->free_meal;

$insert->save();

    if ($insert->free_meal == 1){
            $push = 'Guest is also eligible for a Free Meal';
    }

return $push;   

}

So after the save, I am trying to access that data using $insert->free_meal

artcore's avatar

You should always initialize your variables and use strict comparisons === Personally I leave out {} if it's only one statement, but it's personal.

public function getStuff(){

    $push = '';

    if ($insert->free_meal === '1')
        $push = 'Guest is also eligible for a Free Meal';

    return $push; 
}
pickab00's avatar

Ok so for clearence here is the full code sample of what I am trying to do

public function getStuff(Request $request){

//Do some savings of data here
$insert = new Stuff;
$insert->free_meal = $request->free_meal;

$insert->save();

if ($insert->free_meal == 1){
        $push = 'Guest is also eligible for a Free Meal';
    }

$username = '121222121';
$password = '121222121';

$client = new \SmsSender($username, $password);
$message = $client->send(
        '0000000000', //This is where the mobile number goes
        $push // Message
    );

}

I hope this clears the confusion. I am trying to take that variable in to something else which is down below. I am trying to send that variable as the message inside the message sender.

ftiersch's avatar
ftiersch
Best Answer
Level 28

This isn't about scope since the variable is defined inside the same function.

The if condition is still not executed and you need to find out why.

Based on that code I would guess that $request->free_meal is not actually 1. Or it isn't saved into the $insert object.

I'd put a dd($insert->free_meal); right in front of the if condition to check it's actual value.

That said: It would still be a good idea to initiate the $push variable somewhere above since it makes it easier to read your function and what it is supposed to do.

1 like
pickab00's avatar

@FTIERSCH - Actually you are right. It was a dumb mistake on my side. it was not returning 1 because I had a small issue while adding the free_meal to the db. So nothing was being passed. That was the whole issue here. Thanks!

jlrdw's avatar
if (request->inputfree_meal == 1){
1 like
Cronix's avatar

@pickab00 You still should define $push before your if() check to be proper.

$push = '';

if (some_condition) {
    $push = 'something else';
}

$push will now be defined whether or not "some_condition" was true.

1 like
pickab00's avatar

@CRONIX - Yes I realized that a bit later. That was a mistake on me. Thanks!

rawilk's avatar

@pickab00 you should still initialize $push before the conditional like many others above have told you to. If that conditional fails you're going to run into the same issues with the variable not being defined. But if the conditional always evaluates to true, then why do you even have it?

Please or to participate in this conversation.