PHP variable scope, global, static and other keywords
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
-
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";?>
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?>
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();?>
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!

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

AI Hentai Generator
Generate AI Hentai for free.

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

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

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

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

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.
