• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 156
  • Last Modified:

Syntax error in PHP

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']";

Open in new window


The PHP interpreter doesn't like the way I am using the $_GET [] variable somehow.

Thanks.
0
elepil
Asked:
elepil
  • 5
  • 4
  • 3
  • +1
3 Solutions
 
GaryCommented:
echo "make=$make : yearmade=" . $_GET['yearmade'];

Open in new window

0
 
elepilAuthor Commented:
Gary, I know how to separate them. I wanted to know WHY I was getting the syntax error.
0
 
GaryCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
elepilAuthor Commented:
Gary, did you try this in your PHP editor? The $make is fine. If I do a:

echo "make=$make";

Open in new window


it will work. The problem is in the $_GET and I don't know why.
0
 
GaryCommented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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 />

<a href="PHP-GET-Test.php?yearmade=1972">Year made = 1972.</a>
</body>
</html>

Open in new window

0
 
GaryCommented:
With all respect - thanks for repeating what I said.
0
 
Dave BaldwinFixer of ProblemsCommented:
Well, I did add some detail and references.  And a silly little test program.
0
 
Ray PaseurCommented:
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
 
elepilAuthor Commented:
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
 
Ray PaseurCommented:
"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
 
elepilAuthor Commented:
Ray,

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

Open in new window


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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now