Hackernoon logoHow to Write Your First PHP Code by@Bairesdev

How to Write Your First PHP Code

image
BairesDev Hacker Noon profile picture

@BairesdevBairesDev

Powerful insights in business, technology, and software development.

At some point, your company is going to have to employ numerous programming languages to get the job done. And given your business probably makes heavy use of software for your various channels and pipelines, having software engineers with the right skills is tantamount to success.

This is why education is key to creating a team of developers that can handle any task you throw at them. With a properly educated team, when you need a project that relies heavily on the likes of PHP, you can be sure they'll be ready for the job.

Good thing PHP is one of the most user-friendly, easy to learn languages available. To show you how easy PHP is to learn, let's take a look at getting started with the language.

What is PHP?

First, it's important to understand what PHP is, as it's not quite the same as some of the more traditional languages. PHP stands for PHP: Hypertext Preprocessor and is a widely-used, open source server-side scripting language (which means the code is executed on the server-side of things and not on the client side). 

One of the biggest differences between PHP and other languages is that it doesn't require a compiler. Instead, you "run what you write." In other words, you write a PHP script, and then, using the PHP program itself, you run the script. This means you don't have to use external applications to compile the code into an executable binary.

PHP scripts can contain text, HTML, CSS, JavaScript, and (of course) PHP code, and use the .php file extension. PHP can be used for:

  • Generating dynamic web page content.
  • Creating, opening, reading, writing, deleting, and closing files on a server.
  • Collecting form data.
  • Sending and receiving cookies.
  • Adding, deleting, and modifying data in a database.
  • Controlling user-access.
  • Encrypting data.

With that said, let's learn how to write your first PHP code.

Installing PHP

As with every coding example, we'll start with the Hello, World! program. Why? Because it's pretty much the most basic thing you can do when starting your journey with software development. This program simply prints out "Hello, World!" in the terminal.

Before we actually write our program, we need to install PHP first. Let's demonstrate on macOS. You'll need to first install Homebrew (which is a package manager for macOS). Open your terminal window and install Homebrew with the command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 

Once Homebrew is installed, install PHP with the command:

brew install php

If you're using a Debian-based Linux distribution (such as Ubuntu), PHP can be installed the single command:

sudo apt-get install php -y

For distributions based on Red Hat, that command would be:

sudo dnf install php

Hello, World!

At this point, PHP is installed and ready to say, Hello, World!

To create the new file, issue the command:

nano hello.php

As you can see, you’ll use the .php file extension, which indicates it's a PHP file. In that file, paste the following:

<?php
   echo 'Hello, World!';
?>

Save and close the file. 

Let's break down that file. The <?php is the open tag, which means what follows is to be considered PHP code. The ?> is the closing tag to end the code. We then use the echo command to print out what's within the single quotes, and we end each line of code with a semicolon. And that's the basic Hello, World! program. To run this, you'd issue the command:

php hello.php

Here's where we run into a problem (and you learn your first lesson). The output of the above code (in the macOS terminal) will look something like this:

Hello, World!%

Where did the % come from? That's our prompt indicator. How do we fix this (and print Hello, World! on a line of its own)? With PHP, we can include a new line character (which is a carriage return) that will leave the % prompt out from the output. To do that, we include the .PHP_EOL constant (which works for every platform) like so:

 <?php
   echo 'Hello, World!'.PHP_EOL;
?>

Now, when we run our program, we'll see:

Hello, World!

on its very own line.

Serve it from the web

Remember, PHP is great for serving up web programs. Let's take our Hello, World! program and serve it up from a web server. This will require you to have a machine running a web server (such as Apache or NGINX). Let's assume you're using Apache on Linux. Log into that server and create the new Hello, World! file with the command:

sudo nano /var/www/html/hello.php

In that file, paste the following:

<!DOCTYPE html>
<html>
<head>
<title>Hello, World! Page</title>
</head>
<body>
<?php
	echo 'Hello, World!'.PHP_EOL;
?>
</body>
</html>

As you can see, we've simply embedded our Hello, World! PHP code inside the HTML. Save and close the file. Point your browser to http://SERVER/hello.php (Where SERVER is the IP address or domain of the hosting server) and you should see Hello, World! printed in your web browser.

You've just created your first PHP web application. You're one step closer to custom software development, using PHP.

How to use variables

Let's create the same program, only using variables. Variables make it easy for you to store values and pass those values into your code. Let's create a new PHP program (called variables.php) that uses the following variables:

$greeting
$location
$timedate

Our code will look like this:

<?php
$greeting = "Hello,";
$location = "World!";
$timedate = date("Y/m/d").PHP_EOL;
echo "$greeting $location $timedate";
?>

We threw you for a loop. As you can see, we have standard variables for greeting and location, but for timedate, we've made use of the date command, which will print out the date in year/month/day format.

So we've set:

$greeting to "Hello,"
$location to "World!"

And finally, we've passed the output of the date command to be saved as our $timedate variable (with the added end of line constant). We then print everything out with the echo command and put everything in opening and closing tags. Save and close the file. 

When you run your new program (with the command php variables.php), the output will be something like:

Hello, World! 2021/03/02
You can even embed that program into a web page like so:
<!DOCTYPE html>
<html>
<head>
<title>Hello, World! Page</title>
</head>
<body>
<?php
$greeting = "Hello,";
$location = "World!";
$timedate = date("Y/m/d").PHP_EOL;
echo "$greeting $location $timedate";
?>
</body>
</html>

Point your web browser to http://SERVER/variables.php (where SERVER is the IP address or domain of the hosting server) and you'll see the same output in your browser, that you did when running the php variables.php command.

Conclusion

You've officially begun your first steps with PHP and even embedded your code within a website. As you can see, PHP is incredibly easy to learn. To find out more about this helpful language, read the official PHP documentation, so you can continue on with your custom software development journey.

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.