Alternative conditional syntax (if-else) failing on PHP 5.3.0 (xampp) - php

I recently upgraded to xampp v1.7.2 which dumped PHP 5.3 on me. Along with that all my httpd.confs and php.ini's got wiped out (I had taken it for granted that this release will be an upgrade like all the earlier xampp releases). Anyway, it took me a while to reconfigure all the services - but now I've run into a funny problem.
This self-written CMS that is used in my workplace uses a lot of the alternative conditional syntax for if-else, i.e.
if( condition ): ?>
<some html />
<?php else: ?>
<some other html />
<?php endif;
This used to work fine with PHP 5.2.x that came along with xampp 1.7.1 - and now suddenly such code blocks are producing this error:
Parse error: syntax error, unexpected
T_ELSE in ...
I haven't altered my script in any way - the same used to work absolutely without a hitch in PHP 5.2.x.
So my question is, does PHP 5.3 allow such alternative conditionals? Or do I have to turn on some hidden option in my config files?
Please note that I don't use shorttags - so they aren't an issue here.

Although the if/else syntax hasn't changed in 5.3, many other parts of the syntax have. You should check the lines just before the else statement in question to see if one of the other new syntax features is confusing the parser.
If you can't figure out where the problem is, you can always just start systematically deleting lines of code until you're left with the following three lines:
<?php if(condition): ?>
<?php else: ?>
<?php endif ?>
Update: You really should test your code with short_open_tag turned on, because the syntax error you see is what you would get if you had this code somewhere:
<? if(condition): ?>
<?php else: ?>
<?php endif ?>

Yes, PHP 5.3 allows for alternative syntax for control structures, including your conditional statements.
I would suggest trying to debug by replacing the alternative syntax with regular syntax in one or two places to see if it fixes the problem. If it does, then you know for sure what the issue is.

Related

PhpStorm how to detect/highlight closing "tag" in PHP's alternate syntax of contol structures

Pretty much what the title says. Is there a way to make PhpStorm highlight the closing "tag" when using PHP's alternate syntax for control structures?
Take a look at this sample code for example:
<?
if($x==5) {
echo "x is equal to 5";
}
?>
If i put the cursor next to or before the opening/closing brace, PhpStorm will automatically highlight the matching opening/closing brace.
Now, if we write the same code but this time using PHP's alternate syntax for control structures, we end up with something like this:
<? if ($x==5): ?>
x is equal to 5
<? endif; ?>
In this case, PhpStorm will not highlight either the opening "if" or the closing "endif;". Is there a way to make it highlight it?
Unfortunately current versions of PhpStorm cannot do that.
https://youtrack.jetbrains.com/issue/WI-14517 -- watch this ticket (star/vote/comment) to be notified on any progress. So far it has not been associated with any specific future version (not currently planned for implementation).
Related: https://youtrack.jetbrains.com/issue/WI-566
The 2017 version of PHPStorm now supports it. But apparently they are having issues with it that they turned off this feature, they did not mention what the issue is though. But you can still enable it by going to: Find Action->Registry->php.brace.alt.syntax
See response here Alternate PHP syntax

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.

closing php tag in string stops syntax highlighting

In my vim files, when I have a closing php tag in a string, my syntax highlighting stops at that point
Example:
<?php
... Everything good up to this point
$xml = '<?xml version="1.0"?>';
$foo = 'bar' <-- starting here I loose highlighting to the end of the file
?>
I'm running version 7.2.245. anyone have ideas why this happens? is it only me? or is this a known bug?
Make sure you have the latest version of the runtime files, or at least the very latest version of the php syntax file.
Try using this alternative syntax file instead.
It isnt a bug, you have actually ended the PHP string. you need to just break it up. Do like "'.'?'.'>' or something
This is normal. Notice the SO syntax highlighter stopped highlighting at that point too ;)
My windows editor does the same thing.
Even PHP's built in function works this way (or used to for a long time)

why is there different syntax same outcome?

Why is there different syntax same outcome?
For example
# Example 1
if($myCondition == true) :
#my code here
endif;
if($myCondition == true) {
#my code here
}
# Example 2
foreach($films as $film) :
#my code here
endforeach;
foreach($films as $film) {
#my code here
}
Also I have been using <?= for ages now and I now understand that is deprecated and I should be using <?php echo Is this the case and why? It's a lot more annoying to have to write that out each time.
What are your thoughts?
The colon endif, endforeach, etc syntax is known as Alternative Syntax. I can't say for certain why this functionality exists, just that it does and is supported. I can say that I've noticed the alternative syntax used more for templating purposes where it's easy to pick out an endif/endforeach than it is a closing curly-brace in the middle of HTML markup.
The <?= is known as the short open tag. You can probably find all the info you need about its use here Are PHP short tags acceptable to use?
Why should the outcome be different? The one without the brackets is called alternative syntax for control structures and is very useful, e.g. when dealing with HTML.
<?php echo is much more portable because short open tags can be disabled and are disabled by default since PHP 5.3

PHP text output in clauses

I am working on a PHP project. There, I often use following syntax to output text in a cluase:
if($boolean){
?>
output text
<?
}else{
?>
alternative
<?
}
On my computer, this works perfectly well. I use XAMPP foer Mac OS X. But when I send the files to my coworker, these outputs often do not work and the compiler complains about having reached an unexpected $end of file. This occurs especially often when there is a tag in the output. We have to replace the means of output with echo.
What's the reason for this strange behavior of the compiler? Is the above-mention syntax of outputting text wrong?
Use <?php instead of <? he might not have short tags enabled.
I think you'll find this topic useful;
Are PHP short tags acceptable to use?

Categories