Hello World in PHP

Hello World in PHP Featured Image

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 researchOpens in a new tab..

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 criticismOpens in a new tab.. 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:

  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 tagOpens in a new tab..

How to Run the Solution

As usual, we can run the solution using an online PHP interpreterOpens in a new tab.. 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 XAMPPOpens in a new tab. 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.

Sample Programs in Every Language (44 Articles)—Series Navigation

For 100 Days of Code, I’ve decided to implement a few sample programs in as many languages as possible. Each implementation details a brief history of the language and a description of the code.

The plan for the series is to explore the major general-purpose language like Java, Python, C, C++, and C#. From there, we’ll take a look at some sample programs in web development languages like Ruby, PHP, and JavaScript. As we continue, we’ll cover proprietary languages like Swift and Objective-C. Eventually, we’ll start to tackle less popular languages like Rust, x86, and Verilog. Finally, we’ll play around with some of the esoteric languages like Brainf*ck and LOLCODE.

Who knows? Maybe the Sample Programs in Every Language series will become so popular it’ll never end. To help this series grow, consider sharing it on social media with your friends. Or, if you have a language you want to see, drop your suggestion in the comments.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts