Constants in PHP
A constant is a name or an identifier for a single value. The value of the constant cannot be changed during script execution. By default, a constant is case sensitive. By convention, constant identifiers are always uppercase. The constant name begins with a letter or underscore (_), followed by any number of letters, numbers or underscores. If you have defined a constant, it may never be changed or redefined.
To define a constant in PHP, you must use the define () function and to get the value of a constant, you must specify its name. Unlike variables, you don't need to add $ to constants. You can also use the constant () function to read a constant value if you want to dynamically get the name of the constant.
The constant () function in PHP
As the function name mentioned, this function will return the value of the constant.
This is very useful when you want to get the value of a constant, but you don't know its name, for example, it is stored in a variable or returned by a function.
For example, the constant () function in PHP
Only scalar data (Boolean, integer, float and string) can be kept in constants.
The difference between constants and variables in PHP is:
To define variables, write the character $ first, otherwise there is no need.
Constants cannot be defined by simple assignments, they can only be defined using the function define ().
Hangs can be defined and accessed anywhere without regard to variable scope rules.
A constant when defined, it cannot redefine or cancel the definition.
Valid and invalid constant names in PHP
// Vi du dang hang hop hop
define ("ONE", "Vi khong hang no 1");
define ("TWO2", "Vi du dang hang noi le 2");
define ("THREE_3", "travel to the cave without 3")
// Vi du dang hang hop hop
define ("2TWO", "Vi khong hang no 1");
define ("__ THREE__", "Vi khong hang no 2)";
Magic Constant in PHP
PHP provides a large number of predefined constants so that any script can use it.
There are 5 magic constants, depending on where they are used. For example, the value of __LINE__ depends on the line it is used in your script. These special constants are case-sensitive.
The following table lists some magic constants in PHP:
Name Description __LINE__ Current line of file __FILE__ Full path and full file name. If used within an include, the name of the included file will be returned. Since PHP 4.0.2, __FILE__ always has an absolute path, while in older versions they contain relative paths in some cases __FUNCTION__ Name of function. (Added in PHP 4.3.0) As in PHP 5, this constant returns the name of the function as it was previously declared (case sensitive). In PHP 4, its value is always lowercase __CLASS__ Name of the class. (Added in PHP 4.3.0) As in PHP 5, this constant returns the name of the class as it was previously declared (case sensitive). In PHP 4 its value is always lowercase __METHOD__ Class method name. (Added in PHP 5.0.0) This method name is returned as previously declared (case sensitive).Follow tutorialspoint
Last lesson: What is PHP?
Next lesson: Operator in PHP
You should read it
May be interested
- Summary - How to play Pokemon GO, virtual reality game catch Pokemon on smartphonepokemon go game (styled, raising and training virtual animals) has become popular, the online community with new movements also immediately responded to this mobile game in the direction positive direction
- Things you didn't know about 26 types of Pokeball - Part 2to be able to catch pokemon, players need to use pokeball. the higher the ball, the higher the success rate of pokemon
- Stardust's effective way in Pokemon GOpokemon go stardust is used to evolve pokemom with candy. therefore, if there is more stardust, it will make the evolution easier.
- AZ word about eggs in Pokemon Gothere are many different pokemon eggs that can hatch into many pokemon, but to make these eggs hatch into pokemon, you will have to perform some tasks. each player begins with an egg incubator, which allows the user to incubate an egg after performing certain steps, gps and pedometer tracking on the mobile device of you and the game.
- Things you didn't know about 26 types of Pokéball - Part 1pokéball is the most important item in pokémon go, allowing gamers to quickly catch pokémon back to their team. the higher the pokéball, the more effective the ability to catch pokémon.
- 10 tips to help you become a great Pokemon trainerin the article below, tipsmake.com will introduce to you some tips to easily become a great trainer in this pokemon go game offline ...
- How to use GoChat application in Pokémon GOpokémon go has become a very hot phenomenon since its debut. any information or tricks related to the game are read and applied by players during the process of catching pokémon. and the gochat chat app for pokémon go players brings space to capture pokémon much more interesting.
- How to play Pokemon GO in Landscape Mode on the iPhonealthough players can play pokemon go in portrait mode. however, if you want to watch and play games on a large and eye-catching screen, players can switch to playing games in landscape mode.
- The secret to controlling Pokemon Go employees at workthese days, hr managers are faced with an extremely painful problem that is the status of priority employees playing pokemon go more than work. this has caused a small impact on productivity and efficiency.
- Check out the 'buffalo' Pokémon in Pokémon Goeach type of pokemon has hp, cp, ability to attack and endure differently. based on these indicators, players can determine as well as choosing the most powerful pokemon for their offensive tactics.