tags:

views:

132

answers:

8

Is there a way to check the version of PHP that executed a particular script from within that script? So for example, the following snippet

$version = way_to_get_version();
print $version;

would print 5.3.0 on one machine, and 5.3.1 on another machine.

+13  A: 
$version = phpversion();
print $version;

Documentation

However, for best practice, I would use the constant PHP_VERSION. No function overhead, and cleaner IMO.

Also, be sure to use version_compare() if you are comparing PHP versions for compatibility.

alex
It's just called `version_compare()`.
too much php
Thanks, that's what I get for recalling from memory!
alex
http://www.php.net/manual/en/function.phpversion.php#84424 i would always use `PHP_VERSION` for best practice
RobertPitt
+4  A: 

Take a look at phpversion().

echo "Current version is PHP " . phpversion();
Owen
+4  A: 

phpversion() will tell you the currently running PHP version.

Jordan
+4  A: 

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

Returns exactly the "5.3.0".

Nican
+4  A: 

Technically the best way to do it is with the constant PHP_VERSION as it requires no function call and the overhead that comes with it.

echo PHP_VERSION

Variables are always faster then function calls.

John Conde
Variables?! That's a constant!
Alix Axel
lol. Sure, if you want to get TECHNICAL!
John Conde
On the programming website, yes, I would prefer to get technical.
Matchu
@Matchu So Stack Overflow is **the** programming website now?
alex
In this context, yes, since I'm referring to a definite programming website in the scope of this conversation, hence the definite article :)
Matchu
Wouldn't you want to get technical on *all* programming sites?
alex
+4  A: 

You can either use the phpversion() function or the PHP_VERSION constant.

To compare versions you should always rely on version_compare().

Alix Axel
+1  A: 

phpversion() is one way. As John conde said, PHP_VERSION is another (that I didn't know about 'till now).

You may also be interested in function_exists()

gabrielk
+1  A: 

.........

if (version_compare(phpversion(), '5', '>='))
{
       // act accordintly
}
Sarfraz