sazr
sazr

Reputation: 25928

Blade PHP Variable undefined in Local Laravel Project but not Production?

I have a very weird error in my Laravel project. Some code in my Blade view file runs perfectly on the server but fails on my local project VM.

The simple code is:

<?$class = 'abc';?>
<p><?=$class?></p> /* My local project fails here */

On my local Laravel project I get the error:

exception 'ErrorException' with message 'Undefined variable: class

Why the difference? I'm really curious to know why this would work on the server but fail locally.

Server Setup:

Local Setup:

Edit: After Steve's advice. If I change the code to the below it works. Whats going on?

<?php $class = 'abc';?>
<p><?=$class?></p

Upvotes: 1

Views: 692

Answers (3)

Bashar Gh
Bashar Gh

Reputation: 57

I had the same issue and finally I discovered the issue after half day of work. check the component folder should start with uppercase!

Upvotes: 0

Steve
Steve

Reputation: 20469

To expand my comment into an answer, <?$class = 'abc';?> could be the issue, <? is a short open tag, which are not enabled by default (not to be confused with <?= which is a short echo tag, and is enabled by default).

If this is the case, then you should edit your php.ini with:

short_open_tag = On

to match your live environment, or replace all instances of <? (thats including the trailing space so you dont catch the perfectly valid <?=) with <?php throughout the project.

generally short open tags are disabled because they can cause issues reading xml, so i would personally opt for the second option.

Either way be consistent.

Upvotes: 2

Alexander Reznikov
Alexander Reznikov

Reputation: 1268

May be you cannot use short tag <? on your local environment. Check short_open_tag value in your local php.ini file.

From documentation: PHP also allows for short open tag

http://php.net/manual/en/language.basic-syntax.phptags.php

Or just use

<?php $class = 'abc'; ?>

instead of

<?$ $class = 'abc'; ?>

Upvotes: 1

Related Questions