Link to home
Start Free TrialLog in
Avatar of elepil
elepil

asked on

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.
Avatar of Gary
Gary
Flag of Ireland image

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

Open in new window

Avatar of elepil
elepil

ASKER

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 {}
Avatar of elepil

ASKER

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.
SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

With all respect - thanks for repeating what I said.
Well, I did add some detail and references.  And a silly little test program.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of elepil

ASKER

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. :)
"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!
Avatar of elepil

ASKER

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.