Tuesday, 15 March 2016

parsing - Parse error: syntax error, unexpected T_STRING in 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?


Answer



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.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...