What are variables or parameters in PHP and how to use them?
Variables or Parameters are temporary containers for data in memory. We have predefined variables and user variables. A predefined variable is a type containing information about the Web server application such as Apache, the Web server operating system such as Linux, Mac, or Windows (Environmental variables), or the PHP module uses. The following is an example of using a PHP’s predefined variable.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitionl.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>The PHP example page, using predefined variable!</title>
</head>
<body>
/* “pre” makes the generated PHP information more legible. $GLOBALS variable
contains all the variables accessible to the script (predefined and user).
*/
<pre>
<?php
print_r ($GLOBALS);
?>
</body>
</html>
Save the file in your server running PHP and run the script to see the output.
The following is an example of using a PHP’s user defined variable. When you defined a user defined variable, you must following the following naming convention:
· Must be preceded by $,
· Following with a letter (A-Z, a-z) or an underscore (_),
· Don’t use space or be the same as predefined variables, and
· They are case-sensitive.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitionl.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>The PHP example page, using user defined variable!</title>
</head>
<body>
<pre>
<?php
// my variables - My name and age.
$my_name = ‘John’;
$my_age = 84;
print_r ($GLOBALS);
?>
</body>
</html>
|