Check what php version

How to Check the PHP Version

PHP is one of the most used server-side programming languages. There are some important differences between PHP versions, so knowing which version is running on your server might be necessary in some situations.

For example, if you are upgrading your application or installing a new one that requires a specific PHP version before starting with the installation, you’ll need to find out the version of your PHP server.

In this article, we’ll show you how to check what version of PHP your server is running.

Checking PHP version with phpinfo() #

The most reliable way of finding out what version of PHP is used for that specific website is to use the phpinfo() function, which prints various information about the PHP server, including its version.

In your website document root directory upload the following PHP file using a FTP or SFTP client:

Open your browser, go to yourdoman.com/phpinfo.php ,and the version of the PHP server will be displayed on your screen:

Once you find out what PHP version you have, either remove the file or restrict the access to it. Exposing your PHP configuration to the public can pose a security risk to your application.

Читайте также:  Forum page html code

There is also another function that you can use to find out the PHP version. Unlike phpinfo() , the phpversion() function prints only the version of the PHP server.

php echo 'PHP version: ' . phpversion(); 

Checking PHP version from the Command Line #

If you have SSH access to the server, you can use the PHP CLI binary to determine the version of your PHP.

To get the server version, invoke the php binary using the —version or -v option:

The command will output information about the PHP version and exit. In this example the version of the PHP server is 7.3.11 :

PHP 7.3.11-1~deb10u1 (cli) (built: Oct 26 2019 14:14:18) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.3.11, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.3.11-1~deb10u1, Copyright (c) 1999-2018, by Zend Technologies 

If there are multiple PHP versions installed on the server, when you run the php command, it will show the version of the default PHP CLI, which may not be the version of PHP used on the website.

Conclusion #

Determining the version of the PHP server is a relatively easy task.

In this guide, we have shown several different options about how to find the version of PHP that your server is currently running.

Feel free to leave a comment if you have any questions.

Источник

phpinfo

Outputs a large amount of information about the current state of PHP. This includes information about PHP compilation options and extensions, the PHP version, server information and environment (if compiled as a module), the PHP environment, OS version information, paths, master and local values of configuration options, HTTP headers, and the PHP License.

Because every system is setup differently, phpinfo() is commonly used to check configuration settings and for available predefined variables on a given system.

phpinfo() is also a valuable debugging tool as it contains all EGPCS (Environment, GET, POST, Cookie, Server) data.

Parameters

The output may be customized by passing one or more of the following constants bitwise values summed together in the optional flags parameter. One can also combine the respective constants or bitwise values together with the bitwise or operator.

phpinfo() options

Name (constant) Value Description
INFO_GENERAL 1 The configuration line, php.ini location, build date, Web Server, System and more.
INFO_CREDITS 2 PHP Credits. See also phpcredits() .
INFO_CONFIGURATION 4 Current Local and Master values for PHP directives. See also ini_get() .
INFO_MODULES 8 Loaded modules and their respective settings. See also get_loaded_extensions() .
INFO_ENVIRONMENT 16 Environment Variable information that’s also available in $_ENV .
INFO_VARIABLES 32 Shows all predefined variables from EGPCS (Environment, GET, POST, Cookie, Server).
INFO_LICENSE 64 PHP License information. See also the » license FAQ.
INFO_ALL -1 Shows all of the above.

Return Values

Always returns true .

Examples

Example #1 phpinfo() Example

// Show all information, defaults to INFO_ALL
phpinfo ();

// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo ( INFO_MODULES );

Notes

Note:

In versions of PHP before 5.5, parts of the information displayed are disabled when the expose_php configuration setting is set to off . This includes the PHP and Zend logos, and the credits.

Note:

phpinfo() outputs plain text instead of HTML when using the CLI mode.

See Also

  • phpversion() — Gets the current PHP version
  • phpcredits() — Prints out the credits for PHP
  • ini_get() — Gets the value of a configuration option
  • ini_set() — Sets the value of a configuration option
  • get_loaded_extensions() — Returns an array with the names of all modules compiled and loaded
  • Predefined Variables

User Contributed Notes 21 notes

A simple method to style your own phpinfo() output.

ob_start () ;
phpinfo () ;
$pinfo = ob_get_contents () ;
ob_end_clean () ;

// the name attribute «module_Zend Optimizer» of an anker-tag is not xhtml valide, so replace it with «module_Zend_Optimizer»
echo ( str_replace ( «module_Zend Optimizer» , «module_Zend_Optimizer» , preg_replace ( ‘%^.*(.*).*$%ms’ , ‘$1’ , $pinfo ) ) ) ;

This is necessary to obtain a W3C validation (XHTML1.0 Transitionnal).
phpinfo’s output is declared with that DTD :
— «System ID» has the wrong url to validate : «DTD/xhtml1-transitional.dtd» rather than «http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd»
— Some module names contains space and the function’s output use the name in anchors as ID and NAME. these attributes can’t be validated like that (unique name only).

ob_start (); // Capturing
phpinfo (); // phpinfo ()
$info = trim ( ob_get_clean ()); // output

// Replace white space in ID and NAME attributes. if exists
$info = preg_replace ( ‘/(id|name)(=[«\’][^ «\’]+) ([^ «\’]*[«\’])/i’ , ‘$1$2_$3’ , $info );

$info_doc = new DOMDocument ( ‘1.0’ , ‘utf-8’ );
/* Parse phpinfo’s output
* operator @ used to avoid messages about undefined entities
* or use loadHTML instead
*/
@ $info_doc -> loadXML ( $info );

$doc -> documentElement -> appendChild ( // Adding HEAD element to HTML
$doc -> importNode (
$info_doc -> getElementsByTagName ( ‘head’ )-> item ( 0 ),
true // With all the subtree
)
);
$doc -> documentElement -> appendChild ( // Adding BODY element to HTML
$doc -> importNode (
$info_doc -> getElementsByTagName ( ‘body’ )-> item ( 0 ),
true // With all the subtree
)
);

// Now you get a clean output and you are able to validate.
/*
echo ($doc->saveXML ());
// OR
echo ($doc->saveHTML ());
*/

// By that way it’s easy to add some style declaration :
$style = $doc -> getElementsByTagName ( ‘style’ )-> item ( 0 );
$style -> appendChild (
$doc -> createTextNode (
‘/* SOME NEW CSS RULES TO ADD TO THE FUNCTION OUTPUT */’
)
);

// to add some more informations to display :
$body = $doc -> getElementsByTagName ( ‘body’ )-> item ( 0 );
$element = $doc -> createElement ( ‘p’ );
$element -> appendChild (
$doc -> createTextNode (
‘SOME NEW CONTENT TO DISPLAY’
)
);
$body -> appendChild ( $element );

// to add a new header :
$head = $doc -> getElementsByTagName ( ‘head’ )-> item ( 0 );
$meta = $doc -> createElement ( ‘meta’ );
$meta -> setAttribute ( ‘name’ , ‘author’ );
$meta -> setAttribute ( ‘content’ , ‘arimbourg at ariworld dot eu’ );
$head -> appendChild ( $meta );

// As you wish, take the rest of the output and add it for debugging
$out = ob_get_clean ();

$pre = $doc -> createElement ( ‘div’ ); // or pre
$pre -> setAttribute ( ‘style’ , ‘white-space: pre;’ ); // for a div element, useless with pre
$pre -> appendChild ( $doc -> createTextNode ( $out ));
$body -> appendChild ( $pre );

$doc -> formatOutput = true ; // For a nice indentation
$doc -> saveXML ();

?>

All that could be done with only RegExp but I prefer the use of DOM for manipulating documents

Источник

How to Check PHP Version

Hosting providers are traditionally slow to adopt new PHP versions on their servers. The consequence of this is that many different PHP versions exist on the web at the same time.

If you are implementing new features, installing a new PHP-based app, or trying to locate a bug on your website, it is important to know which PHP version your web server is running.

In this tutorial, you will learn how to check your PHP version by running PHP code on your server or using the command line.

How to Check PHP Version

Check PHP Version by Running PHP Code

The simplest method to determine the PHP version running on your website is executing a PHP file that contains the following code:

Create the file using a text editor like gedit or Notepad, and upload it to your website’s document root directory.

Then open a web browser and type the full address of the file in the address bar. For example, if you uploaded a file titled phpinfo.php to the example.com root directory, you would go to:

http://www.example.com/phpinfo.php

The code above displays the PHP version without any further details, like in the output below:

Checking PHP version by using the phpversion command

If you need more details on your PHP configuration, such as system information, build date, server API, configuration file information, etc., upload a file containing the phpinfo() function:

When visited in the browser, this file shows the PHP version in the upper-left corner, followed by configuration data:

Checking PHP version by using the phpinfo command

Note: While phpinfo() is useful for debugging, the page features sensitive information about your system. Remove the file from the server once you finish using it.

For a list containing all the loaded PHP extensions and their versions, upload a file with the following code:

The output shows each extension in a separate line, including the version of the PHP core:

Checking versions of loaded PHP extensions

Check PHP Version Using the Command Line (Windows, Linux and macOS)

If you have permission to SSH into the remote server, use the command line to check the installed PHP version. This method is also useful for checking the PHP version installed locally.

2. The php -v command works on Linux, macOS, Windows, and other supported systems. Its output contains the PHP version number, build date, and copyright information.

Checking PHP version by using the command line in Ubuntu

Note: If there is more than one PHP version installed on the server, the php -v command shows the default command-line interface (CLI) version. This version is not necessarily the one that runs on the hosted websites.

Fix ‘PHP is not Recognized’ Error on Windows

On Windows, the PHP path is sometimes not recognized by the system, so the php -v command outputs the ‘php is not recognized’ error.

The

To solve this problem, set the PATH environment variable first.

1. Type the following command, replacing [location] with the path to your PHP installation.

Setting the PATH variable for php.exe in Windows

2. Typing php -v now shows the PHP version installed on your Windows system.

Note: Check out our other PHP guides such as How to Make a Redirect in PHP or 4 Different Types of Errors in PHP.

This article aimed to explain the common ways to check the PHP version on your server or local machine. The methods covered in this tutorial include running PHP code and using the command-line interface.

Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.

A PHP Error occurs when something is wrong in the PHP code. An error can be simple as a missing semicolon.

If a script is not written correctly, or if something unusual happens, PHP can generate an error message.

Источник

Оцените статью