Home  >  Article  >  Backend Development  >  Detailed analysis of PHP constants and variables

Detailed analysis of PHP constants and variables

小云云
小云云Original
2018-02-10 13:32:291261browse

This article mainly introduces relevant information about PHP constants and variable examples. Friends who need it can refer to it. I hope it can help everyone.

Detailed explanation of constants and variable examples of php

[PHP receives parameters under 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, 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 determined by the value of another variable. replace.

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


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

[Constant]

Use define to define, cannot be deleted or modified, write directly when calling name. 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; 
 
?>

Tips: First check whether the constant is 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;; 
 
?>

For constants with special symbols, you need to use the constant function to call them. Note that the constant name should be enclosed in 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;); 
 
?>

Get already All constants defined:


<?php 
   
  var_dump(get_defined_constants()); 
 
?>

[Magic variable]

__LINE__ Get the current line , __FILE__ gets the current path.

An application:

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

str_replace(4aa413e8cde63fcbc126f17ea466f577,c5d99ca4839f35b092c01bbffd8ca103,dc88c9b0c9bdc7931c419a538d9082ba,b8165d0b1d96dcc594bc553543c16a73 );


<?php 
   
  define(&#39;ROOT&#39;,str_replace(&#39;a.php&#39;,&#39;&#39;,__FILE__)); 
   
  echo ROOT; 
 
?>

【Base】

Add 0 before the number is octal, add 0x It's 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;

Detailed explanation of constant and variable definitions, usage, and difference examples in php

Summary of commonly used constants and variables in thinkphp

In PHP, how are the predefined constants and variables used and output?

The above is the detailed content of Detailed analysis of PHP constants and variables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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