Php通知:使用未定义的常量

时间:2011-02-17 02:34:02

标签: constants 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' 

2 个答案:

答案 0 :(得分:2)

更改

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

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

答案 1 :(得分:0)

你只需要重写

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

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

你甚至可以使用

foreach($pie as $thi){