Home > Backend Development > PHP Tutorial > Detailed explanation of the definition, usage, and difference examples of constants and variables in PHP

Detailed explanation of the definition, usage, and difference examples of constants and variables in PHP

伊谢尔伦
Release: 2023-03-12 08:04:01
Original
1567 people have browsed it

phpConstants and variablesDetailed explanation of examples

[PHP receives parameters under the command line]

If debugging php on the command line, the parameters passed in are obtained through $argv. Note that it contains the file name element, and 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 value of another variable.

For example: the second statement assigns a value to the variable argv1.

<?php 
 
  $varName = &#39;argv1&#39;; 
  $$varName = &#39;value1&#39;; 
  var_dump($argv1); 
 
?>
Copy after login

[Constant]

Use define to define, cannot be deleted or modified, write the name directly when calling. define also has a three-parameter version. The third parameter represents whether it is case-insensitive. The default is false.

<?php 
 
  define(&#39;pi&#39;,3.14); 
  echo pi; 
 
?>
Copy after login

Tips: First check whether the constant has been defined before defining it, use the defined function:

<?php 
 
  if(!defined(&#39;pi&#39;)) 
    define(&#39;pi&#39;,3.14); 
  else 
    echo &#39;pi has been defined<br>&#39;; 
 
?>
Copy after login

For constants with special symbols, you need to use the constant function to call them. Note that the constant name should be Add quotation marks, for example:

<?php 
 
  if(!defined(&#39;= =&#39;)) 
    define(&#39;= =&#39;,&#39;puzzled&#39;); 
  else 
    echo &#39;pi has been defined<br>&#39;; 
   
  echo constant(&#39;= =&#39;); 
 
?>
Copy after login

Get all defined constants:

<?php 
   
  var_dump(get_defined_constants()); 
 
?>
Copy after login

[Magic variable]

LINE Get the current location OK, FILE gets the current path.

An application:

Use the str_replace function to replace the file name in the file with the path + file name to ensure that the file path changes can still be accessed.

str_replace(,,string>,);

<?php 
   
  define(&#39;ROOT&#39;,str_replace(&#39;a.php&#39;,&#39;&#39;,FILE)); 
   
  echo ROOT; 
 
?>
Copy after login

【Base】

Add 0 before the number to represent octal, and add 0x to represent 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 variable name is separated from other parts.

<?php 
 
  $name = "test"; 
  echo "username is {$name}"; 
 
?>
Copy after login

The above is the detailed content of Detailed explanation of the definition, usage, and difference examples of constants and variables in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template