{"id":947,"date":"2024-11-27T05:51:09","date_gmt":"2024-11-27T00:21:09","guid":{"rendered":"https:\/\/webhostingbengaluru.co.in\/tutorials\/?p=947"},"modified":"2024-11-27T05:51:11","modified_gmt":"2024-11-27T00:21:11","slug":"how-to-check-php-version","status":"publish","type":"post","link":"https:\/\/webhostingbengaluru.co.in\/tutorials\/how-to-check-php-version\/","title":{"rendered":"How to Check PHP Version by Running Code"},"content":{"rendered":"\n<p>Hosting providers are traditionally slow to adopt new\u00a0PHP\u00a0versions on their servers. Consequently, multiple different PHP versions can exist on the server at the same time.<\/p>\n\n\n\n<p>If you are implementing new features, installing a new PHP-based app, or trying to locate a bug on your\u00a0website, it is important to know which PHP version your\u00a0web server\u00a0is running.<\/p>\n\n\n\n<p>In this article, you will learn how to check your PHP version on a local machine, server, or <a href=\"https:\/\/wordpress.com\/\" target=\"_blank\" rel=\"noopener\">WordPress<\/a> website.<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#check-php-version-by-running-php-code\">Check PHP Version by Running PHP Code<\/a><ul><li><a href=\"#method-1-using-phpinfo\">Method 1: Using phpinfo()<\/a><\/li><li><a href=\"#method-2-using-phpversion\">Method 2: Using phpversion()<\/a><\/li><li><a href=\"#method-3-using-the-php-version-constant\">Method 3: Using the PHP_VERSION Constant<\/a><\/li><li><a href=\"#method-4-command-line-script\">Method 4: Command-Line Script<\/a><\/li><li><a href=\"#best-practices\">Best Practices<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"check-php-version-by-running-php-code\">Check PHP Version by Running PHP Code<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"318\" height=\"159\" src=\"https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-content\/uploads\/2024\/11\/images.jpg\" alt=\"\" class=\"wp-image-953\" style=\"width:468px;height:auto\" srcset=\"https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-content\/uploads\/2024\/11\/images.jpg 318w, https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-content\/uploads\/2024\/11\/images-300x150.jpg 300w\" sizes=\"(max-width: 318px) 100vw, 318px\" \/><\/figure>\n\n\n\n<p>To check your PHP version by running code, you can use one of the following methods:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"method-1-using-phpinfo\">Method 1: Using <code>phpinfo()<\/code><\/h3>\n\n\n\n<p>The <code>phpinfo()<\/code> function outputs information about your PHP configuration, including the PHP version.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nphpinfo();\n?><\/code><\/pre>\n\n\n\n<p><strong>Steps:<\/strong><\/p>\n\n\n\n<ol>\n<li>Save the above code in a file named <code>info.php<\/code> or something similar.<\/li>\n\n\n\n<li>Place the file in your <a href=\"https:\/\/www.webhostingbengaluru.co.in\/vps-hosting\/\">web server<\/a> document root (e.g., <code>\/var\/www\/html<\/code>).<\/li>\n\n\n\n<li>Open the file in a browser by visiting <code>http:\/\/your-domain.com\/info.php<\/code>.<\/li>\n\n\n\n<li>Look for the PHP version displayed at the top of the page.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"method-2-using-phpversion\">Method 2: Using <code>phpversion()<\/code><\/h3>\n\n\n\n<p>The <code>phpversion()<\/code> function returns the current PHP version as a string.<\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\necho 'Current PHP version: ' . phpversion();\n?><\/code><\/pre>\n\n\n\n<p><strong>Steps:<\/strong><\/p>\n\n\n\n<ol>\n<li>Save this code in a file, for example, <code>version.php<\/code>.<\/li>\n\n\n\n<li>Place the file in your web server&#8217;s document root.<\/li>\n\n\n\n<li>Access it through your browser (e.g., <code>http:\/\/your-domain.com\/version.php<\/code>).<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"596\" src=\"https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-content\/uploads\/2024\/11\/php-8-2.jpg\" alt=\"\" class=\"wp-image-955\" style=\"width:497px;height:auto\" srcset=\"https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-content\/uploads\/2024\/11\/php-8-2.jpg 1024w, https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-content\/uploads\/2024\/11\/php-8-2-300x175.jpg 300w, https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-content\/uploads\/2024\/11\/php-8-2-768x447.jpg 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"method-3-using-the-php-version-constant\">Method 3: Using the <code>PHP_VERSION<\/code> Constant<\/h3>\n\n\n\n<p>PHP provides a predefined constant, <code>PHP_VERSION<\/code>, that holds the current version of PHP.<\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\necho 'Current PHP version: ' . PHP_VERSION;\n?><\/code><\/pre>\n\n\n\n<p><strong>Steps:<\/strong> Same as Method 2.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"method-4-command-line-script\">Method 4: Command-Line Script<\/h3>\n\n\n\n<p>If you have access to the command line, you can write and execute a PHP script directly.<\/p>\n\n\n\n<p><strong>Command:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php -r \"echo 'Current PHP version: ' . phpversion() . PHP_EOL;\"<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"577\" src=\"https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-content\/uploads\/2024\/11\/Best-Web-Servers-for-PHP-Development-1024x577.jpg\" alt=\"\" class=\"wp-image-958\" style=\"width:563px;height:auto\" srcset=\"https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-content\/uploads\/2024\/11\/Best-Web-Servers-for-PHP-Development-1024x577.jpg 1024w, https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-content\/uploads\/2024\/11\/Best-Web-Servers-for-PHP-Development-300x169.jpg 300w, https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-content\/uploads\/2024\/11\/Best-Web-Servers-for-PHP-Development-768x433.jpg 768w, https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-content\/uploads\/2024\/11\/Best-Web-Servers-for-PHP-Development.jpg 1080w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"best-practices\">Best Practices<\/h3>\n\n\n\n<ul>\n<li><strong>Security:<\/strong> Avoid leaving files like <code>info.php<\/code> on your server in production, as they expose sensitive information about your configuration.<\/li>\n\n\n\n<li><strong>Testing:<\/strong> Use these methods in a controlled environment to avoid exposing your server to unnecessary risks.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hosting providers are traditionally slow to adopt new\u00a0PHP\u00a0versions on their servers. Consequently, multiple different PHP versions can exist on the server at the same time. <a href=\"https:\/\/webhostingbengaluru.co.in\/tutorials\/how-to-check-php-version\/\" class=\"read-more-link\">[Read More&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":951,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-json\/wp\/v2\/posts\/947"}],"collection":[{"href":"https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-json\/wp\/v2\/comments?post=947"}],"version-history":[{"count":6,"href":"https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-json\/wp\/v2\/posts\/947\/revisions"}],"predecessor-version":[{"id":959,"href":"https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-json\/wp\/v2\/posts\/947\/revisions\/959"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-json\/wp\/v2\/media\/951"}],"wp:attachment":[{"href":"https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-json\/wp\/v2\/media?parent=947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-json\/wp\/v2\/categories?post=947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostingbengaluru.co.in\/tutorials\/wp-json\/wp\/v2\/tags?post=947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}