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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Open in new window

elepilAuthor Commented:
Gary, I know how to separate them. I wanted to know WHY I was getting the syntax error.
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 {}
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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.
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.
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'.  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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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"

<title>PHP GET Test</title>
<h1>PHP GET Test</h1>
$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>

Open in new window

With all respect - thanks for repeating what I said.
Dave BaldwinFixer of ProblemsCommented:
Well, I did add some detail and references.  And a silly little test program.
Ray PaseurCommented:
This is such a common question that we have an article to explain it!
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. :)
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!
elepilAuthor Commented:

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.