4.4 Defining Your Own PHP Constants


You want to define your own constants with PHP.

Technique

Use PHP's define() function:

 <?php define("HI", "Hello World!"); print HI; ?> 

Comments

This code defines HI as Hello World by using the define() function. Please note that these are constants, not C-style macros ”only valid scalar data may be represented by a constant. For example, the following will not work:

 function four() {     return 4; } define("HI","four()"); print HI; 

This code would print "four()" instead of printing "4" (as print four(); would do).

It is also possible to check whether a constant has already been defined. The defined() function will help you do so. The two functions in combination enable you to make sure that a library file is included only once (similar to C header file constructs).

 <?php if (!defined("_MYLIB_INC_"))     define("_MYLIB_INC_", 1); else     return; // Library functions continue here ?> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net