大挖在開(kāi)發(fā)某款wordpress主題的時(shí)候通常是在公司與家里同時(shí)開(kāi)工追進(jìn)度,在某天操作wordpress程序文件時(shí)打開(kāi)首頁(yè)突然出現(xiàn)了這樣的提示W(wǎng)arning:Illegal string offset,簡(jiǎn)單測(cè)試了一下是因?yàn)閜hp環(huán)境版本的問(wèn)題,最后通過(guò)本地的環(huán)境版本切換最終確定了問(wèn)題只出現(xiàn)于PHP5.4以上環(huán)境。那我們分析下原因和解決辦法。
解決方案一
缺少 isset 而出現(xiàn)警告。當(dāng)訪問(wèn)未定義變量時(shí),PHP 會(huì)產(chǎn)生警告;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
$artists = get_artists_all(); foreach ($artists as $artists_id => $artist) { if (isset($_GET["id"])) { $artists_id = $_GET["id"]; if(isset($artists["$artists_id"])){ $artist = $artists[$artists_id]; } } if (!isset($artist)){ header("Location:".BASE_URL."artists/"); exit(); } foreach ($artist as $work) { if (isset($_GET["id"])) { $artist_id = $_GET["id"]; if(isset($artist["$artist_id"])){ 在 $work = $artist[$artist_id]["id"]; } } if (!isset($work)){ header("Location:".BASE_URL."artist/"); exit(); } } } ...... foreach ($artist as $work){ ...... } |
改成:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
foreach ($artist as $work) { if (isset($_GET["id"])) { $artist_id = $_GET["id"]; if(isset($artist["$artist_id"])){ $work = $artist[$artist_id]["id"]; } } if (!isset($work)){ header("Location:".BASE_URL."artist/"); exit(); } |
解決方案二
在php文件首行加入error_reporting(0);
1、在PHP手冊(cè)中搜索到函數(shù)error_reporting(0);官方解釋是Turn off all error reporting,于是把error_reporting(0);加到PHP程序的首行,運(yùn)行后果然沒(méi)出現(xiàn)任何錯(cuò)誤提示!
2、在php文件的首行加入error_reporting(E_ALL ^ E_NOTICE);,畢竟不能因?yàn)橐粋€(gè)錯(cuò)誤關(guān)閉所有的錯(cuò)誤警告。
解決方案三
有關(guān)error_reporting()函數(shù):
error_reporting(0);//禁用錯(cuò)誤報(bào)告
error_reporting(E_ALL ^ E_NOTICE);//顯示除去 E_NOTICE 之外的所有錯(cuò)誤信息
error_reporting(E_ALL^E_WARNING^E_NOTICE);//顯示除去E_WARNING E_NOTICE 之外的所有錯(cuò)誤信息
error_reporting(E_ERROR | E_WARNING | E_PARSE);//顯示運(yùn)行時(shí)錯(cuò)誤,與error_reporting(E_ALL ^ E_NOTICE);效果相同。error_reporting(E_ALL);//顯示所有錯(cuò)誤
以上問(wèn)題應(yīng)當(dāng)是把我們的Php升級(jí)大于5.4造成的,希望以上方法對(duì)你有所幫助。