How to debug errors in Laravel like unexpected ';' - php

I have started learning Laravel from last few days and during the course I keep getting errors like
unexpected ';'
below is the detailed error
FatalErrorException in
home/ubuntu/workspace/storage/framework/views/d54ff8380f369dc8c9bc785ef2c17d66e0f399b3.php line 10: syntax error,
unexpected ';'
Though I was able to find the cause of the error,
which was again because of one typographical mistake in one of my view file i.e. create.blade.php .
My question is how to debug similar kind of error in large projects as error don't points out the exact file-name where syntax error was happening though the line number mentioned was the correct one ?
Please do let me know if I am not clear in my above question.
P.S. I checked the logs as well and it says the same thing.
Thanks in advance,
Sambhav

Well, this error is difficult to find when you are starting with laravel, or even worst, when you have kilometers in laravel but have a tons of view compiled at the same layout.
Generally this errors comes when you put a semicolon in blade syntax like this:
#if($something == true)
// code here
#endif;
See the semicolon at endif? well, that breaks laravel's view. Another classic is:
#lang('app.hello');
or
#endforeach;
You need to review in that keywords if it has semicolon at the end. What I do sometimes is to call the views individually to isolate the error, it is easiest to find the error when you find the view in concrete. Why laravel does not display the current line number in the view where the errors is jumping? This is because laravel at first place compile blade syntax into html file, this file is cached to increase response's speed. So, laravel will compile even types like these exists;

Related

A weird PHP file in which any instruction is a syntax error

I have a Web application in PHP which when / (or something else) of it is opened in a browser displays the following error in the browser: Parse error: syntax error, unexpected 'require_once' (T_REQUIRE_ONCE) in /var/www/n_environment.php on line 3. The file n_environment.php is loaded with require_once 'n_environment.php'; in index.php. And also from different places. It contains just comments, calls to define (unconditional or guarded by if (!defined) and two assignments of arrays of strings to variables.
I thought maybe it's (indirectly) in a class but not in a method, which indeed PHP prohibits for require_once, but apparently that's not the problem here. If I put anything else as the first instruction, e.g. echo or an assignment, a similar error (with a different offending token) results.
If I comment out everything in the file except the initial <?php, the error changes to unexpected end of file.
And if I also remove the <?php, what I get in the browser is the file content (block comment with code inside) with â instead of new lines.
What are the possible, most likely reasons for such behaviour? And how to fix it?

codeigniter php code on xampp works but not on ubuntu

I have this code that works perfectly on my local xampp (PHP 5.5.24) but gives error on ubuntu (PHP 5.5.9-1ubuntu4.14):
Parse error: syntax error, unexpected '}' in /var/www/html/maybright/application/views/admin/components/edit_user.php on line 179
A PHP Error was encountered
Severity: Parsing Error
Message: syntax error, unexpected '}'
Filename: components/edit_user.php
Line Number: 179
<p>Backtrace:</p>
I know it is parse error, so I am attaching the file as well. The controller has this
$userDetails = $this->maybright->GetUserDetails($user_id);
$userDetails = json_decode(json_encode($userDetails), FALSE);
$content['user_id'] = $user_id;
$content['User_details'] = $userDetails->response;
$content['subview']="edit_user";
$this->load->view('admin/_main_layout', $content);
The view is in gist at https://gist.github.com/vishwakarma09/27fc2ca1ec33d8eca05d47c878141a32
you can view it as raw and open in notepad++ for proper indentation.
$userDetails is this json response:
{"status":"SUCCESS","id":25,"message":null,"responseSize":1,"response":{"id":25,"hash":null,"name":"Arindam Nath","firstName":null,"middleName":null,"lastName":null,"email":"strider2023#gmail.com","phoneNumber":"9874381131","accountType":"USER","gender":"MALE","dob":630143205000,"maritalStatus":"SINGLE","workStatus":"SALARIED","residentialStatus":"RENTAL","deviceData":null,"userImage":"https:\/\/s3-ap-southeast-1.amazonaws.com\/mbv-pokket\/user-images\/user_profile_25_userImage_1459410437667.jpg","referralCode":null,"fatherName":"Aroon Nath","gcmId":null,"roleType":"LEND","rating":null,"defaults":null,"userLocationDatas":[{"id":70,"userId":25,"address":"House No. 34, Chooliemedu","city":"Chennai","state":"Tamil Nadu","country":"India","pincode":600034,"type":"HOME","isVerified":true},{"id":50,"userId":25,"address":"Chatterjee Bagan","city":"Hooghly","state":"West Bengal","country":"India","pincode":712102,"type":"CURRENT","isVerified":true},{"id":49,"userId":25,"address":"Chatterjee Bagan, ","city":"Hooghly","state":"West Bengal","country":"India","pincode":712102,"type":"HOME","isVerified":true}],"userKYCDatas":[{"id":12,"userId":25,"type":"PASSPORT","kycId":"ASD6Q133","imageUrl":"https:\/\/s3-ap-southeast-1.amazonaws.com\/mbv-pokket\/user-images\/user_kyc_25_kycImg_1459526858473.jpg","isVerified":true},{"id":10,"userId":25,"type":"PAN","kycId":"AHIPN123456","imageUrl":"https:\/\/s3-ap-southeast-1.amazonaws.com\/mbv-pokket\/user-images\/user_kyc_25_kycImg_1458574318698.jpg","isVerified":true}],"userEducationDatas":[{"id":11,"userId":25,"institutionName":"ICAT","degreeType":"BACHELORS","degreeCategoryName":"Game Programming","description":"Game programming","startDate":1187019048000,"endDate":1280158262000,"city":"Chennai","country":"India","state":"Tamil Nadu","pincode":600034,"score":null,"reportUrl":null,"isVerified":true}]}}
UPDATE
I have updated controller view and current deployed URL in comments. Please check.
Using my comment as an answer as it helped fix the issue:
The problem is that the close bracket in question is the close to an opening if-statement on line 123. This statement is opened by the following code:
<? if(isset($User_details->userLocationDatas)){ ?>
This is using the PHP short tags option (doesn't start <?php) The windows machine the development is taking place on allows this, but the Ubuntu server doesn't. As a result, the opening if statement is treated as HTML by the server, and the correctly formed <?php }?> is seen as not needed; hence the error.
There's a couple of ways I can think of to help avoid this in future:
Never use short tags for PHP in your code. Either use <?php or <?= as appropriate. The former is more readable and widely used
Where possible, develop on an environment which is configured the same (or as close as possible) to the environment it is going to be deployed on, so these issues are caught early.

Parse Error OptimizePress 2 WP

Parse error: syntax error, unexpected 'OptimizePress_Install'
(T_STRING) in
/home/investor/public_html/wp-content/themes/optimizepress/lib/admin/install.php
on line 1
Getting this annoying error when I click on "Activate Theme". How do I get around this? Code is below on pastebin
http://pastebin.com/GJkL64d1
First of all it needs a space between <?php and class so change it to <?php class
The second reason is it failing is because it is a 1 line script (all goes is on one line).
The first time it meets a comment the rest of the script is commented out.
You can fix this by
Download the file where is it not a strange one line.
Remove all comments
Beautify the php (make currect spaces)

(PHP,xampp) syntax error in every code I test

I get a " Parse error: syntax error, unexpected" error for whatever I print.
The error "Parse error: syntax error, unexpected '"test"' (T_CONSTANT_ENCAPSED_STRING) in C:\xampp\htdocs\firstfile\test.php on line 3"
The code I tested
<?php
 echo 'test';
 
?>
A basic code, but the error persist.
How to solve this problem?
Could it be that what you posted is not the actual file that is executed? Certainly that file is fine syntactically, so there must be some stupid mistake somewhere. I see that the faulty file is inside a folder firstfile. You are really sure this posted file is that file inside the folder?
What is especially outstanding here are the double quote chars in the error message which simply are not present in the file you posted.
When an error like this remains unchanged whatever you do, then typically you edit the wrong file.

PHP array_search nor working?

What am I doing wrong here?
$array = array('sky'=>'blue', 'grass'=>'green', 'sun'=>'yellow');
$key = array_search('green', $array);
echo $key;
error: Parse error: syntax error, unexpected T_DOUBLE_ARROW in /Applications/XAMPP/xamppfiles/htdocs/search-array.php on line 2
$array = array('sky'=>'blue', 'grass'=>'green', 'sun'=>'yellow');
$key = array_search('green', $array);
echo $key;
The source you posted works perfectly and returns 'grass'. Are you sure it is not another snippet in your application?
You may want to try finding if you closed the array right and if you have commas where they are needed. If that doesn't resolve it for perfectly functional syntax:
error_reporting(E_ALL^E_NOTICE);
Long shot, but worth a mention, I feel:
I've had the experience that sometimes, some FTP programs error out silently if they don't manage to transfer the whole file. If you're experiencing the error on a webserver and you pasted us the local code (and only then), that might be what's causing your problem; e.g. if your remote file looks like this due to an incomplete transfer:
$array = array('sky'=>'blue', 'grass'=>'green', 'sun'=>
I've gotten some odd errors over time that I couldn't explain that then boiled down to an incomplete transferred file.
I'd recommend that whenever you encounter a parse error you can't find, try re-uploading the file. If it still occurs, chances are you overlooked something.
(Needless to say that if this does happen to you, you should probably look into a better FTP client. :) Mind, I don't take this advice, I like mine too much, this is its only shortfall.)

Categories