After a rather boring tutorial in JavaScript, we make our way to the language of backends everywhere, PHP. Hunker down because it’s time to implement Hello World in PHP.
Table of Contents
PHP Background
No tutorial would be complete at this point without a little bit of Wikipedia research.
According to Wikipedia, PHP is a server-side scripting language developed around the same time as JavaScript in 1995. Back then, PHP was designed to help with maintaining personal home pages which is how the language got its name.
Today, PHP is used all over the web for website backends. In other words, PHP generally handles all the heavy lifting like database management and HTML generation. Meanwhile, our old friend JavaScript handles the interactive elements on the frontend.
In terms of features, PHP is fairly limited. After all, PHP was based on C, so the syntax tends to be similar. That said, PHP does offer some core libraries that make sense for web development. For instance, PHP includes functionalities for hashing, json, xml, and glob.
Regardless, PHP still takes a lot of criticism. Much like JavaScript, PHP is often mocked for its inconsistency and brokenness. And, the hilarious part is that both languages dominate the web.
Hello World in PHP
As always, let’s get down to business with an implementation of Hello World in PHP:
<?php echo "Hello, World!"; ?>
And, that’s it. In fact, we can shorten this implementation:
<?="Hello, World!";
I’ve read that leaving the PHP tag open in the second example is the preferred method for pure PHP files. At any rate, let’s dive into the code a bit.
If we use our first implementation as an example, the first thing we’ll see is a PHP tag. The PHP tag exists because the code is actually embedded in an HTML file. In other words, the PHP tag indicates to the PHP interpreter that we have code for processing. Otherwise, the interpreter outputs the raw text.
After that, we have a bit of PHP code which allows us to write our string to the web page. In this case, the print command is echo. That might sound familiar for anyone who has done some command line work because echo is the same command used in command line scripting. More on that when we get to shell scripting.
Finally, we have the closing PHP tag. This signals to the interpreter that we are done processing PHP code. Of course, in the second example, we omit the closing tag. This is done for a variety of reasons, but it’s mainly done to eliminate extra space issues after the tag.
How to Run the Solution
As usual, we can run the solution using an online PHP interpreter. Just drop the code into the editor and hit run.
Alternatively, we have to launch up a local server, so we can view the webpage the PHP produces in our browser. From what I understand, a lot of people use XAMPP which is an all-in-one kit for PHP development.
Sample Programs in Every Language
And, that’ll do it for Hello World in PHP. With Ruby, JavaScript, and PHP out of the way, I think we’ve covered about everything relevant to web development. If you have any requests, feel free to leave them in the comments.
For now, I want to start playing with some older functional languages like Lisp and Haskell. Then, I’m thinking about diving into some newer ones like Elixir and Scala. How does that sound?
Once again, if you enjoyed this tutorial, let me know in the comments. I’m always looking to improve these as I continue my journey.
Recent Posts
Recently, I was thinking about the old Pavlov's dog story and how we hardly treat our students any different. While reflecting on this idea, I decided to write the whole thing up for others to read....
In the world of programming languages, expressions are an interesting concept that folks tend to implicitly understand but might not be able to define. As a result, I figured I'd take a crack at...