- 15 minutes
- PHP installed - PHP comes packaged in various ways. Please use our installation guide for an in-depth guide to the options. This tutorial assumes you have installed the command-line version. If you use other methods, you may not need to start the PHP WebServer.
By the end of this tutorial you will be able to:
- write a Hello World Application that outputs on the:
- Command Line (CLI)
What is PHP?
From the PHP website
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.
Well what does this mean? It is very easy to build dynamic websites using PHP as the server-side language, which generates HTML.
What does PHP look like?
A simple Hello World application would look like the following:
<?php echo "Hello World";
- The file must end with the extension
- The file must begin with the opening tag
"are used to Open/Close a string
echoOutputs the string echo on php website
;must end every statement
How to run the script?
On the Command Line (CLI), type:
And the output that displays just below where you typed will be:
How to the output in the browser?
Seeing it on the Command Line is great, but what about the browser? We will need to get a WebServer running, the easiest way is to use the built-in PHP WebServer.
Note: Built-in PHP WebServer is great for Development, but NOT Production.
Go to the directory where you created the index.php file and run the following command:
$ php -S 0.0.0.0:8080
phpis the same command from before, that we used to run our php script
-Smeans built-in WebServer
0.0.0.0is the IP that the WebServer should listen on. By using
0it will listen on everything - fine for development
8080is the port to listen on - fine for development but in production the default port is
80and therefore not required when accessing a URL
Lets see the script output in your web browser! In your web browser navigate to http://localhost:8080/ and you should see:
Now you should know how to create a simple PHP script and run it via the Command Line or via the Built-in PHP WebServer and see the output to the Command Line or the Browser respectively.