Книга: Fedora™ Unleashed, 2008 edition

Constants

Constants

Constants are frequently used in functions that require specific values to be passed in. For example, a popular function is extract(), which takes all the values in an array and places them into variables in their own right. You can choose to change the name of the variables as they are extracted by using the second parameter— send it 0 and it overwrites variables with the same names as those being extracted, send it 1 and it skips variables with the same names, send it 5 and it prefixes variables only if they exist already, and so on. Of course, no one wants to have to remember a lot of numbers for each function, so you can instead use EXTR_OVERWRITE for 0, EXTR_SKIP for 1, EXTR_PREFIX_IF_EXISTS for 5, and so on, which is much easier.

You can create constants of your own by using the define() function. Unlike variables, constants do not start with a dollar sign, which makes the code to define a constant look like this:

<?php
 define("NUM_SQUIRRELS", 10);
 define("PLAYER_NAME", "Jim");
 define("NUM_SQUIRRELS_2", NUM_SQUIRRELS);
 echo NUM_SQUIRRELS_2;
?>

That script demonstrates how you can set constants to numbers, strings, or even the value of other constants, although that doesn't really get used much!

Оглавление книги


Генерация: 0.040. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз