If you are going to learn to code with PHP for the web, you will eventually need to set up a development (“dev”) machine. This means configuring a web server like Apache HTTP Server along with PHP. If you want to complete the exercise without having a dev box set up, head over to the fabulous online W3C PHP Compiler: W3Schools Tryit Editor.
In this tutorial:
Required knowledge:
Just another text file
Create a PHP file. PHP files are text files with the .php extension. You can code using any text editor, Notepad, Notepad++, or VSCode. For your first steps, you may find the process easier to follow using just Notepad.
Create a file called index.php and open it in your code editor.
Enter the following code (line-by-line explanations follow):
<?php echo 'Hello World!'; ?>
- All your PHP code will start with
<?php
- echo is a PHP function that outputs a text string; the text string is between quotes, in this instance, Hello World!
- your PHP code ends with
?>
Save your file and open the index.php
file in your browser (the file can be open in your text editor and your browser at the same time).
Add some HTML
Bored already? Well, how about combining your HTML skills with your new PHP skills:
<?php echo '<h1>Hello World!</h1>'; phpinfo(); ?>
References:
- W3Schools online PHP editor (no date) W3Schools Online Web Tutorials. Available at: https://www.w3schools.com/php/phptryit.asp?filename=tryphp_compiler (Accessed: 25 November 2023).
- Wikipedia contributors. (2023, November 25). PHP. In Wikipedia, The Free Encyclopedia. Retrieved 18:20, November 25, 2023, from https://en.wikipedia.org/w/index.php?title=PHP&oldid=1186790181
- Buckler, C. (2022) How to install PHP on Windows 10 and 11 (with Apache & MySQL), SitePoint. Available at: https://www.sitepoint.com/how-to-install-php-on-windows/ (Accessed: 08 January 2024).