Home Backend Development PHP Tutorial Detailed explanation of how to use php variables_PHP tutorial

Detailed explanation of how to use php variables_PHP tutorial

Jul 13, 2016 am 10:44 AM
php under Instructions overall situation function dynamic variable exist local Detailed explanation

Variables in PHP are divided into five types: local variables, variable functions, global variables, dynamic variables, and dynamic variables. Now I will give you a detailed introduction to the use of these five PHP variables. Friends who need to know more Available for reference.

Static variables

The storage space of dynamic variables will be automatically released after the function is executed. For static variables, when the function is executed, the storage space still stores its variables. If you want to use the variable results of the function to perform calculations, you need to set the variable as a static variable.

The way to set PHP static variables is to add the "static" symbol before the variable.

Example:

The code is as follows Copy code
 代码如下 复制代码

<?php
function fun(){
static $i=0;
echo "$i <br>";
  $i++;
 }
 fun();
 fun();
?>

<?php
function fun(){
static $i=0;
echo "$i <br>";
$i++;
}
fun();
fun();
?>

Dynamic variables

When we define a variable, it is equivalent to applying for a storage space on the server. Dynamic variables are quantities whose values ​​are constantly changing. The characteristic is that when the function is completed, the value of its variable storage space will be automatically released. Just like a truck pulling goods, unloading it after it reaches the destination, and then returning to pull it.

Example:
 代码如下 复制代码

<?php
function fun(){
$i=0;
echo "$i <br>";
  $i++;
 }
 fun();
 fun();
?>

The code is as follows Copy code

<?php
function fun(){
$i=0;
echo "$i <br>";
$i++;
}
fun();
fun();
?>

Global variables

We learned earlier about local variables, whose values ​​are only valid within a certain range. If you want to implement cross-domain calls, you need to use global variables.

Definition of global variables in PHP: To declare a global variable, just add "global" before the variable name. After defining the global variable, the variables of the main program can be called inside the function. Similarly, the variables within the function can also be called. Used by the main program.

 代码如下 复制代码

<?php
$a=1;
function fun(){
echo $a ."<br>"; /* $a为局部变量,不显示内容 */
  global $a;  /* 定义$a为全局变量 */
  echo $a ."<br>";
  global $b;
  $b=10;
 }
 fun();
 echo $b;
?>

Example 1:

The code is as follows Copy code
<?php
$a=1;
function fun(){
echo $a ."<br>"; /* $a is a local variable and does not display the content */
global $a; /* Define $a as a global variable */
echo $a ."<br>";
global $b;
$b=10;
}
fun();
echo $b;
?>

Local variables

In the PHP language, variables have a certain scope. Once they leave, the variables will lose their meaning or change, which is called variable scope. Just like a local area network, it is only valid in the established office or dormitory. Once it exceeds this range, it will not be able to connect to the network. Variables can be divided into local variables and global variables according to their scope. Here we will talk about local variables first.

PHP local variable value Variables defined within the function are only used within the function and are invalid outside the function; similarly, variables defined outside the function are not valid within the function if they are not passed.
 代码如下 复制代码
<?php
function fun(){
$a1=5;
echo $a2;
}
fun();
$a2=10;
echo $a1;
?>
Example:

The above code outputs nothing after running.

Variable function

Variables can not only be defined by some English letters and other characters, but also can be defined for functions. If you add the "()" symbol after a variable name, PHP will look for a function with the same value as the variable to execute, which is called a variable function.

Example:

The code is as follows
 代码如下 复制代码

<?php
function p($name){
echo "my name is ".$name;
}
$someone="p"; /* 将字符串"p"赋给$someone */
$someone('Tom'); /* 这里等价于$p('Tom'),是一个变量函数 */
?>

Copy code
<?php
function p($name){
echo "my name is ".$name;
}
$someone="p"; /* Assign the string "p" to $someone */
$someone('Tom'); /* This is equivalent to $p('Tom'), which is a variable function */
?>

http://www.bkjia.com/PHPjc/633072.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/633072.html
TechArticle
Variables in php are divided into five types: local variables, variable functions, global variables, dynamic variables, and dynamic variables. Now let me give you a detailed introduction to the use of these five php variables...
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 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles