Table of Contents
Constant
Variables
The scope of variables
Variable variable
Home Backend Development PHP Tutorial A brief introduction to php constants and variables

A brief introduction to php constants and variables

Aug 29, 2018 pm 03:20 PM
php 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)
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['a'];
}
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 = 'b';
$b = 'abcde';
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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP Notice: Undefined variable:Solution PHP Notice: Undefined variable:Solution Jun 25, 2023 pm 04:18 PM

PHP Notice: Undefined variable:Solution

Solution to PHP Notice: Undefined variable: result Solution to PHP Notice: Undefined variable: result Jun 22, 2023 pm 01:32 PM

Solution to PHP Notice: Undefined variable: result

PHP Notice: Undefined variable: arr in solution PHP Notice: Undefined variable: arr in solution Jun 22, 2023 am 10:21 AM

PHP Notice: Undefined variable: arr in solution

How to pass PHP variables by reference How to pass PHP variables by reference Aug 26, 2023 am 09:01 AM

How to pass PHP variables by reference

PHP Notice: Undefined variable: sql solution PHP Notice: Undefined variable: sql solution Jun 23, 2023 am 08:51 AM

PHP Notice: Undefined variable: sql solution

How to use numeric variables in PHP How to use numeric variables in PHP Sep 13, 2023 pm 12:46 PM

How to use numeric variables in PHP

Variables and data types in PHP Variables and data types in PHP May 26, 2023 am 08:40 AM

Variables and data types in PHP

What are the names of php variables? What are the names of php variables? Jul 24, 2023 pm 02:08 PM

What are the names of php variables?

See all articles