Welcome to WebmasterWorld Guest from 54.234.38.8

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Backwards/Forwards compatible syntax

Will the following PHP coding work with previous and future versions of php

     
8:53 am on Jun 9, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:July 21, 2008
posts:103
votes: 0


I'm using a lot of seeming 'shortcuts' with my PHP at the moment, and just wanted to know if they seem like long-term solutions (and if they are compatible with older versions) - I've had a look through the docs but have struggled to find anything concrete.

1. {$braces} for outputting vars

I use this all the time now, instead of ever concatenating a string

$string1 = "text here {$var} without need for ".$xyz." concatenating";

2. (triple arrow) <<<

Whenever I'm outputting a string with more than 40-50 chars I always use

$output = <<<HTML
<h1>Header 1</h1>
<p>Paragraph1 - {$var1} etc</p>
HTML;

My worry is that I'm coding using something similar to 'short tags' and in future/previous PHP versions these may not work. Any thoughts?

12:53 pm on June 9, 2009 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 0


I think you can feel pretty good about interpolation and concatenation remaining quite stable and unchanging moving to the future. There may be some additions as we have seen lately [php.net] but nothing removed.

As far as backwards-compatibility -- IMHO if you are not running a PHP5 system today you are really putting yourself in a compromising position. Support for PHP 4 has been discontinued since 2007-12-31.

3:38 pm on June 9, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


1. {$braces} for outputting vars

I use this all the time now, instead of ever concatenating a string

Just to clarify... it's not strictly the curly braces that allow you to output vars directly in a string, it's the fact you've enclosed the string in double quotes, or used heredoc (<<<) syntax. However, the curly braces are sometimes required in order to avoid ambiguity (PHP Manual: variable parsing [uk2.php.net]). In the examples you've given, the curly braces are optional.

The curly braces (complex syntax) and heredoc syntax were introduced in PHP 4 - so yes, it's backwards compatible. But, as coopster suggests, that shouldn't be an issue for new scripts.

4:45 pm on June 9, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:July 21, 2008
posts:103
votes: 0


Many thanks Coopster/Penders - seems after reading both the comments/links in the post, it's cleared it up for me.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members