How to write your first PHP code
Powerful insights into business, technology, and software development.
At some point, your business will need to use many programming languages to get the job done. And since your business is probably using a lot of software for your various channels and pipelines, having software engineers with the right skills is all about success.
This is why training is essential to create a team of developers capable of handling any tasks that you give them. With a properly trained team, when you need a project that relies heavily on PHP, you can be sure that it will be ready for the job.
Fortunately, PHP is one of the friendliest and easiest languages to learn. To show you how easy PHP is to learn, let’s take a look at getting started with the language.
What is PHP?
First of all, it’s important to understand what PHP is because 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 code is executed on the server side, not the client side).
One of the biggest differences between PHP and other languages is that it doesn’t require a compiler. Instead, you “do what you write”. In other words, you write a PHP script and then, using the PHP program itself, you run the script. This means that you don’t need to use any external applications to compile the code into a binary executable.
- Dynamic web page content generation.
- Create, open, read, write, delete and close files on a server.
- Collection of form data.
- Sending and receiving cookies.
- Add, delete and modify data in a database.
- User access control.
- Encrypt data.
With that said, let’s learn how to write your first PHP code.
As with every coding example, we’ll start with Hello, World! program. Why? Because it’s pretty much the most basic thing you can do when starting your software development journey. This program simply prints “Hello, World!” in the terminal.
Before writing our program, we must first install PHP. Let’s demonstrate on macOS. You will first need to 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:
If you are using a Debian-based Linux distribution (such as Ubuntu), PHP can be installed with the single command:
sudo apt-get install php -y
For Red Hat-based distributions, this command would be:
At this point, PHP is installed and ready to say, Hello, World!
To create the new file, run the command:
As you can see, you will be using the .php file extension, which indicates that this is a PHP file. In that file, paste the following:
echo 'Hello, World!'; ?>
Save and close the file.
Let’s break this file down. The Php est la balise ouverte, ce qui signifie que ce qui suit doit être considéré comme du code PHP. Le?> Is the closing tag to end the code. We then use the echo command to display what’s inside the single quotes, and we end each line of code with a semicolon. And this is the Hello, World base! program. To perform this, you issue the command:
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 like this:
Where does the% come from? This is our quick indicator. How do I solve this problem (and print Hello, World! On a separate line)? With PHP we can include a new line character (which is a carriage return) that will leave the% prompt out of the output. To do this, we include the constant .PHP_EOL (which works for each platform) like this:
'Hello, World!'.PHP_EOL; ?>
Now when we run our program we will see:
on its own line.
Serve it from the web
Remember, PHP is great for serving web programs. Take our Hello, World! program and serve it from a web server. This will require you to have a machine running a web server (such as Apache or NGINX). Suppose you are using Apache on Linux. Connect to this server and create the new Hello, World! file with the command:
sudo nano /var/www/html/hello.php
In that file, paste the following:
<html> <head> <title>Hello, World! Pagetitle> head> <body> echo 'Hello, World!'.PHP_EOL; ?> body> html>
As you can see, we simply integrated our Hello, World! PHP code inside 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 have just created your first PHP web application. You are one step closer to developing custom software, using PHP.
How to use variables
Let’s create the same program, using only variables. Variables allow you to easily store values and pass those values in 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:
"Hello,"; $location = "World!"; $timedate = date("Y/m/d").PHP_EOL; echo "$greeting $location $timedate"; ?>
We threw you a loop. As you can see we have standard variables for home and location, but for the date we used the date command, which will print the date in year / month / day format.
We have therefore defined:
$greeting to "Hello," $location to "World!"
And finally, we’ve passed the output of the date command to save as our $ timedate variable (with the end-of-line constant added). We then print everything with the echo command and put everything in the opening and closing tags. Save and close the file.
When you run your new program (with the php variables.php command) the output will be something like:
Hello, World! 2021/03/02 You can even embed that program into a web page like so: <html> <head> <title>Hello, World! Pagetitle> head> <body> $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 will see the same output in your browser as when running php variables. php order.
You’ve officially started your first steps with PHP and even integrated your code into a website. As you can see, PHP is incredibly easy to learn. To learn more about this useful language, read the official PHP documentation so you can continue your custom software development journey.
Create your free account to unlock your personalized reading experience.