dsce23640 2014-04-22 15:40
浏览 52
已采纳

如何在另一个函数php中使用变量

 function pokemon1()
{ 
include 'details.php';
 $path = $_SESSION['path'];
 $health = $_SESSION['health'];
 echo "<br />";
 echo $path;
 echo "<br />";
 echo $health;
}


function pokemon2()
{
include 'details.php';
 $flag =  $_SESSION['flag'];
 $damage = $_POST['attack']; 
 $oppo_health = $_SESSION['oppo_health']; 

 $oppo_path = $_SESSION['path'];
 echo "<br />";
 echo $oppo_path;
 echo "<br />"; 
   $oppo_health = $oppo_health - $damage;
  echo $oppo_health;
 $_SESSION['attack'] = $damage; 
 $_SESSION['oppo_health'] = $oppo_health;

}

How can I use the $path variable of function pokemon1() in function pokemon2()??? I tried declaring outsite the function with "global" but still its showing undefined!

  • 写回答

4条回答 默认 最新

  • douao2019 2014-04-22 15:48
    关注

    You have to set the variable inside Pokemon 1 to global, after you do that, make sure you call function Pokemon 1 first, else the variable inside pokemon 2 with the variable from pokemon 1 will be undefined.

    Also, why don't you just declare the variable outside the scope of a function?

     $path = "";
    
    function pokemon1(){
        ....
        global $path...;
    }
    
    function pokemon2(){
       ....
      global $path...;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制