5

How to Write Your First PHP Code

 3 years ago
source link: https://hackernoon.com/how-to-write-your-first-php-code-9f1f33e3
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Write Your First PHP Code

3
heart.pngheart.pngheart.pngheart.png
light.pnglight.pnglight.pnglight.png
boat.pngboat.pngboat.pngboat.png
money.pngmoney.pngmoney.pngmoney.png

@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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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). 

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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

0 reactions
heart.png
light.png
money.png
thumbs-down.png
  • 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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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:

0 reactions
heart.png
light.png
money.png
thumbs-down.png
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 

Once Homebrew is installed, install PHP with the command:

0 reactions
heart.png
light.png
money.png
thumbs-down.png
brew install php

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

0 reactions
heart.png
light.png
money.png
thumbs-down.png
sudo apt-get install php -y

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

0 reactions
heart.png
light.png
money.png
thumbs-down.png
sudo dnf install php

Hello, World!

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

0 reactions
heart.png
light.png
money.png
thumbs-down.png

To create the new file, issue the command:

0 reactions
heart.png
light.png
money.png
thumbs-down.png
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:

0 reactions
heart.png
light.png
money.png
thumbs-down.png
<?php
   echo 'Hello, World!';
?>

Save and close the file. 

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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:

0 reactions
heart.png
light.png
money.png
thumbs-down.png
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:

0 reactions
heart.png
light.png
money.png
thumbs-down.png
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:

0 reactions
heart.png
light.png
money.png
thumbs-down.png
 <?php
   echo 'Hello, World!'.PHP_EOL;
?>

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

0 reactions
heart.png
light.png
money.png
thumbs-down.png
Hello, World!

on its very own line.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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:

0 reactions
heart.png
light.png
money.png
thumbs-down.png
sudo nano /var/www/html/hello.php

In that file, paste the following:

0 reactions
heart.png
light.png
money.png
thumbs-down.png
<!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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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:

0 reactions
heart.png
light.png
money.png
thumbs-down.png
$greeting
$location
$timedate

Our code will look like this:

0 reactions
heart.png
light.png
money.png
thumbs-down.png
<?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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

So we've set:

0 reactions
heart.png
light.png
money.png
thumbs-down.png
$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. 

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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

0 reactions
heart.png
light.png
money.png
thumbs-down.png
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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

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.

0 reactions
heart.png
light.png
money.png
thumbs-down.png
3
heart.pngheart.pngheart.pngheart.png
light.pnglight.pnglight.pnglight.png
boat.pngboat.pngboat.pngboat.png
money.pngmoney.pngmoney.pngmoney.png
Share this story
Read my stories

Powerful insights in business, technology, and software development.

Join Hacker Noon

Create your free account to unlock your custom reading experience.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK