有什么办法可以重写下面的代码,以便它可以在运行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>
感谢这么这么这么这么多!
嗨,感谢您审查代码。但是php中的最后一个“p”和函数中的“f”之间只有空白。但是,如果我删除了一些空白,而是使第一行代码为'<?php function storeAddress(){',那么我会得到一个新错误:'解析错误:语法错误,意外$ end' – mark
它可能有看起来像是一个空白的东西,事实并非如此。 '意外的$ end'通常意味着你留下了一个未关闭的支架,但是我找不到任何关于你的代码的信息 – seppo0010
除了未关闭的支架之外,还有什么可能导致'意外的$ end'? – mark