Some of the services and websites that use PHP include WordPress, Facebook, Wikipedia, Drupal, Tumblr and Slack. It’s one of the most popular web development languages in the world. In January 2013, it was used by more than 240 million websites – 39% of the websites sampled. WordPress itself, as well as most plugins and themes built for it, rely on PHP to manipulate and output data retrieved from the MySQL database.
The following is a simple example of PHP code. It outputs the text “Hello World” within the HTML paragraph element.
<p><?php echo 'Hello World'; ?></p>
The code <?php
indicates to the PHP interpreter on the server that PHP code will follow. The echo
function says that whatever follows should be output as HTML, and whatever follows, in this case, is the text string "Hello World"
. The semicolon indicates that the statement has ended, and the ?>
tells the PHP interpreter to stop interpreting the text as PHP code. If you visit a web page with this piece of code on it, it will simply display the words “Hello World”.
You can see a more complex example below:
<?php $variable = 'Hello World!'; if ( $variable ) { echo $variable; } else { echo 'That variable is empty'; } ?>
This piece of code will also output “Hello World”, but the output is wrapped in a conditional – a control gate which determines whether something is true or false. In this case, we assign the text “Hello World” to the variable $variable
. The if/else
statement that follows checks if the variable has a value. In this case it does, so the variable – holding the text “Hello World” – is output. If it didn’t have a value, the if/else
statement would evaluate to false, and the code within the else {}
part of the statement would be executed instead. In this example, that code would output the words “That variable is empty”.
PHP is a server-side scripting language. That means that the PHP code is executed on the server, where the website is hosted, and the results are served to the browser – usually as HTML. The latest big release is PHP 7.0, which was released in December 2015. WordPress requires PHP 5.2.4, but recommends version 7.2.