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 using the second parametersend it a 0 and it overwrites variables with the same names as those being extracted, send it a 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!

Was this article helpful?

0 0

Post a comment