Jump to content

Out Function in PHP - Introduction


Recommended Posts

In this PHP tutorial you are going to learn about the most important php output function (echo), that is to say about the function which will allow you to display on the screen the output of your php scripts.

OUTPUT FUNCTION IN PHP - USING THE ECHO FUNCTION

The echo output function allows you to return HTML code to the web browser of your visitor. In this way, the HTML code to be returned can be entered directly between quotation marks, as in the example below:

Learn the PHP code:

<html>

<body>
<?php
echo "Enter here the message to be displayed"; 
?>
</body>

</html>

Run the PHP script in your web browser:

Remark:

The previous example is printing plain text on the screen; however, we mentioned that the echo function is able to return HTML code to the visitor's browser. Indeed, it is possible to enter plain HTML code between quotation mark and the client's browser will interpret this code directly. For instance, we can try to specify that the text of your message is in bold characters:

Learn the PHP code:

<html>

<body>
<?php
echo "<b>Enter here the message to be displayed</b>"; 
?>
</body>

</html>

Run the PHP script in your web browser:

Remark:

The reasoning can be pushed further; for instance, you could very well include the whole HTML frame of your file (header and body) within the echo statement. Just like above, the whole code will be directly interpreted by the client's web browser:

Learn the PHP code:

<?php
echo "
<html>

<head> <title>Here is the title of your webpage</title> </head> <body>
<b>Enter here the message to be displayed</b> 

</body>

</html>";
?>

Run the PHP script in your web browser:

OUTPUT FUNCTION IN PHP - PRINTING THE OUTPUT OF PHP OBJECTS

Using the function echo, you can also print the output of other PHP variables (such as strings, arrays, integers, etc ...). In this case, the variable whose value is to be printed will not be put between quotation marks.

Learn the PHP code:

<?php
$your_age = 25;
echo $your_age; ?>

Run the PHP script in your web browser:

Remarks:

The echo function is able to return plain HTML code (when this code is put between quotation marks) but is also able to print the content of various PHP variables; in the example above, the output was the content of a numerical variable called $your_age.

In PHP, a variable must always start with a $ symbol.

The print function plays the same role as the echo function (with a few minor technical details which we won't present here; the interested reader will be able to get more information about this topic in the php reference manual).

Note: The echo and print php functions allow you to return HTML to your visitor's web browser and allows you to print the outpout of certain PHP variables.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...