Sending Data to the Web Browser: How to Send Data to the Web Browser Using PHP?

How to make a basic PHP script presents a basic idea of creating a general PHP script. Now, we would like to add some functionality to send data using that php script code. For creating dynamic website using PHP, we must have to know how to send data to the Web browser. A number of built-in function of PHP couuld resolve these type of purposes. The most common PHP function is echo and print to send data to the web browser for any dynamic site. For example:\
print 'Visit HideMyTips.com for latest tips!';
echo 'Visit HideMyTips.com for latest tips!';
In this page, we will discuss how to send data to the web browser using PHP.

How To send data to the Web browser using PHP?

Step 1: Open MyPage.php (which we created to the tutorial How to make a basic PHP script) using a text editor or an IDE.
Step 2: Between the PHP tags (lines 11 and line 12) of Step 3 of How to make a basic PHP script, add a simple message:
echo 'Hi! This is my first printing message using PHP!';
You can write any type of message. We just learning ans so we use a simple message. The message is printed using the echo function.
Moreover, we change the title of the page as "Displaying data..." by the following line:
<title>Displaying data...</title>
Step 3: Now, save the file as SecondPage.php and place the file in the Web directory like the previous tutorial, and test it in your Web browser. Now, the page looks like the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Displaying data...</title>
</head>
 
<body>
This is standard HTML page.
 
<?php
    echo 'Hi! This is my first printing message using PHP!';
?>
 
</body>
</html>


Tips and more

  • Both echo and print can both be used over multiple lines. For example the following code is correct:

echo 'This is an example of multiple line
sentence to see how echo can works for multiple lines.';

  • For printing each new line, use the newline character (\n) within double quotation marks, which is equivalent to Enter or Return.

No comments:

Post a Comment

Thanks for your valuable feedback. Your feedback will be published soon.