php variable scope(php变量作用域)
问题描述
我对 php 变量范围感到困惑.例如:
i'm confused about the php variable scope. such as:
while(true){
$var = "yes , it is a test!";
}
printf($var)
$var
是在 while 语句范围内定义的,我们怎么才能让它超出它的范围呢?我在文档上找不到解释.
the $var
is defined in while statement scope , how could we get it outside it's scope ? and i can't find explanation on the document .
我想知道 php 如何处理它的范围.
i wonder how php deal with it's scope .
推荐答案
如果你做了 while(true)
你就不会离开 while,所以没关系.但是如果你有一个真实的表达,像这样(这是一个无用的例子,我知道)
If you do while(true)
you will not get out of the while, so it wouldn't matter. But if you would have a real expression, something like this (this is a useless example, i know)
$i=0
while($i<10){
$var = "yes , it is a test!";
$i++;
}
printf($var);
会起作用.没有特殊的while"变量范围,printf 将打印您的字符串.检查:http://php.net/manual/en/language.variables.scope.php
Will just work. There is no special "while" variable scope, the printf will print your string. check : http://php.net/manual/en/language.variables.scope.php
这篇关于php变量作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:php变量作用域
基础教程推荐
- PHP 守护进程/worker 环境 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- HTTP 与 FTP 上传 2021-01-01