PHP Basics Syntax

Embedding PHP code

When creating the first PHP program, some basic principles for creating PHP scripts were already touched upon. Now, let’s look at them in more detail.

A PHP program or script is usually found in a .php file. Although developers can also embed PHP code in .html/.htm files as well.

A PHP document can contain both HTML markup and PHP code. The tags are used to move from HTML markup to PHP code, with PHP code in between. These tags indicate to the PHP interpreter that their content should be interpreted as PHP code, not HTML markup.

For example, let’s define the folder where the website files are stored (based on previous topics, this should be the folder c:\localhost\php_tutorials) and the file hello.php. Define the following code in this file:

<?php
echo "Hello world!";
?>

The actual PHP code consists of a set of instructions. Here is one instruction echo “Hello world!”;. This instruction represents the echo built-in command, which prints some value to the web page. The output value is specified after the echo command – in this case, it is the string “Hello world!”. Every single statement in PHP ends with a semicolon.

Since in this case, the file “hello.php” is located in the root folder of the web server, to access this script in the address bar of the browser, enter the address http://localhost/php_tutorials/hello.php . As a result, when accessing this script, we will see the following page in a web browser:

Embedding PHP on a Web Page

You can also use the short version of the tags to style your PHP code: <? and ?>. To do this, you need to change the line in the php.ini file:

short_open_tag = Off
 
short_open_tag = On

When the user accesses the script in the browser’s address bar, typing http://localhost/php_tutorials/hello.php for example, the web server passes it to the PHP interpreter. Then the interpreter processes the code and generates HTML markup based on it. And then the generated HTML code is sent to the user.

In the case of the above-defined script, the hello.php generated markup will look like this:

Hello World!

But of course, we can also add some HTML code. For example, let’s change the hello.php script as follows:

 <!DOCTYPE html>
 <html lang="en">
 <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>easywptutorials.com PHP Tutorials</title>
 </head>
 <body>
        <h1>PHP website</h1>
        <?php
        echo "Hello world!";
        ?>
 </body>
 </html>
 

As mentioned above, the interpreter will use the tags to understand that all text between these tags should be treated as PHP code. And all code outside these tags is treated as HTML code.

In this case, the interpreter will generate the following HTML code:

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>easywptutorials.com PHP Tutorials</title>
</head>
<body>
    <h1>PHP website</h1>
    Hello World!
</body>
</html>

With this, of course, we can use more instructions, as well as embed PHP code in various parts of the web page. For example:

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>easywptutorials.com PHP Tutorials</title>
</head>
<body>
    <h1>
    <?php
    echo "First PHP site";
    ?>
    </h1>
    <div>
    <?php
    echo "<h2>Paragraph title</h2>";
    echo "Paragraph text";
    ?>
    </div>
    </body>
</html>

In this case, the PHP code is embedded in two places. In the first case – inside the element

This will give us the following result:

Moreover, when using the function, echo can include HTML code in the output text, as is the case with the expression:

echo "<h2>Paragraph title</h2>";

Although the PHP code was defined in a .php file, we can also define the code in .html files and they will also be processed by the PHP interpreter.

The shortened version of PHP tags

If we need to display one value on a web page, then we can use a special form of PHP tags – – after the = (“equal”) sign, the output expression is placed. For example:

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>easywptutorials.com PHP Tutorials</title>
</head>
<body>
    <h1>
    <?= "First PHP site"; ?>
    </h1>
    <div>
    2 + 2 = <?= 2+2 ?>
    </div>
    </body>
</html>

In the first case, the string is displayed.

In the second case, the result of the expression is displayed as 2 + 2:

The result of the script:

Comments

When creating a website, we may use comments. For example, we can comment on action so that we can later have an idea of ​​what this code is doing:

<?php
echo "<p>Hello world!</p>"; // message output
// echo "Bye bye";
?>

The sign //precedes a single-line comment, and everything after this sign on one line will be considered a comment and will not be executed by the interpreter. When processing, the interpreter will simply skip the comments.

If we need to comment out multiple lines, then we can use a multi-line comment /* comment text*/:

<?php
echo "<p>Hello world!</p>"; // message output
/*
multiline comment
displaying the result of an arithmetic expression
echo "2 + 2 = " . (2+2);
*/
/**
*API comments
*/

?>

All lines inside the comment will also not be processed by the interpreter.