The first line of PHP- Echo, and Print

Starting from this lesson we will see how to write the first lines of code in the PHP language. we will see, in practice, how to define instructions which will then have to be translated by the webserver (through the interpreter installed in it). we talk in this case of server-side scripting as the processing of the page takes place precisely at the server level, while the client receives a simple “preformatted” HTML output.

Because of its ability to create HTML output “on the fly”, PHP is defined as a dynamic language, exactly like ASP, JSP, etc.
This ability to produce “variable” output, in fact, makes PHP very different from simple HTML since the latter does not have the ability to dynamize its output (if not using other scripting languages).

The PHP Rendering Tags

But let’s get to the code! We open our notepad (or any other text editor or specific for PHP) and we write our first line of PHP code:

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

We save everything as “test.php” and test the result on your web server.

As you may have guessed, each portion of PHP code opens with <? Php and closes with ?> (So-called rendering tags ) which indicate to our PHP interpreter, respectively, the beginning of the code and its end. Anything between <? and ?> is PHP, everything that is out will be returned by the server as plain text or HTML.

This observation, which may seem trivial, is actually very important: in the same file, in fact, it is possible to alternate blocks of PHP code with portions of simple HTML, as in the example below:

<p>This is HTML.</p>
<?php
echo "<p>This Is PHP!</p>";
?>

Obviously, for this to work, our file must have a .php extension, otherwise, if we used the .html extension, the PHP code would not be “translated” by the web-server but simply shown on the screen!

NB: this topic – the combination of PHP and HTML – will be treated in more detail in a dedicated lesson.

In our example, the rendering tags were written above and below the PHP code, but nothing prevents you from writing everything online like this:

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

Such a syntax, albeit correct, is – according to the writer – to be used only in particular situations (for example when, as in our example, a single instruction must be used) being preferable to add a carriage return after opening and just before closing the rendering tags in order to facilitate the readability of the code.

Print on-screen: echo and print

Specifically of the example, we have seen above, we have written a single line of code: we have used the echo function to tell our server to “print on the screen” the phrase contained in the quotation marks:

echo "Hi, this is PHP!"

At the end of our instruction, we then used the semicolon (;) which is used to close the line (or more properly, to end a PHP instruction).

It is appropriate to underline that instead of echo we could have used print to perform the same function:

<?php
print "Hi,This is PHP PHP!";
?>

The two commands, in fact, except for marginal differences, serve the same purpose and can be used indifferently to print something on the screen.

Our example, of course, is purely scholastic: the use of PHP, in fact, is an end in itself and has no utility at the application level. In the next lessons, obviously, we will see how to combine the echo/print command in more complex and articulated situations where the output to be printed on the video is produced through the use of variables, conditions or other types of processing.

Leave a Comment