在另一个函数中定义的函数内访问变量? [英] php - Accessing variables within a function defined in another function?

查看:132
本文介绍了在另一个函数中定义的函数内访问变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我在该函数中调用的另一个函数中获取我定义的变量,例如:

I'm trying to get variables that I defined while in a function from another function I called in that function, example:

$thevar = 'undefined';
Blablahblah();
echo $thevar; (should echo blaaah)
function Blahedit(){

     echo $thevar; (should echo blah)
     $thevar = 'blaaah';

}
function Blablahblah(){

     global $thevar;
     $thevar = 'blah';
     Blahedit();

}

我想知道是否有另一种方法将参数传递给Blahedit(),get_defined_vars在函数中给我变量而不是$ thevar ...并调用全局变量$ thevar将给我以前未经编辑的版本。

I want to know if there's another way of doing this without passing down params to Blahedit(), get_defined_vars gives me vars within the function not $thevar... and calling global $thevar will just give me the previous unedited version.

请帮忙):

Please help ):

推荐答案

只要全球$ thevar内blahedit。

Just global $thevar inside blahedit.

function Blahedit(){
    global $thevar;
    echo $thevar; //(should echo blah)
    $thevar = 'blaaah';

}

这篇关于在另一个函数中定义的函数内访问变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆