当我通过require()
错误:Parse error: syntax error, unexpected T_PUBLIC, expecting T_STRING in /home/content/23/9953123/html/new/lib/functions.php on line 3
代码:
<?php
require("lib/functions.php");
require("lib/dbconnect.php");
?>
functions.php:
<?php
function stateToAbb($input){
//reset found
$found = 0;
//list states
$states = "Alaska,Alabama,Arkansas,Arizona,California,Colorado,Connecticut,Delaware,Florida,Georgia,Hawaii,Iowa,Idaho,Illinois,Indiana,Kansas,Kentucky,Louisiana,Massachusetts,Maryland,Maine,Michigan,Minnesota,Missouri,Mississippi,Montana,North Carolina,North Dakota,Nebraska,New Hampshire,New Jersey,New Mexico,Nevada,New York,Ohio,Oklahoma,Oregon,Pennsylvania,Rhode Island,South Carolina,South Dakota,Tennessee,Texas,Utah,Virginia,Vermont,Washington,Wisconsin,West Virginia,Wyoming";
//list abbreviations
$abb = "AK,AL,AR,AZ,CA,CO,CT,DE,FL,GA,HI,IA,ID,IL,IN,KS,KY,LA,MA,MD,ME,MI,MN,MO,MS,MT,NC,ND,NE,NH,NJ,NM,NV,NY,OH,OK,OR,PA,RI,SC,SD,TN,TX,UT,VA,VT,WA,WI,WV,WY";
//create arrays
$states_array = explode(",", $states);
$abb_array = explode(",", $abb);
//run test
for ($i = 0; $i < count($states_array); $i++){
if (strtolower($input) == strtolower($states_array[$i])){
$found = 1;
$output = $abb_array[$i];
return $output;
}
}
if ($found != 1){
$output = $input;
return $output;
}
return $output;
}
?>
但是当我把这个函数放在文件中时需要一个单独的php文件,它就可以正常工作......
有什么想法吗?
... UPDATE
因为每个人都认为我试图快速使用不同的代码... 这是一个更新的代码...
<?php
require("lib/functions.php");
require("lib/dbconnect.php");
function stateToAbb($input) {
//reset found
$found = 0;
//list states
$states = "Alaska,Alabama,Arkansas,Arizona,California,Colorado,Connecticut,Delaware,Florida,Georgia,Hawaii,Iowa,Idaho,Illinois,Indiana,Kansas,Kentucky,Louisiana,Massachusetts,Maryland,Maine,Michigan,Minnesota,Missouri,Mississippi,Montana,North Carolina,North Dakota,Nebraska,New Hampshire,New Jersey,New Mexico,Nevada,New York,Ohio,Oklahoma,Oregon,Pennsylvania,Rhode Island,South Carolina,South Dakota,Tennessee,Texas,Utah,Virginia,Vermont,Washington,Wisconsin,West Virginia,Wyoming";
//list abbreviations
$abb = "AK,AL,AR,AZ,CA,CO,CT,DE,FL,GA,HI,IA,ID,IL,IN,KS,KY,LA,MA,MD,ME,MI,MN,MO,MS,MT,NC,ND,NE,NH,NJ,NM,NV,NY,OH,OK,OR,PA,RI,SC,SD,TN,TX,UT,VA,VT,WA,WI,WV,WY";
//create arrays
$states_array = explode(",", $states);
$abb_array = explode(",", $abb);
//run test
for ($i = 0; $i < count($states_array); $i++){
if (strtolower($input) == strtolower($states_array[$i])){
$found = 1;
$output = $abb_array[$i];
return $output;
}
}
if ($found != 1){
$output = $input;
return $output;
}
return $output;
}
?>
它不起作用...当我删除它require("lib/functions.php");
时,它的工作原理就是问题代码
答案 0 :(得分:1)
调试此问题的第一步:更改内容以查看错误消息是否也发生了变化。
我总是通过在错误行之前的某处添加一个空行来确保我查看正确的文件。如果错误消息相应更改,我知道我有正确的文件。
之后,根据我的错误消息,如果代码崩溃,我可能会插入die()
。对于解析错误,在某些地方添加半音可能会有所帮助。
为了确定一行中错误的确切位置,我通常会将此行中的每个关键字拆分为一行。空白对于解析没有问题,但这样做可以更好地识别解析器所抱怨的位置。