Constants

Constants are values in your application that will never change. For example, instead of including the literal string “My Cool Game“,you create a constant and reference the constant throughout your code. This makes your code much easier to update. Instead of changing hundreds of lines of code where “My Cool Game“ exists, you simply change the constant.

To define a constant in PHP you use the define() function.

define(“GAME_TITLE“, “My Cool Game“);

The define() function takes two parameters. The first is the name of the constant, and the second is the value of the constant. To reference this constant later in your code you would simply use what you entered as the first parameter.

echo(GAME_TITLE);

You can also tell if a constant is defined by using the defined() function. This will return 1 if the constant is defined and 0 if the constant is not defined.

if(defined(GAME_TITLE))
{
    echo(GAME_TITLE);
}

PHP also includes several built-in constants that are available to you. PHP has defined TRUE as 1 and FALSE as 0. You can retrieve the version of PHP you are using by referencing the PHP_VERSION constant. PHP_OS will tell you what OS PHP is running on. You can retrieve the current file being parsed and the current line number by referencing the _FILE_ and _LINE_ constants.

Additionally, PHP gives you access to some constants for error reporting, such as E_ERROR, E_PARSE, and E_WARNING. You can also access several variables that are predefined. You can find out what these variables are by using the phpinfo() function. You used this function while testing your PHP installation in Chapter 2.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset