Home > Article > Backend Development > Detailed graphic explanation of constants and variables in php
This article mainly introduces relevant information about the detailed explanation of constants and variable instances in php. Friends who need it can refer to
Detailed explanation of constants and variable instances in php
[PHP receives parameters on the command line]
If debugging php on the command line, the incoming parameters are obtained through $argv. Note that it contains the file name element. , the number of elements in the array is obtained through $argc.
【Variable variable】
means that the name of the variable is variable, and the identifier of the variable can be determined by the value of another variable. replace.
For example: the second statement assigns a value to the variable argv1.
<?php $varName = 'argv1'; $$varName = 'value1'; var_dump($argv1); ?>
【Constant】
Use define to define, cannot be deleted or modified, call When writing the name directly. define also has a three-parameter version. The third parameter represents whether it is case-insensitive. The default is false.
<?php define('pi',3.14); echo pi; ?>
Tips: Check whether the constant is defined before defining it, use 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 them. Note that the constant name should be enclosed in quotes, for example:
<?php if(!defined('= =')) define('= =','puzzled'); else echo 'pi has been defined<br>'; echo constant('= ='); ?>
Get all defined constants:
<?php var_dump(get_defined_constants()); ?>
[Magic Variable]
__LINE__ gets the current line, __FILE__ gets the current path.
An application:
Use the str_replace function to replace the file name in the file with the file name to ensure that the file path changes can still be accessed.
str_replace(4aa413e8cde63fcbc126f17ea466f577,c5d99ca4839f35b092c01bbffd8ca103,dc88c9b0c9bdc7931c419a538d9082ba,b8165d0b1d96dcc594bc553543c16a73 );
<?php define('ROOT',str_replace('a.php','',__FILE__)); echo ROOT; ?>
【Base】
Add 0 before the number is octal , plus 0x is hexadecimal.
[String type]
Both double quotes and single quotes can be used, but double quotes can parse internal variables, but single quotes are more efficient .
Double quotes parse variables: { } can ensure that the variable name is separated from other parts.
<?php $name = "test"; echo "username is {$name}"; ?>
Related recommendations:
PHP variablesDetailed explanation and string dynamic insertion of variables ( Case)
PHP variablesDetailed explanation of scope
Detailed explanation of PHP constants and data type instances
The above is the detailed content of Detailed graphic explanation of constants and variables in php. For more information, please follow other related articles on the PHP Chinese website!