PHP is a popular programming language that runs on servers and often is used to generate the HTML code sent to your browser. It is a scripting language, so it doesn’t have to be compiled, and it is generally considered an easy programming language to learn and use.
Originally created in 1994, PHP is now on version 8.1 and has evolved substantially since that time. PHP powers a large portion of the web, and this is the language used to make the incredibly popular WordPress blogging and content management system.
How to Write PHP Code
PHP code starts with a
<?php tag to specify that the following code should be interpreted as PHP. To end a section of PHP code, use
?>. The end tag is only required if you need to write code after that is not interpreted as PHP, such as some HTML.
How to Write Comments in PHP
To write a line comment that is not interpreted, begin a line with
//. This type of comment ends at the end of the line.
If you want to write a comment that spans multiple lines, you can begin a comment with
/* and end with
Variables in PHP begin with a dollar sign like
Because PHP is loosely typed you do not need to specify what kind of variable it is (like an integer or string) like in some other languages.
Each statement (typically a single line of code) must end with a semicolon
The following assignment statement sets the variable
1 and must end with a semicolon, or else there will be an error.
$foo = 1;
Example of a Simple PHP Web Page
"Hello World!"; <html> <body> // The ‘echo’ statement will output the value of the variable. echo $message; </body> </html>$message =
The above will print “Hello World!” into a simple web page. This is how easy it is to use a little PHP in a web page.
How Do I Run My PHP Code?
To create a web page, you simply have to write some HTML code into a text file using a text editor like notepad, or better, a programmer’s editor (also known as an Integrated Development environment, or IDE) like the excellent and free Visual Studio Code. You can then open the webpage in a browser like Google Chrome.
PHP is written in the same way but opening the code in a browser will not interpret the PHP code. Why not?
This is because the code must be interpreted by the PHP interpreter that must be installed on the computer or server where the code lives. If you wish to try writing a program of any complexity, you probably want to install PHP on your computer so you can easily do this, but just to play around and try writing a few lines of code, you can try a PHP playground like Teh Playground. (Yes, spelled that way).
Why Should I Learn PHP?
Consequently, if you are looking to become a backend or full-stack web developer, PHP is an excellent language to know.
Additionally, PHP is also a very actively maintained language that gets continually better. It has an excellent ecosystem of packages available (via packagist.org) that you can easily use to add functionality to a project with the Composer package manager, and a robust community of developers who are interested in helping each other learn and solve problems.
I hope this brief introduction to the PHP programming language has been informative for you. Please let me know in the comments if you have any questions or would like clarification on any of the points in this post!