|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: Introduction to PHP |
| 4 | +--- |
| 5 | + |
| 6 | +##### Requirements |
| 7 | + |
| 8 | +* 15 minutes |
| 9 | +* PHP installed - this is by default on most Operating Systems. But if not, here are the official [installation instructions](http://php.net/manual/en/install.php) |
| 10 | + * Check installation by running the following command in cli `php -v`, this should output the version, eg. **5.5.9** |
| 11 | + |
| 12 | +##### Achievements |
| 13 | + |
| 14 | +By the end of this tutorial you will be able to: |
| 15 | + |
| 16 | +* write a **Hello World** Application that outputs on the: |
| 17 | + * Command Line (CLI) |
| 18 | + * Browser |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## What is PHP? |
| 23 | + |
| 24 | +From the **PHP** [website](http://php.net/manual/en/intro-whatis.php) |
| 25 | + |
| 26 | +> 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. |
| 27 | +
|
| 28 | +Well what does this mean? It is very easy to build dynamic websites using **PHP** as the server-side language, which generates **HTML**. |
| 29 | + |
| 30 | +## What does PHP look like? |
| 31 | + |
| 32 | +A simple Hello World application would look like the following: |
| 33 | + |
| 34 | +*File: index.php* |
| 35 | +```php |
| 36 | +<?php |
| 37 | +echo "Hello World"; |
| 38 | +``` |
| 39 | + |
| 40 | +1. The file must end with the extension `.php` |
| 41 | +2. The file must begin with the opening tag `<?php` |
| 42 | +3. `"` are used to Open/Close a string |
| 43 | +4. 'echo' Outputs the string [echo on php website](http://php.net/manual/en/function.echo.php) |
| 44 | + |
| 45 | +## How to run the script? |
| 46 | + |
| 47 | +On the Command Line (CLI), type: |
| 48 | + |
| 49 | +```php |
| 50 | +php index.php |
| 51 | +``` |
| 52 | + |
| 53 | +And the output will be: |
| 54 | + |
| 55 | +```bash |
| 56 | +$ php index.php |
| 57 | +Hello World |
| 58 | +``` |
| 59 | + |
| 60 | +*Note: `$` implies the Command Line shell, you did not need to type this.* |
| 61 | + |
| 62 | +## How to the output in the browser? |
| 63 | + |
| 64 | +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. |
| 65 | + |
| 66 | +*Note: Built-in PHP WebServer is great for Development, but **NOT** Production.* |
| 67 | + |
| 68 | +Go to the directory where you created the `index.php` file and run the following command: |
| 69 | + |
| 70 | +```bash |
| 71 | +$ php -S 0.0.0.0:8080 |
| 72 | +``` |
| 73 | + |
| 74 | +1. `php` is the same command from before, that we used to run our **php** script |
| 75 | +2. `-S` means built-in WebServer |
| 76 | +3. `0.0.0.0` is the IP that the WebServer should listen on. By using `0` it will listen on everything - fine for development |
| 77 | +4. `8080` is the port to listen on - fine for development but in production the default port is `80` and therefore not required when accessing a URL |
| 78 | + |
| 79 | +Lets see the script output in your web browser! In your web browser navigate to ` http://localhost:8080/` and you should see: |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +## Summary |
| 84 | + |
| 85 | +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. |
0 commit comments