简体   繁体   English

如何在 else 语句之后到达 $fileName 变量的 scope ?

[英]How can I reach the scope of the $fileName variable after the else statement?

I would like to find a way to access the file in the else statement but the $fileName scope is local.我想找到一种在 else 语句中访问文件的方法,但 $fileName scope 是本地的。 How can I make it global in both statements?我怎样才能在这两种陈述中使其成为全球性的?

<?php
        if ($_SERVER['QUERY_STRING'] == ''){
    
            $session = rand(1000000, 9999999);
            $fileName = $session;
            $idfile = fopen("$fileName.txt", "w");
            fclose($idfile);
            indexform($session);
    
        }else{
    
            $support = $counter + $support - 1;
            $counter = $support;
    
            if (fsize($idfile) != 0){
                $idfile = fopen($fileName, "r");
                $difficulty = fgets($idfile);
                $number = fgets($idfile);
                $counter = fgets($idfile);
                fclose($idfile)
                $counter++;
            }
            
            $idfile = fopen("$fileName.txt", "w");  
            fputs($idfile, $difficulty);
            fputs($idfile, $number);
            fputs($idfile, $counter);
            fclose($idfile);
            form($counter);
    
        }
    ?>

You could declare it, outside the if/else statement.您可以在 if/else 语句之外声明它。

Take a look at this reference: https://www.php.net/manual/en/language.variables.scope.php看看这个参考: https://www.php.net/manual/en/language.variables.scope.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM