The authors chooseOpen Sourcing Mental Illness LtdAs aWrite for DOnationsPart of the program accepts donations.

Introduction to the

“Hello, world! Programming is a classic and time-honored tradition in computer programming. As a beginner’s first complete program, as well as a good program for testing systems and programming environments, “Hello world!” Explains the basic syntax of a programming language.

This tutorial will guide you through writing a “Hello World!” The program. You’ll also learn how to turn PHP code blocks on and off in your code, and how to use different types of comments in your code.

The premise condition

You will need to install PHP and set up a local programming environment on your computer.

To set this up, follow how to install PHP 7.4 and set up a local development environment for your operating system.

Write “Hello, World!” The program

To write “Hello, World!” The program starts by opening a command-line text editor, such as Nano, and creating a new file.

nano hello.php
Copy the code

Enter the program once the text file is open in a terminal window.

hello.php


      
echo "Hello, World!";
? >
Copy the code

Let’s break down the different components of the code.

All PHP code belongs to a PHP code block that starts with
the end.

Echo is a language construct. Its arguments are a list of expressions followed by the echo keyword, separated by commas and not bounded by parentheses. Echo tells PHP to display or print semicolons at the end of echo and; Between any content.

In the echo and; Between is a series of characters -Hello, World! – Put it in quotes. Any character inside quotation marks is called a string.

After writing the program, press CTRL and X to exit nano. When prompted to save the file, press Y.

Once you exit nano, you’ll go back to your shell.

So, you have your “Hello, World!” The program.

Run “Hello, World!” Application program

Write “Hello, World!” After the program, you can run the program. Use the name of the PHP command and program file, as shown below.

php hello.php
Copy the code

Running the hello. PHP program you just created will cause your terminal to produce the following output.

OutputHello, World!
Copy the code

Let’s look at what this program does in more detail.

PHP calls the language structure echo to execute the line echo “Hello, World!” ; . Hello, World! The string value is passed to the structure.

In this example, the string Hello, World! Also known as a parameter because it is a value that is passed to another part of the code, such as a structure or a function.

Hello, World! The quotes are not printed to the screen because they are used to tell PHP that this code contains a string. Quotes delimit the beginning and end of a string.

Now that the program is running successfully, you can confirm that PHP is installed correctly and that the program syntax is correct. Before getting into the code itself, let’s take a closer look at the PHP code block.

Work outside of PHP code blocks

In a.php file, anything other than a PHP tag is treated as HTML or plain text. The PHP language was originally written as a way to extend the capabilities of HTML. With this in mind, you can include multiple blocks of PHP code throughout the file. Anything outside of a code block is rendered as HTML or plain text.

Update your hello.php file.

hello.php

Hi Sammy

       echo "Hello, World!"; ? >

How are you doing?

       echo "Swimmingly!";
Copy the code

Save the file and rerun it.

OutputHi Sammy
Hello, World!
How are you doing?
Swimmingly!
Copy the code

Dig into the code and you’ll find Hi Sammy and How are you doing? Are outside the PHP code block and therefore rendered as plain text when the program is run.

This file contains two blocks of PHP code. The first code block contains start and end tags, while the second code block, because it is at the end of the file, does not have a final end tag.

Includes end block tag? That’s not necessary. When terminating a file with a BLOCK of PHP code, it is recommended not to use the closing tag. Any characters, even blank Spaces, if rendered after the closing tag, will be output to the screen as HTML or plain text. This can have unintended consequences for the functionality of the application, because some features, such as redirection, will not be processed if anything is output to the browser. When writing a file that contains only PHP code, never include the end of the PHP tag.

When code becomes increasingly complex, such as when splitting concepts into blocks of code, it is good to leave comments for yourself and others. You can do this by using the _ comment _.

Add comments to PHP

A comment in code is a line that will not be executed as part of the program. Its sole purpose is to be read by the person who is looking at the code. One thing that strikes many developers is how much time it takes to read code compared to writing it. This means having code that is as easy to read as possible. You can do this in several ways.

  • ** Use coding standards. ** These are clear and consistent collections of guidelines and best practices for organizing and formatting code. In PHP, the most common coding standard is set by PHP-FIG (Framework Interop Group).
  • Choose readability over writability. Use descriptive variables rather than short ones. It’s not how many lines of code you write, it’s how long it takes someone to read those lines and understand what’s going on.
  • Be clear in your comments. While this is not a hard and fast rule, if you follow the first two points, your code should explain what is happening and comments should explain why things are happening the way they are.

When writing comments in PHP, there are two types of comments: single-line comments and multi-line comments. Single-line comments can start at any point in a line and end at the end of the line or the end of the code block, whichever comes first.

The most common method is to start a single line comment with a double forward slash (//), although PHP also recognizes the hash symbol (#) as a valid start for a single line comment.

hello.php

Hi Sammy

       echo "Hello"; //, World!" ;? >

How are you doing?

       echo "Swimmingly!";
// other options: Floating along
Copy the code

Save the file and run it again.

OutputHi Sammy
Hello
How are you doing?
Swimmingly!
Copy the code

The first comment begins in the middle of the line. “Hello” is followed by a closing and semicolon, and the rest of the line is commented out by _. Commenting out one or more lines of code is often used for debugging to test how the code reacts if certain elements are removed.

You use the second comment to give a secondary option to an answer. In your project, the next step might be to respond with one of several different options each time you execute the application. Comments are used to remind you of additional options that can be added.

Multi-line comments start with /* and end with */. The PHP interpreter ignores any text or code within these characters. To provide more options, let’s change the last line to a multi-line comment.

hello.php

Hi Sammy

       echo "Hello"; //, World!" ;? >

How are you doing?

       echo "Swimmingly!";
/* When responding with one of a number of answers, here are some other options:
* Floating along
* Fin-tastic
* Going with the flow
* Treading water
* Swamped
*/
Copy the code

Using multi-line comments provides more room to add detail or formatting to again make the code and the intent of the code easier to understand. The multi-line comment includes a newline character and adds * as a list separator. The */ combination marks the end of our comment block.

Use DocBlocks to document

There is a special type of multi-line comment called DocBlock. This is a unique way to document the functionality of a particular file, class, method, or other structural element. Although DocBlock starts and ends with other multi-line comments /* */, they are designed to provide specific details about how to use an element. These details not only give the developer an overview of the code, but can also be used by the code editor (or IDE) to provide advice and validation.

A DocBlock consists of several parts. First, a short summary introduces the element, and if more context is required, a longer description is required.

The final part that makes DocBlock unique is tags and comments. These provide a concise and uniform way to provide meta-information about related elements. For example, a tag can describe the type of information that a method or function accepts or returns. It can also provide details about a file’s author or copyright.

hello.php


      
/**
 * DocBlock example
 *
 * @author Sammy <sammy@digitalocean.com>
 */.Copy the code

While you should strive to write code that is clear and easy to understand, adding clarifying comments can add additional context to your understanding of the code and the choices behind it.

conclusion

In this tutorial, you’ve written “Hello World! The program. You learned how to turn PHP code blocks on and off in your code, and how to use different comments to clarify and add context as your code gets more complex. From here, you can continue your learning by following the tutorial “How to Use Strings in PHP”.