When to use a variable variable in PHP?

后端 未结 8 1738
-上瘾入骨i
-上瘾入骨i 2021-01-03 03:49

I\'ve been developing in PHP for a while now, and I still have not had a task where I\'ve had to use variable variables. Can anyone give me examples where using them is a go

8条回答
  •  -上瘾入骨i
    2021-01-03 04:23

    I've found a quite good one..

    $php = "templates/php/default.php";
    $html = "templates/html/default.php";
    $css = "templates/css/default.php";
    $js = "templates/js/default.php";
    

    now i asked the user to say which file he wants php or/and html..

    $userarray = array("php", "css");
    foreach($userarray as $file){
        var_dump($$file);
    }
    

    output:

    templates/php/default.php
    templates/css/default.php

    I've crossed myself with this when trying to scope static variables self::$file; like this then I remembered I could use variable variables self::$$file; which will be interpreted as self::$php;

提交回复
热议问题