Saturday, January 6, 2018

PHP Constant in class


We can also define value-containers called constants in PHP. The values of constants, as their name implies, can never be changed. Constants can be defined only once in a PHP program. Constants differ from variables in that their names do not start with the dollar($) sign, but other than that they can be named in the same way variables are. 
       However, it’s good practice to use all-uppercase names for constants. In addition, because constants don’t start with a dollar($) sign, you should avoid naming your constants using any of PHP’s reserved words, such as statements or function names. For example, don’t create a constant called ECHO or SETTYPE

Example of Constant in PHP
PHP Constant in class

         If we do name any constants this way, PHP will get very confused! Constants may only contain scalar values such as Boolean, integer, float, and string (not values such as arrays and objects), can be used from anywhere in your PHP program without regard to variable scope, and are case-sensitive.

     A cousin to the PHP variable is the defined constant . This is an entity that we can define anywhere in the code file, generally close to the beginning of the code or in a function. A defined constant holds its value until the script has completed. The scope of a defined constant is global, meaning it is file-wide and within any defined function or class that is also part of that code file, including any other included files or functions. 
            The rules for defining a constant are similar to those that govern variables, but not exactly the same. The major difference is theuse of the built-in PHP function define() . 

When defining a constant, you must adhere to the following rules:-

1. defined().
2. Letters and underscore .
3. Restriction .

Use this PHP function to define the constant. 

Letters and underscores 

A constant must start with either a letter or an underscore character, followed by letters, numbers, or underscores. Case-sensitive By default and convention, a defined constant is uppercase, although we can alter this within the define() function’s options


Only scalar data see the section Variables: Data Types, Loose Typing, and Scope can be stored in a constant.

To define a constant, use the define() function, and include inside the parentheses the name we've chosen for the constant, followed by the value for the constant.

Example :- 

define( "MY_CONSTANT", "19" ); // MY_CONSTANT always has the string value "19" 
echo MY_CONSTANT;     // Displays "19" (note this is a string, not an integer)

     Defined constants definitely have their place in the PHP world :- 

Their worth becomes apparent when we have values that should not change throughout a code script, like a path variable where PDFs are stored, or a tax rate. Be sure to use this feature as much as you need, but also be sure that we test well before sending anything into the production world so that you are getting the results you expect from your constants.

How it's work 

First, the script stores the radius of the circle to test in a $radius variable. Then it calculates the diameter – twice the radius and stores it in a $diameter variable. Next it works out the circle’s circumference, which is π (pi) times the diameter, and stores the result in a $circumference variable. It uses the built-in PHP constant, M_PI, which stores the value of π. 
     Then the script calculates the circle’s area, which is π times the radius squared, and stores it in an $area variable. To get the value of the radius squared, the script uses the built-in pow() function, which takes a base number, base, followed by an exponent, exp, and returns base to the power of exp. Finally, the script outputs the results of its calculations, using the string concatenation operator (.) to join strings together. Read More


Please comment and follow this site blog inbox