PHP Version number issue

43 views Asked by At

I am not sure why the following code is showing me an error message in PHP 5.2 but it is working perfectly in php 5.4

$f_channelList = array();
    $f_channelCounter = 0;
    $f_channel = null;
    foreach ($f_pageContent->find("div.col") as $f_channelSchedule){
        $f_channel = $f_channelSchedule->find("h2.logo")[0];//error here
        if(trim($f_channel->plaintext) != " " && strlen(trim($f_channel->plaintext))>0){
            if($f_channelCounter == 0){
                mkdir($folderName);
            }
            array_push($f_channelList, $f_channel->plaintext);
            $f_fileName = $folderName . "/" . trim($f_channelList[$f_channelCounter]) . ".txt";
            $f_programFile = fopen($f_fileName, "x");
            $f_fileContent = $f_channelSchedule->find("dl")[0]->outertext;
            fwrite($f_programFile, $f_fileContent);
            fclose($f_programFile);
            $f_channelCounter++;
        }
    }

Also, I am using simple_html_dom.php (html parser api) in my code to parse a html page. When I run this code on PHP 5.2 it shows me an error message at "//error here" stating "parse error at line number 67"

Thanks

2

There are 2 answers

0
Amal Murali On BEST ANSWER

You're having:

$f_channel = $f_channelSchedule->find("h2.logo")[0]; 
                                                ^^^

Array dereferencing is a PHP 5.4+ feature, and that's the reason why you're getting this error. You'd have to use a temporary variable if you want this code to work on previous versions of PHP:

$temp = $f_channelSchedule->find("h2.logo");
$f_channel = $temp[0];

Refer to PHP manual for more details.

0
jeroen On

You cannot access the result of a function call like that in php 5.2.

According to the manual:

As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.