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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
shiven
  • 421
  • 2
  • 8
  • 19
  • You have to use your escape characters correctly. – Matt Aug 02 '12 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. – Eric Leschinski Oct 27 '16 at 20:17

3 Answers3

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.

Matt
  • 6,993
  • 4
  • 29
  • 50
  • 1
    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. – parttimeturtle Feb 25 '20 at 00:17
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...

Madbreaks
  • 19,094
  • 7
  • 58
  • 72
1
$json_url = file_get_contents("http://maps.google.com/maps/api/geocode/json?address='jaipur'&sensor=false");

Or escaping it with \

tigrang
  • 6,767
  • 1
  • 20
  • 22