Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse error: syntax error, unexpected T_STRING in php

Tags:

php

parsing

<?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?

like image 945
shiven Avatar asked Aug 02 '12 20:08

shiven


People also ask

What is unexpected T_string?

Fixing "Parse error: syntax error Unexpected T_STRING error" This type of error (syntax error) usually happens because of a typo in your code, such as a missing apostrophe/quotation mark/a missing or extra space, or code that has been commented out incorrectly.

What is T_string?

T_String is the common parse error associated with the creation of PHP files. This problem is observed when an apostrophe sign is added in between a code that has been delimited with apostrophes, generating huge conflict with the PHP document.


1 Answers

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.

like image 50
Matt Avatar answered Sep 27 '22 17:09

Matt