In Previous article, the Hello World script displays Hello World! on a Web page by using a simple echo statement. In this section, you write a script that also displays Hello World!, but uses a variable in the script., the following PHP section is used to display the output:
<?php
echo “<p>Hello World!</p>”;
?>
The following script is a complete script that contains a PHP section that uses a variable to display Hello World!:
<html>
<head><title>Hello World Script using Variable</title></head>
<body>
<?php
$salutation = “Hello World!”;
echo “<p>$salutation</p>”;
?>
</body>
</html>
If you point your browser at this script by typing the URL into the browser, the following output is displayed on the Web page:
Hello World!
A variable keeps its information for the whole script, not just for a single PHP section. If a variable is set to 5 at the beginning of a script, it will still hold 5 at the end of the script (unless, of course, you assign it another value). For example, the following script has two separate PHP sections:
<html>
<head><title>Hello World Script</title></head>
<body>
<?php
$salutation = “Hello World!”;
echo “<p>$salutation</p>”;
?>
<p>This is an HTML section</p>
<?php
echo “<p>$salutation again</p>”;
?>
</body>
</html>
If you point your browser at this script by typing the URL into your browser, the following output displays on the Web page:
Hello World!
This is an HTML section
Hello World! again
0 comments:
Post a Comment