douzi9211 2012-05-01 05:12
浏览 34
已采纳

简单的PHP IF语句无法正确评估

This PHP IF statement always results in 'false' and I can't figure out why. Any help is greatly appreciated.

Where $i is passed in as 0 and $j is passed in as 4 (these numbers were verified in the POST)

$i = $_POST['entry'];
$j = $_POST['j'];

function tabs() {
  if ($i < $j)
    echo 'i is less than j';
  else
    echo 'false';
};
  • 写回答

1条回答 默认 最新

  • dtny30176 2012-05-01 05:30
    关注

    $i and $j are not within the visible scope of tabs():

    … [W]ithin user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope.

    Pass them as parameters or make them global (the first being the preferred way):

    Parameters

    $i = $_POST['entry'];
    $j = $_POST['j'];
    
    function tabs($i, $j) {
      if ($i < $j)
        echo 'i is less than j';
      else
        echo 'false';
    };
    
    tabs($i, $j);
    

    Global

    $i = $_POST['entry'];
    $j = $_POST['j'];
    
    function tabs() {
      global $i, $j;
      if ($i < $j)
        echo 'i is less than j';
      else
        echo 'false';
    };
    
    tabs();
    

    EDIT
    Alternatively, you can access the superglobal $_POST array from within the tabs() function directly, or $_GLOBALS['i'] and $_GLOBALS['j'], respectively.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图