2011-02-17 34 views
0

我做錯了什麼?Php注意:使用undefined常量

 
echo try_add("test","2562782"); 
echo try_add("test","25627826"); 
function try_add($ct,$tankid){ 
    $file = 'testFile_'.$ct.'.txt'; 
    $pizza = file_get_contents($file); 
    preg_match_all("/Tankid:(?P.*?);/", $pizza, $pie); 
//print_r($pie); 
//outputs 
//Array 
//(
// [0] => Array 
//  (
//   [0] => Tankid:2562782; 
//   [1] => Tankid:25627826; 
//   [2] => Tankid:2562782; 
//   [3] => Tankid:25627826; 
//  ) 
// 
// [this] => Array 
//  (
//   [0] => 2562782 
//   [1] => 25627826 
//   [2] => 2562782 
//   [3] => 25627826 
//  ) 
// 
// [1] => Array 
//  (
//   [0] => 2562782 
//   [1] => 25627826 
//   [2] => 2562782 
//   [3] => 25627826 
//  ) 
// 
//) 

    foreach($pie[this] as $thi){ 
     if($thi == $tankid){ 
      $inthis = "yes"; 
     } 
     else{ 
      $inthis = "no"; 
     } 
    } 
    if($inthis == "no"){ 
     $myFile = "testFile_$ct.txt"; 
     $fh = fopen($myFile, 'a'); 
     $stringData = "Tankid:$tankid;\n"; 
     fwrite($fh, $stringData); 
     fclose($fh); 
     return "This '$tankid' has been added in this file '$myFile' \n"; 
    } 
    else{ 
     return "This '$tankid' is already in this file '$myFile' \n"; 
    } 
} 



//Code outputs 
//Notice: Use of undefined constant this - assumed 'this' in \xampp\htdocs\new.php on line 39 
//This '2562782' has been added in this file 'testFile_test.txt' 
//Notice: Use of undefined constant this - assumed 'this' in \xampp\htdocs\new.php on line 39 
//This '25627826' has been added in this file 'testFile_test.txt' 

+0

見http://stackoverflow.com/questions/2941169/mysql-php-use-of-undefined-constant –

回答

0

你只需要重寫

foreach($pie[this] as $thi){ 

foreach($pie[$this] as $thi){ 

你甚至可以使用

foreach($pie as $thi){ 
+0

$餡餅[這]不是調用數組 IM的一部分變量 –

+0

它沒有問題。這只是從PHP的警告,它假設$ – atx

+0

我得到更多的錯誤使用$餅[$ this] –

2

變化

foreach($pie[this] as $thi){ 

進入

foreach($pie['this'] as $thi){ 
+0

嘗試也doesent工作(在新的xamp),不,他們確定我的腳本正在工作(在託管eviroment),所以是回到舊xamp –

+0

@ user479911 - 謝謝。 '單引號'幫助了我 – Davis

相關問題