[PHP函数]③--变量作用域

';
$filename='3.html';
echo getExt($filename);//html

?>

函数作用域

局部变量--动态变量



[PHP函数]③--变量作用域_第1张图片
Paste_Image.png
[PHP函数]③--变量作用域_第2张图片
Paste_Image.png
[PHP函数]③--变量作用域_第3张图片
Paste_Image.png
[PHP函数]③--变量作用域_第4张图片
Paste_Image.png

局部变量-动态变量

function test()
{
    $i=1;
    echo $i."
"; ++$i; } test();//1 test();//1

局部变量-动态变量

function test()
{
    static $i=1;
    echo $i."
"; $i++; } test();//1 test();//2 var_dump($i);//报错
[PHP函数]③--变量作用域_第5张图片
Paste_Image.png

global关键字

$i = 1;
$j = 2;
function test2()
{
    //global $i;
    //global $j;
    global $i, $j;
    var_dump($i, $j);//int 1 int 2
    $i = 3;
    $i = 5;
}

test2();
var_dump($i, $j);//int 5 int 2

global不能赋值

function test()
{
    //global $m=3;//报错
    global $m, $n;
    $m = 3;
    $n = 5;
}

test();
var_dump($m, $n);//int 3 int 5

$GLOBAL

$username = 'king';
$age = 12;
$i = 11;

//print_r($GLOBALS);
//.....
//(...
//[username] => king
//[age] => 12
//    [i] => 11

function test()
{
    echo '用户名为' . $GLOBALS['username'] . "
";//用户名为king $GLOBALS['age'] = 22; } test(); var_dump($age);//int 22
Array
(
    [GLOBALS] => Array
 *RECURSION*
    [_POST] => Array
        (
        )

    [_GET] => Array
        (
        )

    [_COOKIE] => Array
        (
        )

    [_FILES] => Array
        (
        )

    [_ENV] => Array
        (
        )

    [_REQUEST] => Array
        (
        )

    [_SERVER] => Array
        (
            [HTTP_HOST] => 127.0.0.1
            [HTTP_CONNECTION] => keep-alive
            [HTTP_CACHE_CONTROL] => max-age=0
            [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
            [HTTP_UPGRADE_INSECURE_REQUESTS] => 1
            [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
            [HTTP_ACCEPT_ENCODING] => gzip, deflate, sdch
            [HTTP_ACCEPT_LANGUAGE] => zh-CN,zh;q=0.8
            [PATH] => C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;D:\Program Files\Git\cmd;C:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\nodejs\;D:\Program Files\mysql-5.7.18-winx64\bin;C:\WINDOWS\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps
            [SystemRoot] => C:\WINDOWS
            [COMSPEC] => C:\WINDOWS\system32\cmd.exe
            [PATHEXT] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
            [WINDIR] => C:\WINDOWS
            [SERVER_SIGNATURE] => 
            [SERVER_SOFTWARE] => Apache/2.2.21 (Win32) PHP/5.3.10
            [SERVER_NAME] => 127.0.0.1
            [SERVER_ADDR] => 127.0.0.1
            [SERVER_PORT] => 80
            [REMOTE_ADDR] => 127.0.0.1
            [DOCUMENT_ROOT] => C:/wamp/www/
            [SERVER_ADMIN] => admin@localhost
            [SCRIPT_FILENAME] => C:/wamp/www/testClass/test1.php
            [REMOTE_PORT] => 3929
            [GATEWAY_INTERFACE] => CGI/1.1
            [SERVER_PROTOCOL] => HTTP/1.1
            [REQUEST_METHOD] => GET
            [QUERY_STRING] => 
            [REQUEST_URI] => /testClass/test1.php
            [SCRIPT_NAME] => /testClass/test1.php
            [PHP_SELF] => /testClass/test1.php
            [REQUEST_TIME] => 1502890255
        )

    [username] => king
    [age] => 12
    [i] => 11
)

你可能感兴趣的:([PHP函数]③--变量作用域)