首页 > 解决方案 > PHP/作用域——在函数内部设置var,在外部访问而不返回

问题描述

我觉得很愚蠢,但范围每次都让我感到困惑。我也不擅长 PHP。在这种情况下,如何在函数内部设置变量,$httpcode然后在该函数外部访问它?

<?php

  function get_feed() {
    $ch       = curl_init('someurl');
    $output   = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // how to use this anywhere?
    // try / catch
    curl_close($ch);
  }

  // later down the page

  if ($httpcode == "200") {
    // do this
  }
?> 

标签: phpvariablesscope

解决方案


推荐阅读