PHP/MySQL : First steps with PHP
Jean-Guillaume Birot
Using Variables
The previous example does not bring more than an ordinary HTML code, except that we have written an instruction instead of
a simple phrase into the code. Let's further explore the PHP syntax by replacing our character string with a variable.
Correct your
hello.php file as follows:
<?php
$message = "Hello world!";
?>
<html>
<head>
<title>PHP test</title>
</head>
<body>
<H1 align="center">
<?php print($message); ?>
</H1>
</body>
</html>
You can see two PHP tags. The first one defines a so-called variable. A variable sets aside a portion of memory where you
can store and read data from within your program. In PHP, a variable begins with the
$ character. A variable can contain a character string delineated by quotes (
"Hello world!"), as in our example, or a numeric value: a whole number (integer) or a floating point number (double). The
= operator is used to assign a value to a variable.
The second PHP tag introduces a call to the
print function to which we pass the variable name. This is the basic principle of dynamic Web content: a program assigns, calculates,
or retrieves data that are then forwarded as a HTML code to the browser that displays it to the user.
We get the same results as with the preceding example:
PHP test
Hello world!
Pick up good habits. By placing the PHP code sections at the beginning of your files before the static HTML part, you can clearly separate the
treatment part that is maintained by the programmer from the displayed part that is allocated to the Web designer.