了解PHP中的变量范围 [英] Understanding variable scopes in php

查看:87
本文介绍了了解PHP中的变量范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

echo "Point1, a=".$a."\n";
echo "Point1, b=".$b."\n";
if(1<2)
    {
        $a = 6; 
        $b['link'] = "here";
        echo "Point2, a=".$a."\n";
        echo "Point2, b[link]=".$b['link']."\n";
    }
echo "Point3, a=".$a."\n";
echo "Point3, b[link]=".$b['link']."\n";

为什么上面的代码会打印出以下内容?

Why does the above code print out the following?

Point1, a=
Point1, b=
Point2, a=6
Point2, b[link]=here
Point3, a=6
Point3, b[link]=here

据我了解,$ a和$ b的范围应以花括号{}结尾!

In my understanding, the scope of $a and $b should end within the curly braces { }!

推荐答案

据我了解,$ a和$ b的范围应以花括号{}结尾!

In my understanding, the scope of $a and $b should end within the curly braces { }!

仅函数和方法具有其自己的局部范围.其他控制结构(回路,条件...)则没有.

Only functions and methods have their own, local scope. Other control structures (loops, conditions...) do not.

PHP手册中的可变范围

这篇关于了解PHP中的变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆