Home Backend Development PHP Tutorial PHP variable scope, global, static and other keywords

PHP variable scope, global, static and other keywords

Jul 06, 2018 am 09:56 AM
php variables

This article mainly introduces the scope, global, static and other keywords of PHP variables. It has certain reference value. Now I share it with you. Friends in need can refer to it

  1. Local and global scope

Variables defined in the function body in php are local variables, and variables defined outside the function are called global variables

 <?php
$x=5; // 全局变量function myTest()
{
    $y=10; // 局部变量
    echo "<p>测试函数内变量:<p>";
    echo "变量 x 为: $x";
    echo "<br>";
    echo "变量 y 为: $y";
} 

myTest();

echo "<p>测试函数外变量:<p>";
echo "变量 x 为: $x";
echo "<br>";
echo "变量 y 为: $y";?>
Copy after login

2 . global keyword

Global variables cannot be used in the function body in php. If you want to use them, you need to use the global keyword to declare them before use

<?php
$x=5;
$y=10;
 
function myTest()
{    global $x,$y;
    $y=$x+$y;
}
 
myTest();
echo $y; // 输出 15?>
Copy after login

3.static scope

When the function finishes running, the variables within the function will be eliminated. If you still need to use them and do not want them to be deleted, use the static keyword. Only used when declaring a variable for the first time.

<?php
function myTest()
{    static $x=0;
    echo $x;
    $x++;
}
 
myTest();
myTest();
myTest();?>
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Commonly used array functions in PHP

Introduction to static variables in PHP

The above is the detailed content of PHP variable scope, global, static and other keywords. 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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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

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

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

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

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

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:

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

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

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

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

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

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

PHP is an extremely popular server-side programming language. Its flexibility and ease of use make it one of the preferred languages ​​for building large-scale web applications. In PHP, variables are a very basic concept that can be used to store and manipulate data. In this article, we will take a deep dive into variables and data types in PHP. Variables In PHP, variables are used to store values ​​or the results of expressions. The naming rules for PHP variables are relatively flexible, but for the readability and maintainability of the code, the following rules should usually be followed: The variable name must

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

The names of PHP variables are: 1. Single letter variable name; 2. Variable name beginning with a number; 3. Variable name combining letters and numbers; 4. Variable name with underline; 5. Camel case variable name; 6 , Global variable name; 7. Predefined variable name; 8. Language function variable name; 9. More complex names can also be used in PHP, such as $my_variable, $this_is_a_long_variable_name, etc.

See all articles