php声称我定义的变量未定义

时间:2022-11-05 20:50:52

My php is a little rusty but this is boggling my mind right now. I googled this and read all the * questions I could find that looked related, but those all seemed to have legitimate undefined variables in them. That leads me to believe that mine is the same problem, but no amount of staring at the simple bit of code I have reduced this to seems to get me anywhere. Please someone give me my dunce cap and tell me what I did wrong!

我的php有点生疏,但现在这让我很难过。我搜索了这个并阅读了所有看起来相关的*问题,但那些似乎都有合法的未定义变量。这让我相信我的是同样的问题,但没有多少盯着简单的代码我减少了这个似乎让我无处可去。请有人给我我的笨蛋帽,并告诉我我做错了什么!

<?php
//test for damn undefined variable error

$msgs = "";

function add_msg($msg){
  $msgs .= "<div>$msg</div>";
}
function print_msgs(){
  print $msgs;
}

add_msg("test");
add_msg("test2");
print_msgs();
?>

This gives me the following, maddening output:

这给了我以下令人抓狂的输出:

Notice: Undefined variable: msgs in C:\wamp\www\fgwl\php-lib\fgwlshared.php on line 7

注意:未定义的变量:第7行的C:\ wamp \ www \ fgwl \ php-lib \ fgwlshared.php中的消息

Notice: Undefined variable: msgs in C:\wamp\www\fgwl\php-lib\fgwlshared.php on line 7

注意:未定义的变量:第7行的C:\ wamp \ www \ fgwl \ php-lib \ fgwlshared.php中的消息

Notice: Undefined variable: msgs in C:\wamp\www\fgwl\php-lib\fgwlshared.php on line 10

注意:未定义的变量:第10行的C:\ wamp \ www \ fgwl \ php-lib \ fgwlshared.php中的消息

Yes, this is supposed to be a shared file, but at the moment I have stripped it down to just what I pasted. Any ideas?

是的,这应该是一个共享文件,但此刻我已经将它剥离到我粘贴的内容。有任何想法吗?

4 个解决方案

#1


10  

It's defined at the global scope. Use global if you want to use it.

它是在全球范围内定义的。如果要使用全局,请使用全局。

#2


13  

<?php
$msgs = "";

function add_msg($msg){
  global $msgs;
  $msgs .= "<div>$msg</div>";
}
function print_msgs(){
  global $msgs;
  print $msgs;
}

add_msg("test");
add_msg("test2");
print_msgs();
?>

global tells that PHP need to use the global variable in the local function scope.

global告诉PHP需要在本地函数范围内使用全局变量。

#3


5  

Using globals for something like this is a poor practice. Consider an alternate approach such as the following:

对这样的事情使用全局变量是一种糟糕的做法。考虑一种替代方法,如下所示:

class MessageQueue {
  private static $msgs;


  public static function add_msg($msg){
    self::$msgs .= "<div>$msg</div>"; 
  }
  public static function print_msgs(){
    print self::$msgs;
  }
}


MessageQueue::add_msg("test");
MessageQueue::add_msg("test2");
MessageQueue::print_msgs();

#4


1  

if you don't want to use globals, you can jast use

如果你不想使用全局变量,你可以使用它

 function add_msg($msg)
   {
         echo  "<div>$msg</div>";
   }
    add_msg("test");
    add_msg("test2");

function, the result will be the same.

功能,结果会一样。

#1


10  

It's defined at the global scope. Use global if you want to use it.

它是在全球范围内定义的。如果要使用全局,请使用全局。

#2


13  

<?php
$msgs = "";

function add_msg($msg){
  global $msgs;
  $msgs .= "<div>$msg</div>";
}
function print_msgs(){
  global $msgs;
  print $msgs;
}

add_msg("test");
add_msg("test2");
print_msgs();
?>

global tells that PHP need to use the global variable in the local function scope.

global告诉PHP需要在本地函数范围内使用全局变量。

#3


5  

Using globals for something like this is a poor practice. Consider an alternate approach such as the following:

对这样的事情使用全局变量是一种糟糕的做法。考虑一种替代方法,如下所示:

class MessageQueue {
  private static $msgs;


  public static function add_msg($msg){
    self::$msgs .= "<div>$msg</div>"; 
  }
  public static function print_msgs(){
    print self::$msgs;
  }
}


MessageQueue::add_msg("test");
MessageQueue::add_msg("test2");
MessageQueue::print_msgs();

#4


1  

if you don't want to use globals, you can jast use

如果你不想使用全局变量,你可以使用它

 function add_msg($msg)
   {
         echo  "<div>$msg</div>";
   }
    add_msg("test");
    add_msg("test2");

function, the result will be the same.

功能,结果会一样。