Home > Backend Development > PHP Tutorial > A brief introduction to php constants and variables

A brief introduction to php constants and variables

不言
Release: 2023-04-03 22:42:01
Original
1950 people have browsed it

This article brings you a brief introduction to PHP constants and variables. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Constant

1. A constant, as the name suggests, is a normal quantity
2. A constant is a quantity that remains unchanged during the execution of the script
3. The definition of a constant Using

//定义一个常量
define('NAME','wuhen');
//使用一个常量
echo NAME;//输出结果
wuhenecho "<br>";
//判断常量是否被定义
var_dump(defined(&#39;NAME&#39;));//结果为bool(true)
Copy after login

Variables

1. A variable is a container used to temporarily store values, such as numbers, text characters, or arrays, etc.

2. Definition of variables
In PHP, variables are represented by the dollar sign ($) plus a variable name. There is no need to explicitly declare variables in PHP.

3. Naming rules

  • The variable name must start with a letter or an underscore "_"

  • The variable name can only contain Letters, numbers, and underscores

  • Variable names cannot contain spaces

  • PHP is a weak type checking language, so variables do not need to be predefined before use , and there is no need to specify the data type

4. Variable assignment
Assignment by value: Use "=" to directly assign the value of the assignment expression to another variable
Assignment by reference : Assign the reference of the assignment expression memory space to another variable

5. Destruction of the variable
Use the unset() function

6. Determine whether the variable exists
Use isset( )Function

//定义一个变量
$a;
//传值赋值
$a = 5;
//引用赋值
$b = &$a;//把$a的地址传给$b
$b = 6;
echo $a;//结果为6
//引用赋值,改变$b等于是改变$a
//销毁变量
unset($a);
//判断变量是否存在
var_dump(isset($a));//结果为bool(false),说明$a不存在已经被销毁
Copy after login

The scope of variables

1. Local variables
 The scope of a variable declared inside a function is the function in which it is located. It is saved in the stack of memory, so it is very fast

2. Global variables

  • Contrary to local variables, global variables can be accessed anywhere in the program

  • Variables defined outside all functions have their scope within the entire PHP file

  • Global variables are used inside functions, and keywords are added before the variables global declaration or use $GLOBAL[”] to access

//全局变量
$a = 1;function fnc(){
    //引用全局变量
    global $a;    
    echo $a;    
    $a = $a+1;    
    //使用全局变量数组引用
    echo $GLOBALS[&#39;a&#39;];
}
fnc();//结果为1 2
Copy after login

3. Static variables

  • Static variables are a special kind of local variables, static Variables only exist within the function scope

  • They will still exist on the stack after the function ends and will not be destroyed

  • Add the key before the variable Word static, the variable becomes a static variable

//静态变量
function fnc(){
    //定义一个静态变量
    static $count = 1;    
    echo $count;    
    $count += 1;
}
fnc();//结果为1
fnc();//结果为2
//说明静态变量$count,没有随着函数的结束而销毁
Copy after login

Variable variable

  • Variable variable refers to using the value of a variable as the variable Name

  • Variable names can be dynamically named and defined using the

  • syntax with two dollar signs $, or wrapped with {} Get up

//可变变量
$a = &#39;b&#39;;
$b = &#39;abcde&#39;;
echo $b;   
echo $$a; //相当于$b
echo ${$a}; //相当于$b
//结果都是 abcde
Copy after login

Related recommendations:

PHP newbies learn variables and constants

PHP 7: Definition of PHP variables and constants

The relationship and difference between php constants and variables

The above is the detailed content of A brief introduction to php constants and variables. 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