解析错误:语法错误,php 中意外的 T_STRING
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11785017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse error: syntax error, unexpected T_STRING in php
提问by shiven
<?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
解析错误:语法错误,第 9 行 /home/a4101275/public_html/index.php 中的意外 T_STRING
line 9 is where I am using the file_get_contents.
第 9 行是我使用file_get_contents.
What does the error mean and how do I fix it?
错误是什么意思,我该如何解决?
回答by Matt
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 \.
您必须正确使用转义字符。'在单引号封装的字符串中不能有单引号 ( )。它打破了它。为了继续字符串并让 PHP 从字面上解释您的内部单引号,您必须使用\.
$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_STRINGusually means you have a bad string somewhere on that line.
为了将来参考,Parse error: syntax error, unexpected T_STRING通常意味着您在该行的某处有一个错误的字符串。
回答by Madbreaks
Why quote it at all? I can't imagine the Google API requires (or even expects) that value to be quoted.
为什么要引用它?我无法想象 Google API 需要(甚至期望)引用该值。
$json_url = file_get_contents('http://maps.google.com/maps/api/geocode/json?address=jaipur&sensor=false'); //line 9
Or, is jaipura variable? If so:
或者,是jaipur一个变量?如果是这样的话:
$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...
很难从你的问题中看出你想要完成什么......
回答by tigrang
$json_url = file_get_contents("http://maps.google.com/maps/api/geocode/json?address='jaipur'&sensor=false");
Or escaping it with \
或者逃避它 \

