Solved

Syntax error in PHP

Posted on 2014-10-29
14
142 Views
Last Modified: 2014-10-30
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
Comment
Question by:elepil
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40412395
echo "make=$make : yearmade=" . $_GET['yearmade'];

Open in new window

0
 

Author Comment

by:elepil
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

by:Gary
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

by:elepil
ID: 40412408
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
 
LVL 58

Assisted Solution

by:Gary
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 82

Accepted Solution

by:
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 82

Expert Comment

by:Dave Baldwin
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 />

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

Open in new window

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 58

Expert Comment

by:Gary
ID: 40412499
With all respect - thanks for repeating what I said.
0
 
LVL 82

Expert Comment

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

Assisted Solution

by:Ray Paseur
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

by:elepil
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

by:Gary
ID: 40413519
0
 
LVL 108

Expert Comment

by:Ray Paseur
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

by:elepil
ID: 40413817
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now