【问题标题】:How to get $var outside the function without using global php?如何在不使用全局 php 的情况下在函数之外获取 $var?
【发布时间】:2012-05-01 21:52:45
【问题描述】:

在这里需要一点帮助。正在阅读在函数中添加全局变量并在外部调用它是多么糟糕,但是在外部获取变量时遇到了小问题。全球帮助,但我也想安全,因为我在这里处理一些文件

我的循环是这样的

<?php 
require_once('functions.php'); 
?>

<?php 
foreach( $files as $key=> $file ){  

global $download_link;
get_file_info($key,$file);
?>

<span><a href="<?php echo $download_link ?>"><?php echo $file->name ?></a></span>

<?php } ?>

PART 我的 function.php / 大约 150 行,但这是主要片段

function  get_file_info($key,$file){

global $download_link;
$access     = explode(",",$file->access);
$permission = in_array(1,$access);

if($permission){

$download_link = 'ok to download';
}else{
$download_link = 'canot download';
}


}

除了链接变量之外,我还有一些其他的,比如 date 、 counter 等,但它们都受某些条件的约束。

我试过了

返回$链接;在函数的末尾,而不是使用全局但得到未定义的变量错误;

这里的基本问题是,如何在不使用全局的情况下获取函数外部的 download_link var?

【问题讨论】:

    标签: php function global-variables


    【解决方案1】:

    你可以通过修改你的 File 类来更容易地做到这一点

    class File {
    
        # ...
    
        function get_url() {
            return in_array(1, explode(',', $this->access))
                ? $this->url  # return the file's url
                : "/path/to/subscribe" # return a default path for non-access
            ;
        }
    }
    

    您的 HTML 将按如下方式使用它

    <?php
    
    foreach ($files as $file) {
        echo '<a href="'.$file->get_url().'">Download this '.$file->name.'</a>';
    }
    

    【讨论】:

      【解决方案2】:

      既然你只是用get_file_info来设置$download_link,那为什么不直接返回$permission,在函数外定义$download_link呢?

      <?php 
      function  get_file_info($key,$file){
          $access     = explode(",",$file->access);
          $permission = in_array(1,$access);
          return $permission;
      }
      
      foreach( $files as $key=> $file ){  
          $download_link = 'canot download';
          if(get_file_info($key,$file)) {
              download_link = 'ok to download';
          }
          echo '<span><a href="$download_link ">'. $file->name . '</a></span>';
      } 
      ?>
      

      【讨论】:

      • like 建议它不仅仅是我在该函数中拥有的链接变量。大约 150 行长,除了函数中的 vars after 条件外,一切正常
      • 好的,但是你能让函数返回$permission吗?如果是这样,我的回答仍然有效。
      • 我的一切都在条件范围内,试图在这里制作干净的文件并分离输出,所以我宁愿把一切都放在函数中。这就是为什么我试图远离循环文件的条件,或者我应该说 html 输出文件
      • 您在问题上说您尝试返回$link,但出现错误。那是因为没有$link 变量,你可以返回$download_link。另外,您说要避免使用全局变量以“保持安全”。全局变量的问题与安全无关,它与命名空间冲突和代码可维护性有关。
      • 我返回了 $download_link;没有正确评论,我得到 undefined var 错误,我确信所有 var 名称都是正确的
      【解决方案3】:

      你可以像这样改变你的循环:

          <?php 
      require_once('functions.php'); 
      ?>
      
      <?php 
      foreach( $files as $key=> $file ){  
      
         $download_link = get_file_info($key,$file);
      
      ?>
      
      <span><a href="<?php echo $download_link ?>"><?php echo $file->name ?></a></span>
      
      <?php } ?>
      

      还有你的功能代码:

        function  get_file_info($key,$file){
       $access     = explode(",",$file->access);
       $permission = in_array(1,$access);
      
        if($permission){
      
      return  'ok to download';
        }
        else {
      return 'canot download';
        }
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-19
        • 1970-01-01
        • 1970-01-01
        • 2018-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多