6
<?php
    include_once 'forecastVo.php';
    include_once 'BaseVo.php';
    $count=0;
    $json_url = file_get_contents(
        'http://maps.google.com/maps/api/geocode/json' .
        '?address='jaipur'&sensor=false');                //line 9
    if($json_url){
        $obj = json_decode($json_url,true);

        $obj2= $obj['results'];
    }
?>

I am getting an error:

Parse error: syntax error, unexpected T_STRING in /home/a4101275/public_html/index.php on line 9

line 9 is where I am using the file_get_contents.

What does the error mean and how do I fix it?

2
  • You have to use your escape characters correctly. Commented Aug 2, 2012 at 20:12
  • 1
    You can get this error for many different reasons 1: forget a semicolon on a previous line, 2: forget a concatenation operator dot . between variables, or like in your case, 3: delimit a string using single quotes which contain single quotes. The interior single quotes are treated as delimiters and complete the string. The PHP parser sees the word after it as a syntax error. As matt said above, your interior single quotes need to be properly escaped with a \'in order to be interpreted as literals rather than delimiters. Commented Oct 27, 2016 at 20:17

3 Answers 3

7

You have to use your escape characters correctly. You can't have a single-quote (') inside of a single-quote-encapsulated string. It breaks it. In order to continue the string and have PHP interpret your inner single-quote literally, you have to escape it with \.

$json_url = file_get_contents('http://maps.google.com/maps/api/geocode/json?address=\'jaipur\'&sensor=false'); 

Or you can use the alternative string encapsulator, double-quote (").

$json_url = file_get_contents("http://maps.google.com/maps/api/geocode/json?address='jaipur'&sensor=false");

For future reference, Parse error: syntax error, unexpected T_STRING usually means you have a bad string somewhere on that line.

Sign up to request clarification or add additional context in comments.

1 Comment

I think it's worth elaborating that the line this error points to is where the error became fatal, not necessarily where it began. The actual typographical error you need to correct may be several lines or more above the line number indicated in the error message. In other words, the line number in the error message is where the error culminated into failure, rather than where it began. Those two moments in code are not always on the same line.
2

Why quote it at all? I can't imagine the Google API requires (or even expects) that value to be quoted.

$json_url = file_get_contents('http://maps.google.com/maps/api/geocode/json?address=jaipur&sensor=false'); //line 9

Or, is jaipur a variable? If so:

$json_url = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$jaipur&sensor=false"); //line 9

Hard to tell from your question what it is you're trying to accomplish...

Comments

1
$json_url = file_get_contents("http://maps.google.com/maps/api/geocode/json?address='jaipur'&sensor=false");

Or escaping it with \

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.