【问题标题】:PHP codes works in ver. 5.2, not in 5.3PHP 代码在版本中工作。 5.2,不在 5.3 中
【发布时间】:2011-08-22 23:30:17
【问题描述】:

有没有办法重写下面的代码,使其在运行 PHP 5.2.x 版本的环境中工作?它在 5.3.5 (dev) 版本中运行良好,但我的共享托管环境 (prod) 运行 5.2 并抛出此错误:

解析错误:语法错误,第 1 行 /home/myperf7/public_html/inc/store-address.php 中的意外 T_FUNCTION

这是有问题的代码:

<?php

function storeAddress(){

    // Validation
    if(!$_GET['email']){ return "No email address provided"; } 

    if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) {
        return "Email address is invalid"; 
    }

    require_once('MCAPI.class.php');
    // grab an API Key from http://admin.mailchimp.com/account/api/
    $api = new MCAPI('XXXXXXX');

    // grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
    // Click the "settings" link for the list - the Unique Id is at the bottom of that page. 
    $list_id = "XXXXXXX";

    if($api->listSubscribe($list_id, $_GET['email'], '') === true) {
        // It worked!   
        return 'Success! Check your email to confirm sign up.';
    }else{
        // An error ocurred, return error message   
        return 'Error: ' . $api->errorMessage;
    }

}

// If being called via ajax, autorun the function
if(isset($_GET['ajax'])){ echo storeAddress(); }

?>

这就是我的 index.php 页面中包含 store-address.php 文件的方式:

<form id="signup" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
    <input type="text" value="" name="email" class="email" id="email" placeholder="email address" required="required" />
    <div class="clear">
        <input type="submit" value="Notify me" name="subscribe" class="button"/>
    </div>
    <span id="response"><?php require_once('inc/store-address.php'); if(isset($_GET['submit'])){ echo storeAddress(); } ?></span>
</form>

非常感谢!

【问题讨论】:

    标签: php


    【解决方案1】:

    出现问题是因为 Netbeans 将我的 PHP 文件全部保存在一行中,直到我在普通的旧记事本中打开我的代码时才发现这个问题。即使在 Notepad++ 中打开 Netbeans 保存的文件时,代码也会像往常一样出现缩进和断行。但在经典记事本中,所有代码都在一行中,这意味着脚本在到达第一个“//”注释时就中断了。

    感谢大家的帮助!

    【讨论】:

    • 我猜你的共享主机环境是windows server。
    • 有换行符,但它们是 Unix 风格的换行符。记事本可能是唯一不理解它们的编辑器。在 Windows 上,您的文件应该是 Windows 样式的。 NetBeans 中可能有一个设置。
    • 是的,我整理了 Netbeans 中的设置,现在一切正常!谢谢!
    【解决方案2】:

    您的function 之前似乎有一些非空白字符。

    您的 PHP 代码似乎对 5.2 和 5.3 有效。

    【讨论】:

    • 您好,感谢您查看代码。但是php中最后的“p”和函数中的“f”之间只有空格。但是,如果我删除了一些空格,而是将第一行代码改为&lt;?php function storeAddress(){,那么我会收到一个新错误:Parse error: syntax error, unexpected $end
    • 它可能看起来像一个空格,但事实并非如此。 unexpected $end 通常意味着您留下了一个未闭合的括号,但我在您的代码中找不到任何内容
    • 除了未闭合的括号还有什么可能导致unexpected $end
    • @mark:我认为问题可能出在preg_match;见php.net/manual/en/function.preg-match.php#88587
    【解决方案3】:

    我能看到的唯一可能是错误的是$api-&gt;errorMessage。尝试取出整个 if 语句:

    if($api->listSubscribe($list_id, $_GET['email'], '') === true) {
        // It worked!   
        return 'Success! Check your email to confirm sign up.';
    }else{
        // An error ocurred, return error message   
        return 'Error: ' . $api->errorMessage;
    }
    

    然后看看你是否得到错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-09
      • 2012-07-27
      • 2012-07-19
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多