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 use while loop and do...while loop in PHP

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to use the while loop and do...while loop in PHP. I hope you will get something after reading this article. Let's discuss it together.

PHP variable

A variable is a container for storing information.

The names of variables can be short (such as x and y) or more descriptive (such as carname).

Just like algebra, PHP variables can be used to preserve values (xvalues 5) and expressions (z=x+y).

Note: similar to algebra, the value of z is calculated to be 11.

Tip: in PHP, these three letters are called variables. Variables can be thought of as containers for storing data.

PHP variable rules:

The variable begins with the $symbol, followed by the name of the variable

Variable names must start with a letter or an underscore, not a number

Variable names can only contain alphanumeric characters and underscores (Amurz, 0-9, and _)

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

Create a PHP variable

PHP does not have a command to create variables. The variable is created the first time it is assigned:

Note: in this case, the variable txt holds the value Hello worldview, the variable x holds the value 5, and the variable y holds the value 10.5.

Tip: if the value you assign to a variable is text, surround the value in quotation marks.

PHP is a loosely typed language

In the above example, note that we do not have to tell the data type of the PHP variable.

PHP automatically converts the variable to the correct data type based on its value.

In languages such as C and C++ and Java, programmers must declare the name and type of a variable before using it.

PHP variable scope

In PHP, variables can be declared anywhere in the script.

The scope of a variable refers to the part of the script in which the variable can be referenced / used.

PHP has three different variable scopes: local (local), global (global), and static (static).

Local and Global scope

Variables declared outside the function have Global scope and can only be accessed outside the function.

Variables declared inside the function have LOCAL scope and can only be accessed within the function.

Note: in the above example, there are two variables $x and $y, and a function myTest ().

$x is a global variable because it is declared outside the function, while $y is a local variable because it is declared within the function.

Output the values of two variables inside the myTest () function, and $y outputs the value declared locally, but not the value of $x.

If you output the values of two variables outside the myTest () function, you will output the value of $x, but not the value of $y.

You can create local variables with the same name in different functions, because the local variable can only be recognized by the function in which it was created.

PHP global keyword

The global keyword is used to access global variables within a function. Use the global keyword before the variable (inside the function):

PHP also stores all global variables in an array called $globals [index]. The subscript contains the variable name.

This array is also accessible within the function and can be used to update global variables directly. The above example can be rewritten as follows:

PHP static keyword

Typically, when the function completes / executes, all variables are deleted.

However, sometimes you need not to delete a local variable, so use the static keyword when you declare a variable for the first time.

Then, every time a function is called, the information stored in this variable is the information contained in the last time the function was called:

Note: this variable is still a local variable of the function after using the static keyword. The running result of this example is "012".

After reading this article, I believe you have a certain understanding of "how to use while loop and do...while loop in PHP". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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