1

I have a script that uses a PHP 5 syntax that's apparently not supported in PHP 4.

like MyClass::method()->method(...)

I'm trying to display a error at the beginning of the script telling that the server doesn't have PHP 5 installed, and "die" nicely, but I can't because I get that stupid parse error...

So how can I make PHP ignore errors if < 5 ?

Jaaa
  • 13
  • 2
  • 1
    Why do you still have to deal with PHP 4 in the first place? – Pekka Aug 26 '11 at 11:40
  • Cannot work around parser failures. You can check the `PHP_VERSION` constant though and write a nicer error message. But not in the same script, you have to test it **before** the potentially failing code gets invoked. – mario Aug 26 '11 at 11:42
  • why does PHP have to "parse" the code first, why doesn't just execute it? – Jaaa Aug 26 '11 at 11:45
  • Parsing is part of the execution process. – mario Aug 26 '11 at 12:21

4 Answers4

5

The easiest way I can think of is to have one file that includes another if PHP5 is installed:

//index.php
if (intval(substr(phpversion(), 0, 1)) < 5) {
    die ('you must have PHP 5 installed');
} else {
    include('main.php');
}

//main.php
MyClass::method()->method(); // or whatever
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
2
    if (version_compare(PHP_VERSION, '5.0.0', '<')) {
        die('PHP is to old.');
    }

http://de3.php.net/manual/en/function.version-compare.php

http://www.php.net/manual/en/reserved.constants.php#reserved.constants.core

or

http://php.net/manual/en/function.phpversion.php

coding Bott
  • 4,287
  • 1
  • 27
  • 44
0

It can't ignore the errors, however it can die nicely if error_reporting(0);

lukas.pukenis
  • 13,057
  • 12
  • 47
  • 81
0
if ((int)phpversion() < 5){
  error_reporting(0);
}
k102
  • 7,861
  • 7
  • 49
  • 69