Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can code support two versions of PHP?

Status
Not open for further replies.

suheil

Programmer
Dec 15, 2005
3
US
Hi

Is it possible to have PHP code support two versions of PHP? In particular, in PHP 4 I have code of the form
Code:
$obj->__clone();
Now, in PHP 5 that needs to change to:
Code:
clone $obj;

Is it possible to write code for this which will run on both PHP 4 and PHP 5? As a C programmer, I am thinking of something like:
Code:
  #if PHP_VERSION<5
      $obj->__clone();
  #else
      clone $obj;
  #endif

Thanks.
 
You can use: phpversion()

The function returns a string containing the version of the currently running PHP parser. Which you can then manipulate to use in conditional statements.
 
Thanks. I tried that, but it seems its doesn't quite do what I'm looking for.
Something like:
Code:
       $phpversion = strval(substr(phpversion(),0,1));
       if ($phpversion<=4)
           echo "Version 4 or lower";
       else
           echo "Version 5 or higher";
works as expected.

But the following gives a parse error (under PHP4, the parse error is thrown by the PHP5 code, and vice-versa):
Code:
       $phpversion = strval(substr(phpversion(),0,1));
       if ($phpversion<=4)
           $dupl = $curr->__clone();
       else
           $dupl = clone $curr;

It seems the PHP parser wants to be able to parse all code in the file before it will display anything. (With "#if" in C-programming, the compiler only processes the relevant code as determined by the conditional.
 
could you try moving the relevant calls out to an included file and then picking the relevant file out in your version picker script:

Code:
$phpversion = strval(substr(phpversion(),0,1));
       if ($phpversion<=4):
           require "php4include.php";
       else:
           require "php5include.php";
       endif;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top