Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to apply PHP variable

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to apply PHP variables". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "how to apply PHP variables" together.

PHP variables

Variables are "containers" for storing information:

examples

Examples »

Similar to algebra

x=5

y=6

z=x+y

In algebra, we use letters (e.g. x) and assign them values (e.g. 5).

From the expression z=x+y above, we can calculate that z has a value of 11.

In PHP, these letters are called variables.

Variables are containers for storing data. PHP variables

Similar to algebra, PHP variables can be assigned values (x=5) or expressions (z=x+y).

Variables can be short names (such as x and y) or more descriptive names (such as age, carname, totalvolume).

PHP variable rules:

Variables start with the $sign, followed by the variable name

Variable names must begin with a letter or underscore character

Variable names can only contain alphanumeric characters and underscores (A-z, 0-9, and_)

Variable names cannot contain spaces

Variable names are case sensitive ($y and $Y are two different variables)

PHP statements and PHP variables are case-sensitive. Creating (declaring) PHP variables

PHP has no command to declare variables.

A variable is created the first time you assign it:

examples

Examples »

In the execution of the above statement, the variable txt will hold the value Hello world!, And variable x will hold the value 5.

Note: When you assign a text value to a variable, enclose the text value in quotation marks.

PHP is a weakly typed language.

In the example above, we note that you don't have to declare the variable's data type to PHP.

PHP automatically converts variables to the correct data type based on their value.

In strongly typed programming languages, we must declare (define) the type and name of variables before using them.

PHP variable scope

The scope of a variable is the part of the script where the variable can be referenced/used.

PHP has four different variable scopes:

local

global

static

parameter

Local and global scope

Variables defined outside all functions have global scope. Global variables can be accessed by any part of the script except functions. To access a global variable in a function, you need to use the global keyword.

Variables declared inside PHP functions are local variables and can only be accessed inside functions:

examples

Examples »

In the example above, myTest() defines the $x and $y variables. The $x variable is declared outside the function, so it is global, and the $y variable is declared inside the function, so it is local.

When we call myTest() and output the values of two variables, the function will output the value of the local variable $y, but cannot output the value of $x, because the $x variable is defined outside the function and cannot be used inside the function. If you want to access a global variable in a function, you need to use the global keyword.

Then we output the values of two variables outside myTest(), which outputs the value of the global variable $x, but cannot output the value of $y, because the $y variable is defined in the function and is a local variable.

You can use the same variable name in different functions, because variable names defined within these functions are local variables that only work within that function. PHP global Keyword

The global keyword is used to access global variables within a function.

To call a global variable defined outside a function within a function, we need to precede the variable in the function with the global keyword:

examples

Examples »

PHP stores all global variables in an array named $GLOBALS[index]. index holds the name of the variable. This array can be accessed inside a function or used directly to update global variables.

The above example can be written as follows:

examples

Examples »

Static scope

When a function completes, all its variables are usually deleted. However, sometimes you want a local variable not to be deleted.

To do this, use the static keyword the first time you declare a variable:

examples

Examples »

Then, each time the function is called, the variable retains the value it had the previous time the function was called.

Note: This variable is still local to the function.

parameter scope

Parameters are local variables that pass values to functions through calling code.

Parameters are declared in the parameter list as part of the function declaration:

examples

Thank you for reading, the above is "PHP variable how to apply" the content, after the study of this article, I believe we have a deeper understanding of how to apply PHP variables this problem, the specific use of the situation also needs to be verified. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report