legacy800 Posted March 15, 2010 Share Posted March 15, 2010 Hi,In Examples like this:echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";I understand how the concatenation operator works in examples like these , but in examples like the one below why is it needed?Couldn't really find a good answer in the tutorials.echo "<td>" . $row['website'] . "</td>";Greetings,A php noob Link to comment Share on other sites More sharing options...
jeffman Posted March 15, 2010 Share Posted March 15, 2010 PHP variables can all be resolved inside double quotes (and heredocs, FWIW). But array elements require a special syntax. The altrernative to your example is this:"<td>{$row['website']}</td>";Some developers dislike the curly braces, I think. It's a matter of personal style more than anything. Link to comment Share on other sites More sharing options...
ShadowMage Posted March 15, 2010 Share Posted March 15, 2010 Some developers dislike the curly braces, I think. It's a matter of personal style more than anything.Like me! I get confused if variables are interpolated. Strings are strings, variables are variables, they shouldn't be mixed together! Link to comment Share on other sites More sharing options...
iwato Posted March 15, 2010 Share Posted March 15, 2010 I understand how the concatenation operator works in examples like these , but in examples like the one below why is it needed?Is it not because because you are concatenating string literals and variables in both instances?Frankly I see no difference between the two examples that you provide. In both cases what is contained in quotation marks becomes a fixed part of the HTML document, and what is preceded by a dollar sign is a PHP variable whose value changes according to some statement written in PHP.Roddy Link to comment Share on other sites More sharing options...
jeffman Posted March 15, 2010 Share Posted March 15, 2010 Like me!I actually like the ability to interpolate variables. Sometimes I get a better idea what my finished string will look like, especially when I'm building SQL queries. But those curly braces are annoying. I often assign array elements to a scalar variable before assembling strings just so I can make them more readable. Link to comment Share on other sites More sharing options...
boen_robot Posted March 15, 2010 Share Posted March 15, 2010 I actually like the ability to interpolate variables. Sometimes I get a better idea what my finished string will look like, especially when I'm building SQL queries. But those curly braces are annoying. I often assign array elements to a scalar variable before assembling strings just so I can make them more readable.I like interpolation too, but I personally prefer having curly braces around variables that would be interpolated. When you mix several layers at once (e.g. PHP inserting PHP code into MySQL... an unlikely case, I know, but still), it can sometimes be confusing whether a certain special character is part of one layer or another. Curly braces make it somewhat more explicit - you know that if there's "{$", you have a variable name in place, whereas if you watch out only for "$", you also need to keep in mind the "\$" sequence... then again, having "\{$" is also a possible sequence, but even then, you still have interpolation occuring... only with "{" in front of the interpolated variable value. Link to comment Share on other sites More sharing options...
ShadowMage Posted March 15, 2010 Share Posted March 15, 2010 I like interpolation too, but I personally prefer having curly braces around variables that would be interpolated. When you mix several layers at once (e.g. PHP inserting PHP code into MySQL... an unlikely case, I know, but still), it can sometimes be confusing whether a certain special character is part of one layer or another. Curly braces make it somewhat more explicit - you know that if there's "{$", you have a variable name in place, whereas if you watch out only for "$", you also need to keep in mind the "\$" sequence... then again, having "\{$" is also a possible sequence, but even then, you still have interpolation occuring... only with "{" in front of the interpolated variable value.That's exactly the problem. There's too much looking to do. When I see this: "variable: ".$varI know that means I'm using a variable and I want to see the variable's data when it's printed.When I see this: "variable: $variable"It isn't clear to me that this should also print the variable's data. It requires a more careful eye. (Especially with syntax coloring )And as far as the different layers go...well, you don't have to worry about your escape sequences and what pattern to look for.Just look for this: 'var: '.$var.' with $var'So simple! Link to comment Share on other sites More sharing options...
boen_robot Posted March 15, 2010 Share Posted March 15, 2010 I prefer concatenation to interpolation too. My point was that when I do use interpolation (which I use for simpler cases, mostly), I prefer to use curly braces than not. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.