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

<?php

function getExt($filename){
    $ext=strtolower(pathinfo($filename,PATHINFO_EXTENSION));
    return $ext;
}

echo getExt('1.txt.html.pHP');//php
echo '<br/>';
$filename='3.html';
echo getExt($filename);//html

?>

函数作用域

局部变量--动态变量

<?php

function test1(){
    $i=1;
    $j=2;
    return $i+$j;
}

var_dump(test1());//int 3

var_dump($i,$j);//报错

?>
<?php

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

test();

?>
Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png

局部变量-动态变量

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

test();//1
test();//1

局部变量-动态变量

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

test();//1
test();//2

var_dump($i);//报错
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'] . "<br/>";//用户名为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
)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容