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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100