有什麼辦法可以重寫下面的代碼,以便它可以在運行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