【发布时间】: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