FIRST LINE OF CODE IN PHP

FIRST LINE OF CODE IN PHP

The very first line of PHP code is coming your way.

When the PHP environment is ready we can start writing our first script.

First of all, congratulations!

Hardest part is already behind you. Now you can expect a ton of fun and challenges that will reward you greatly if you persevere. As in any other topic, beginnings are tough. The more you enjoy learning by practice, the easier it gets along the way.

We start from creating a new file, which we will be named test.php. An important notice regarding modern code editors (such as VS Code). They work in the context of a folder, hence you don’t need to worry about complicated project creation process inside the tool.

Simply, create new empty folder on your harddrive and you are ready to go. Just select it as the project folder inside VS Code (or any other editor – check out recommendations on Resources page).

Inside it, create a new file test.php.

FIRST CODE IN PHP

First, let’s write several lines of classic HTML code that describes HTML document. For example:

<html>
<head>
<title>First script in PHP</title>
</head>
<body>
<p>
To be honest, it doesn't matter what we put here.
</p>
</body>
</html>
I assume you know the basics of HTML syntax so I won’t dig dipper in explaining the mechanics here. The result of running above code in the web browser is simply displaying the “To be honest, it doesn’t matter what we put here” text on the screen.

Now, let’s look at the example of how to achieve the same effect using PHP:

<html>
<head>
<title>First script in PHP</title>
</head>
<body>
<p>
<?php
  echo "To be honest, it doesn't matter what we put here.";
?>
</p>
</body>
</html>
Let’s see what has changed compared to the previous version.

At the first glance, the <?php and ?> tags have appeared. This is probably the most important thing in the PHP programming -> for the code to be processed by the interpreter, it must be closed between such markings.

Now see what appears between them.

It is an echo command.

The echo command simply displays a given text on the screen. In this case, it is the same text as in plain HTML. Result on the page will be the same.

Congrats! You’ve just written your first line of code in PHP. Now go and celebrate it with friends! Such an achievement.


MAIN TAKEAWAYS

PHP code generates HTML code and send this code to the browser to display.
Interpretation of PHP code only happens inside the <?php and ?>  tags.
You’ve already learned the first system command – echo. It simply displays the given text on the screen. You can check echo command in the PHP manual.
Now it’s time to learn what are variables and constants.

Comments

Popular posts from this blog

VARIABLES AND CONSTANTS

SIMPLE PHP