有没有办法重写以下代码,以便它在运行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>
非常感谢!
答案 0 :(得分:1)
问题出现是因为Netbeans将我的PHP文件全部保存在一行中,直到我在普通的旧记事本中打开我的代码之前我才解决这个问题。即使在Notepad ++中打开Netbeans保存的文件,代码也会像往常一样缩进和换行。但是在经典的记事本中,所有代码都在一行中,这意味着一旦脚本达到第一个“//”注释,脚本就会破坏。
感谢大家的帮助!
答案 1 :(得分:0)
您的function
之前似乎有一些非空格字符。
您的PHP代码似乎对5.2和5.3有效。
答案 2 :(得分:0)
我唯一能看到错误的是$api->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;
}
看看你是否收到错误。