drf65218 2016-08-24 02:10
浏览 37

为什么我不能访问php文件中设置的变量[重复]

I have two php files, say

action.php

require_once 'action_helper.php';

storeDataToDb($data); //function from action_helper.php
logPersistIsPerformed(); //function from action_helper
echo $success; //variable set in action_helper.php

action_helper.php

$success = "success";


function storeDataToDB($data) {
    // persist data
}

function logPersistIsPerformed() {
    insertToDB($success);
}

I'm not sure if this is just a scope issue but what I encounter is when action.php calls the functions and variables declared in action_helper.php there are no issues.

but when I call a function in action_helper.php from action.php, which calls a variable declared in action_helper.php, it doesn't seem to see this success variable.

during debugging, once I loaded the page, I get to see all the variables both from action and action_helper. but when I get to step into the function from action_helper, I'm not able to see the variables declared in action_helper but just the variables passed into that function.

</div>
  • 写回答

1条回答 默认 最新

  • douzhao9608 2016-08-24 02:20
    关注

    You need to use the global keyword to let PHP know that $success is a global variable.

    function logPersistIsPerformed() {
        global $success;
        insertToDB($success);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名