Parse error: syntax error, unexpected T_STRING in php
When you encounter a Parse error: syntax error, unexpected T_STRING on line 9 of a PHP script, the immediate instinct is often to check the logic of that specific line. However, parsing errors occur before execution begins; the engine simply cannot construct a valid representation of your code based on the rules of the language. In this specific case, the culprit isn't file_get_contents itself, but how the arguments are being passed to it.
The error points directly to the URL construction within the file_get_contents call. Looking closely at the source code, we see:
$json_url = file_get_contents(
'http://maps.google.com/maps/api/geocode/json' .
'?address='jaipur'&sensor=false');
The issue lies in the string interpolation and concatenation logic. PHP treats a variable enclosed in double quotes as an interpolated string, whereas single quotes define a literal string where variables are not expanded by default. However, the real problem here is the placement of the single quote character '.
In the segment '?address='jaipur', the code attempts to concatenate a URL parameter key with a value. The syntax ?address='jaipur' implies that the parser sees an opening single quote after the equals sign. To PHP, this looks like an attempt to start a new string literal immediately following the concatenation operator, but the structure is malformed. Specifically, the sequence 'jaipur' inside the double-quoted string context is valid only if it represents a string literal. However, when combined with the preceding ., and surrounded by single quotes in the source representation provided, the parser gets confused about where the variable or string ends and begins.
The correct approach requires ensuring that the entire URL argument is treated as a single coherent string expression. When using double quotes for the outer string (which allows for interpolation if needed), you must ensure that internal single quotes used to delimit strings inside the concatenation are handled correctly, or simply use double quotes consistently to avoid ambiguity.
Here is the corrected implementation:
$json_url = file_get_contents(
'http://maps.google.com/maps/api/geocode/json' .
'?address=jaipur&sensor=false');
By removing the single quotes around jaipur inside the concatenation chain, we ensure that jaipur is treated as a literal string value rather than an attempt to start a new quoted block. The original code effectively read as: take the first part of the URL, concatenate it with ?address= followed by a string starting with ', then insert jaipur, then close that inner string, and finally append &sensor=false'. The mismatch in quote types (mixing single and double quotes without proper nesting) confuses the tokenizer.
It is also worth noting the deprecated parameter sensor=false. Modern implementations of the Google Geocoding API no longer require this flag; omitting it is cleaner and avoids potential deprecation warnings in newer versions of the API. While your immediate goal is to fix the parse error, updating the request aligns with current best practices.
If you are building this functionality within a larger application ecosystem, such as using the Laravel framework, handling external HTTP requests often benefits from dedicated clients rather than raw file_get_contents. For instance, Laravel provides a robust HTTP client that handles redirects, timeouts, and JSON decoding more gracefully. You might consider migrating to:
$response = \Illuminate\Support\Facades\Http::get('http://maps.google.com/maps/api/geocode/json', [
'address' => 'jaipur',
]);
$decoded = json_decode($response->body(), true);
$results = $decoded['results'] ?? [];
This approach separates the HTTP logic from the parsing logic, making the codebase more maintainable. However, fixing the immediate syntax error in your current script is a matter of ensuring that all string delimiters are consistent and that concatenation operators connect complete string literals without breaking the token stream for the parser.