2011-08-22 89 views
0

有什么办法可以重写下面的代码,以便它可以在运行PHP 5.2.x的环境中运行?它工作得很好的运行在版本5.3.5(DEV),但我的共享主机环境(产品)运行5.2和抛出这个错误:PHP代码在ver。 5.2,而不是5.3

Parse error: syntax error, unexpected T_FUNCTION in /home/myperf7/ public_html/inc/store-address.php on line 1

这里是有问题的代码:

<?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(); } 

?> 

这如何在商店address.php文件被纳入从我的index.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> 

感谢这么这么这么这么多!

回答

0

看起来你可能有一些非空白字符在你的function之前。

您的PHP代码似乎适用于5.2和5.3。

+0

嗨,感谢您审查代码。但是php中的最后一个“p”和函数中的“f”之间只有空白。但是,如果我删除了一些空白,而是使第一行代码为'<?php function storeAddress(){',那么我会得到一个新错误:'解析错误:语法错误,意外$ end' – mark

+0

它可能有看起来像是一个空白的东西,事实并非如此。 '意外的$ end'通常意味着你留下了一个未关闭的支架,但是我找不到任何关于你的代码的信息 – seppo0010

+0

除了未关闭的支架之外,还有什么可能导致'意外的$ end'? – mark

0

我能看到的唯一可能是错误的是$api->errorMessage。尝试取出整个,如果声明:

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; 
} 

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

1

发生此问题的原因是Netbeans将我的PHP文件全部保存在一行中,直到我在普通的旧记事本中打开我的代码之前,我一直没有选择该问题。即使在Notepad ++中打开Netbeans保存的文件,代码也会像平常一样显示为缩进和断行。但是在经典的记事本中,所有代码都在一行中,这意味着脚本一旦达到第一个“//”注释就会中断。

感谢大家的帮助!

+4

我想你的共享主机环境是Windows服务器。 – xdazz

+0

有换行符,但它们是Unix样式的换行符。记事本可能是唯一不理解它们的编辑器。 在Windows上,您的文件应该是Windows风格的。在NetBeans中可能有一个设置。 – MaxVT

+0

雅,我整理了Netbeans的设置,所以一切都很好!谢谢! – mark