Solved

# Syntax error in PHP

Posted on 2014-10-29
146 Views
This should be an easy one for you PHP experts out there. Can anyone tell me why I'm getting a syntax error on the following echo statement?

echo "make=$make : yearmade=$_GET['yearmade']";


The PHP interpreter doesn't like the way I am using the $_GET [] variable somehow. Thanks. 0 Question by:elepil • 5 • 4 • 3 • +1 14 Comments LVL 58 Expert Comment ID: 40412395 echo "make=$make : yearmade=" . $_GET['yearmade'];  0 Author Comment ID: 40412399 Gary, I know how to separate them. I wanted to know WHY I was getting the syntax error. 0 LVL 58 Expert Comment ID: 40412405 Because it's trying to parse an expected variable (like$make) and it's not a variable.
If you want to embed it directly then wrap it in squigglies {}
0

Author Comment

ID: 40412408
Gary, did you try this in your PHP editor? The $make is fine. If I do a: echo "make=$make";


it will work. The problem is in the $_GET and I don't know why. 0 LVL 58 Assisted Solution Gary earned 100 total points ID: 40412412 It expects a variable like$make
If you try with $_GET['yearmade'] it will only read$_GET - you can't have [ in a variable name
Wrap it in squigglies then it will work.
0

LVL 83

Accepted Solution

Dave Baldwin earned 150 total points
ID: 40412437
This is the error I get (without {}):
PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Inetpub\wwwroot\ee2\PHP-GET-Test.php on line 12

This is the page that describes these 'tokens'.  http://php.net/manual/en/tokens.php  Their explanation appears to say that if it is too complex, it won't be parsed without the '{}' to tell it what goes together.

I myself never put $_GET or anything similar in a quoted string. I always first assign it to a 'regular' variable because I had problems many years ago. You may think that would slow things down... but the PHP interpreter pre-compiles and optimizes it before it is run. 0 LVL 83 Expert Comment ID: 40412438 For what it's worth, this is my little test program. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>PHP GET Test</title> </head> <body> <h1>PHP GET Test</h1> <?php$make = 'Yugo';
echo "make=$make : yearmade=$_GET['yearmade']";
//echo "make=$make : yearmade={$_GET['yearmade']}";
?>
<br />

</body>
</html>

0

LVL 58

Expert Comment

ID: 40412499
With all respect - thanks for repeating what I said.
0

LVL 83

Expert Comment

ID: 40412519
Well, I did add some detail and references.  And a silly little test program.
0

LVL 109

Assisted Solution

Ray Paseur earned 250 total points
ID: 40412958
This is such a common question that we have an article to explain it!
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_12241-Quotation-Marks-in-PHP.html
0

Author Closing Comment

ID: 40413468
Let me explain my point distribution. Ultimately, the person with the most thorough answer gets the most points.

Ray, although it seemed initially lazy that he provided just a link (which I normally frown upon), actually led me to an article he wrote about this subject and explained it thoroughly. Kudos for that.

Dave Baldwin actually exerted effort to try out my ONE-LINER and even provided his test program showing the correct code explicitly.

Gary, you've helped me quite a few times before, and I always appreciate your responses. But I couldn't help but feel you were rushing on this one. While your responses were all correct, it would've helped just to take that "extra step" to improve your clarity. For example, telling me a "squiggly" would take care of the problem. I remember someone who called a tilde (~) a squiggly. So you had me thinking there for a moment.

But thanks to all for responding. As always, I appreciate your efforts. :)
0

LVL 58

Expert Comment

ID: 40413519
0

LVL 109

Expert Comment

ID: 40413735
"Lazy" programmers are the ones who get the most value out of the least effort.  It may be "lazy" to investigate an issue, document it and run it to ground so that questions can be answered more efficiently, especially where there are layers of information and nuance needed to convey the important concepts.  Sort of that thing about teaching a man to fish.  Don't presume that a link to an E-E published article is either an answer or a non-answer - just go read the article and judge for yourself.

FWIW, Experts-Exchange articles are, in fact, a great way to be lazy.  My article about PHP client authentication has been viewed over 37,000 times and has earned me over 180,000 points.  And I've hardly had to lift a finger since its original publication more than four years ago.  Anyone wondering whether they should write an article for E-E can look to that record as evidence that the effort was worth it!
0

Author Comment

ID: 40413817
Ray,

Don't presume that a link to an E-E published article is either an answer or a non-answer


Of course I always click the link to see what it's all about. Links are okay as long as it's scope is within the confines of my query; giving me a link to an entire PHP tutorial would've earned you zero points, but your link this time specifically addressed my question, which I appreciate.

Lazy is a virtue in programming if it leads to creation of reusable function libraries. Lazy is NOT good when an answer to a question is unclear because the responder obviously just didn't want to exert that extra ounce of effort to be thorough.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question