【问题标题】:T_PUBLIC, expecting T_STRING using require() [closed]T_PUBLIC,期待 T_STRING 使用 require() [关闭]
【发布时间】:2012-12-31 08:15:48
【问题描述】:

当我通过 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 文件时,它工作得很好......

有什么想法吗?

更新...

由于每个人都认为我正在尝试快速开发并使用不同的代码... 这是更新的代码...

<?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"); 时它起作用了,所以问题在于那行代码

【问题讨论】:

  • 1. SO 不是您的个人解析器 2. 我相信您向我们展示了不同的文件
  • 显示的代码没有问题,请尝试发布其他文件。
  • 我只是猜测:您在课堂外的某个地方有一个public function foobar() { /* code */ }。那是不可能的,不允许的,除此之外它甚至没有多大意义:D
  • 更新了顶部的完整部分 php,如果我删除 require("lib/functions.php"); 页面加载正常,并且上面的文本就是 functions.php 文件中的全部内容
  • 您确定向我们展示了正确的文件吗?我注意到您的路径包括 html/new/ 也许您正在查看“html/old”?

标签: php require


【解决方案1】:

调试的第一步:改变一些东西,看看错误信息是否也改变了。

我总是通过在错误行之前的某处添加一个空白行来确保我正在查看正确的文件。如果错误消息相应更改,我知道我有正确的文件。

之后,根据我收到的错误消息,如果代码崩溃,我可能会插入die()。对于解析错误,在某些地方添加分号可能会有所帮助。

为了确定一行中错误的确切位置,我通常将这一行中的每个关键字拆分为自己的一行。空格对于解析没有问题,但这样做可以更好地识别解析器抱怨的确切位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 2013-12-15
    • 2013-07-15
    • 2015-02-26
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多