Category: php

php Constants and VariablesLearn PHP.

Using Constants and Variables to Add Functionality

We’ve covered the basics of using the echo function to display text the way you want it. Really, this works
no differently from coding an HTMLpage. However, using constants and variables allows you to take
advantage of the power of PHP.

Overview of Constants

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 PHPfunction define(). Once you’ve defined a con-
stant, it can’t be changed or undefined.

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:

<title>My Movie Site</title>
define (“FAVMOVIE”, “The Life of Brian”);
echo “My favorite movie is “;


2. Save this file as moviesite.php and open it in your browser.

Passing Variables between Pages

Suppose your site allows viewers to enter their name on the front page. You’d like to be able to greet the
user by name on each page in your site, but to do so, you need some way to pass the value of the name variable from page to page.

There are basically four ways to accomplish this task: pass the variables in the URL, through a session, via a cookie, or with an HTMLform.

The method you choose is based on the situation and what best fits your needs at the time.

>A Word about register_globals

Before we begin discussing the four methods of parsing variables between pages, you need to under-
stand a little concept called register_globals.

This is a configuration setting in your php.ini file that, when turned off, prevents the variable value from being falsely inserted by an outside source.

While previous versions of PHPset the default setting in php.ini to “on,” ever since version 4.2, the
default has been switched to “off.”

This was the cause of many a programmer’s sleepless night, because
you must refer to your variables differently if register_globals is turned off, or else find all your
variables’ values coming up empty.

Although many third-party Web hosts have turned on register_globals, for security reasons not
everyone does; we decided to assume that register_globals is off for the purposes of the exercises
in this book.

Coding with the assumption that register_globals has been turned off is the safest
way to code because your program will work regardless of the server’s setting.

Instead of calling variable values by the standard $varname syntax, when register_globals is “off”
and you need to pass variables across pages, you need to refer to them in a different way, but only in the
receiving page.

You will see this in action in the next “Try It Out” section, but the various ways to refer
to variables depend on how they are being sent.

If you do not retrieve the variables using this syntax, the variable value will appear to be empty in your
program and can cause you much grief in debugging!

Read Full Article