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

如何在另一个函数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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀