php_mysql_apache [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

php_mysql_apache [Electronic resources] - نسخه متنی

Julie C. Meloni

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Variables


A variable is a special container that you can define, which will then "hold" a value, such as a number, string, object, array, or a Boolean. Variables are fundamental to programming. Without variables, we would be forced to hard-code all the values in our scripts. By adding two numbers together and printing the result, you can achieve something useful:



echo (2 + 4);

This snippet of code will only be useful for people who want to know the sum of 2 and 4, however. To get past this, you could write a script for finding the sum of another set of numbers, say 3 and 5. However, this approach to programming is clearly absurd, and this is where variables come into play.

Variables allow us to create templates for operations (adding two numbers, for example), without worrying about what values the variables contain. Values will be given to the variables when the script is run, possibly through user input, through a database query, or from the result of another action earlier in the script. In other words, variables are used whenever the data in your script is liable to changeeither during the lifetime of the script, or when it is passed to another script for later use.

A variable consists of a name of your choosing, preceded by a dollar sign ($). Variable names can include letters, numbers, and the underscore character (_), but they cannot include spaces. Names must begin with a letter or an underscore. The following list shows some legal variables:



$a;
$a_longish_variable_name;
$2453;
$sleepyZZZZ;

Your variable names should be meaningful as well as consistent in style. For example, if your script deals with name and password values, don't create a variable called $n for the name and $p for the passwordthose are not meaningful names. If you pick up that script weeks later, you might think that $n is the variable for "number" rather than "name" and that $p stands for "page" rather than "password."

A semicolon (;)also known as the

instruction terminator is used to end a PHP statement. The semicolons in the previous fragment of code are not part of the variable names, but are used to end the statement which defines the variables.

As you can see, you have plenty of choices when naming variables. To declare a variable, you need only include it in your script. When you declare a variable, you usually assign a value to it in the same statement, as shown here:



$num1 = 8;
$num2 = 23;

The preceding lines declare two variables, using the assignment operator (=) to give them values. You will learn about assignment in more detail in the "Operators and Expressions" section later in this chapter. After you assign values to your variables, you can treat them exactly as if they were the values themselves. In other words



echo $num1;

is equivalent to



echo 8;

as long as $num1 is assigned a value of 8.

Globals and Superglobals


In addition to the rules for naming variables, there are rules regarding the availability of variables. In general, the assigned value of a variable is present only within the function or script where it resides. For example, if you have scriptA.php which holds a variable called $name with a value of joe, and you want to create scriptB.php that also uses a $name variable, you can assign to it a value of jane without affecting scriptA.php. The value of the $name variable is

local to each script, and the assigned values are independent of each other.Chapter 6, "Working with Functions."

In addition to global variables of your own creation, PHP has several predefined variables called

superglobals . These variables are always present, and their values available to all of your scripts. Each of the following superglobals is actually an array of other variables:

  • $_GET contains any variables provided to a script through the GET method.

  • $_POST contains any variables provided to a script through the POST method.

  • $_COOKIE contains any variables provided to a script through a cookie.

  • $_files contains any variables provided to a script through file uploads.

  • $_SERVER contains information such as headers, file paths, and script locations.

  • $_ENV contains any variables provided to a script as part of the server environment.

  • $_REQUEST contains any variables provided to a script via any user input mechanism.

  • $_SESSION contains any variables that are currently registered in a session.


If you're using a version of PHP earlier than 4.1.x and cannot upgrade to a newer version, you must adjust the names of the variables when you're following the scripts in this book. The old names are $HTTP_GET_VARS (for $_GET), $HTTP_POST_VARS (for $_POST), $HTTP_COOKIE_VARS (for $_COOKIE), $HTTP_POST_files (for $_files), $HTTP_ENV_VARS (for $_ENV), and $HTTP_SESSION_VARS (for $_SESSION). These old names are simply arrays and not superglobals, so if you want the old arrays to be global, you must define them as such.

The examples in this book will use superglobals wherever possible. Using superglobals within your scripts is an important in creating secure applications, as it reduces the likelihood of injected input to your scripts. By coding your scripts to only accept what you want, in the manner defined by you, you can eliminate some of the problems created by loosely written scripts.


/ 323