This article mainly and you introduced the PHP constants and variables examples of the relevant information, the need for friends can refer to, hope to help everyone.
Examples of constants and variables in PHP
"PHP receives parameters under command line"
If you debug PHP at the command line, the incoming parameters are obtained through $ARGV, note that the file name contains the element, and the number of elements in the array is obtained by $ARGC.
"Mutable variables"
Refers to the variable name variable, the identifier of the variable can be replaced by the value of another variable.
For example, the second statement assigns a value to the ARGV1 variable.
<?php $varName = ' argv1 '; $ $varName = ' value1 '; Var_dump ($argv 1); ? >
constant
Use define definition, not delete and modify, write name directly when calling. Define also has a three-parameter version, and the third parameter indicates whether it is case insensitive and defaults to false.
<?php define (' pi ', 3.14); echo pi; ? >
Tip: First check that constants are defined and redefined, using the defined function:
<?php if (!defined (' pi ')) define (' pi ', 3.14); else Echo ' pi has been defined<br> '; ? >
For constants with special symbols, you need to use the constant function to call, note that this constant name should be quoted, for example:
<?php if (!defined (' = = ')) define (' = = ', ' puzzled '); else Echo ' pi has been defined<br> '; ECHO constant (' = = '); >
Gets all the constants that have been defined:
<?php Var_dump (get_defined_constants ()); ? >
"Magic Variable"
__line__ gets the current row, __file__ gets the current path.
An application:
Use the Str_replace function to replace file names in files with paths + filenames to ensure that file path changes are still accessible.
Str_replace (< value to find >,< replace with value >,< searched string >,< Replacement count variable (optional) >);
<?php define (' ROOT ', str_replace (' a.php ', ' ', __file__ '); echo ROOT; ? >
"In-System"
Before the number 0 is octal, plus 0x is hexadecimal.
"String Type"
Double and single quotes are possible, but double quotes resolve internal variables, but single quotes are efficient.
Double quotation-Mark parsing variable: {} can be guaranteed to separate variable names from other parts.
<?php $name = "Test"; echo "username is {$name}"; ? >
Related recommendations;
Definitions of constants and variables in PHP, usage, and examples of differences
Summary of constants and variables commonly used in thinkphp
In PHP, what are the predefined constants and variables, and how are they used and exported?