A brief introduction to php constants and variables
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('NAME'));//结果为bool(true)
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不存在已经被销毁
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['a']; } fnc();//结果为1 2
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,没有随着函数的结束而销毁
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 = 'b'; $b = 'abcde'; echo $b; echo $$a; //相当于$b echo ${$a}; //相当于$b //结果都是 abcde
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP development, we often encounter the error message PHPNotice:Undefinedvariable. This error message means that we have used an undefined variable in the code. Although this error message will not cause the code to crash, it will affect the readability and maintainability of the code. Below, this article will introduce you to some methods to solve this error. 1. Use the error_reporting(E_ALL) function during the development process. In PHP development, we can

Solution to PHPNotice:Undefinedvariable:arrin In PHP programming, we often encounter the error message "Notice:Undefinedvariable". This error message is usually caused by accessing an undefined variable or the variable has not been initialized. For this problem, we need to find the problem and solve it in time. In this article, we will focus on PHPNotice:Undefin

How to use numeric variables in PHP In PHP, a numeric variable is a variable type that is used directly without declaration. You can use numeric variables to perform mathematical calculations, data comparisons, and other numerical operations. This article will explain how to use numeric variables in PHP and provide specific code examples. Defining numeric variables In PHP, defining numeric variables is very simple, just assign a number directly to the variable. Here is an example: $number=10; In the above code, we define a value called $numb

How to quickly eliminate PHP variable undefined errors? In PHP development, we often encounter undefined variable errors. This is because an unassigned variable is used in the code. When encountering this kind of error, we need to quickly find the cause of the error and solve it. Here are some ways to quickly troubleshoot PHP variable undefined errors to help you locate and fix errors faster. Turn on error reporting: When we turn on error reporting, PHP will display all error and warning messages, including variable undefined errors. We can do this by opening the code

In PHP, you can use the ampersand (&) symbol to pass variables by reference instead of by value. This allows the original variable to be modified within a function or method. There are mainly two ways to pass PHP variables by reference: Using ampersand symbol Using ampersand symbol in function/method declaration Using ampersand symbol in function/method declaration When passing variables to function/method In PHP, you can use function/ The ampersand symbol (&) in a method declaration passes variables by reference. Here is the updated explanation: To pass a reference variable by using the & symbol in a function/method declaration, you need to include the & symbol before the parameter name in the function/method definition. This indicates that parameters should be passed by reference, allowing

When developing a PHP application, if you encounter the "Undefined variable: sql" prompt, it usually means that you are referencing an undefined variable. This can be due to many reasons, such as misspellings of variable names, scoping issues, or syntax errors in the code, etc. In this article, we will explore the various causes of this problem and provide some ways to solve it. 1. Variable name is misspelled In your PHP code, if the variable name is incorrect or misspelled, the system

PHPNotice:Undefinedvariable:result means that an undefined variable result is called in the PHP program, which will cause the program to generate a Notice-level warning. This situation is generally caused by programmers not correctly defining variables or the scope of variables when writing PHP code. If not resolved in time, this Notice level warning may cause problems in the operation of the program. So, how to resolve PHPNotice:

In PHP programming, variables are the basic unit of storing values and are used to store and use data during program execution. In PHP, variables can be assigned different data types, including integers, floating point, strings, arrays, etc. In this article, we will introduce common variables and their usage in PHP programming. Simple variables are the most common variable type. They can store regular data types such as integers, floating point numbers, and strings. In PHP, the initial value of undefined variables is NULL. Here are a few examples: Integer variable: $
