A constant
is a placeholder for a value that you reference within your code. Constants are
typically named with capital letters (so you can easily find them within your
code), and the values are usually formally defined before using them. Constant
names must begin with a letter or an underscore and cannot begin with a number.
Names are also case-sensitive. You define a value assigned to a constant with
the PHP function define(). Once you’ve defined a constant, it
can’t be changed or undefined. Try It Out Using Constants
In this
exercise, you’ll see how you can use constants in your program.
1. Open your
text editor and type the following program:
<html>
<head>
<title>My Movie Site</title>
</head>
<body>
<?php
define (“FAVMOVIE”, “The Life of Brian”);
echo “My favorite movie is “;
echo FAVMOVIE;
?>
</body>
</html>
2. Save this
file as moviesite.php and open it in your browser. You should see the text shown
in
How It Works
By defining
the constant known as FAVMOVIE, you have set the value as “The Life of Brian,” which
can be recalled and displayed later on. Although this constant can’t be changed
or reset throughout your script, it is available for use by any part of your
script.
Written by "Shojib"
No comments:
Post a Comment