Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel 9 Undefined variable $variable

I have 2 variables $b and $c defined in an if() function, but when running the function, I keep getting an Undefined variable $b.

public function printPDF()
    {
        /*Treadwear*/
        if ($this->tread_wear == 140) {
            $c = 140;
        } elseif ($this->tread_wear == 0) {
            $b = 120;
        } elseif ($this->tread_wear < 50) {
            $b = 80;
        } elseif ($this->tread_wear < 140) {
            $b = 40;
        } elseif ($this->tread_wear < 201) {
            $b = 20;
        }

        $pdf = new Pdf('/docs/ax_reg_form.pdf');
        $result = $pdf->fillForm([
            ...
            'b' => $b,
            'c' => $c,
            ...
        ])
            ->needAppearances()
            ->saveAs('docs/reg-forms/'.$this->usercar->user->id.'_'.strtolower($this->usercar->user->first_name).'_'.strtolower($this->usercar->user->last_name).'_'.'car_class.pdf');

        // Always check for errors
        /*if ($result === false) {
            $error = $pdf->getError();
        }*/

        session()->flash('url', '/docs/reg-forms/'.$this->usercar->user->id.'_'.strtolower($this->usercar->user->first_name).'_'.strtolower($this->usercar->user->last_name).'_'.'car_class.pdf');

        return redirect()->route('usercar.show', $this->usercar->id);

    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Your declarations of $b and $c are inside if and elseif so only one of them will be declared. Maybe you forget about else statement?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading