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 ?

4
  • 1
    Why do you still have to deal with PHP 4 in the first place? Commented Aug 26, 2011 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. Commented Aug 26, 2011 at 11:42
  • why does PHP have to "parse" the code first, why doesn't just execute it? Commented Aug 26, 2011 at 11:45
  • Parsing is part of the execution process. Commented Aug 26, 2011 at 12:21

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

This, of course, is the correct solution. Correct but for the typo, that is ;)
Will not work because the parse error triggers before any code is executed.
sure, that must part of the bootstrap process
0

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

1 Comment

Will not work because the parse error triggers before any code is executed.
0
if ((int)phpversion() < 5){
  error_reporting(0);
}

1 Comment

Will not work because the parse error triggers before any code is executed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.