php回音问题

时间:2011-03-18 15:49:01

标签: mysql arrays echo

$this->get_name = mysqli_fetch_assoc(mysqli_query($this->link,"SELECT complete_name FROM student WHERE email='$this->email'"));

mysqli_query($this->link, $this->drop_query) or die(mysql_error());

echo $this->get_name['complete_name'].  " has been marked as Left!";

当我运行时,我得到以下内容:

“约翰被标记为左!”

但是当我跑步时

echo ".$this->get_name['complete_name'] has been marked as Left!";

我明白了:

“Array ['complete_name']已标记为Left!”

他们不应该打印相同的东西吗?

3 个答案:

答案 0 :(得分:3)

PHP:s解析器不知道变量在第二种情况下的结束位置,使用{}将变量封装在字符串中

echo "{$this->get_name['complete_name']} has been marked as Left!";

答案 1 :(得分:1)

如果要获取数组偏移量,请使用:

{$this->get_name['complete_name']}

使用括号告诉PHP,如果可能的话,期望将整个包含用作变量。

有关详细信息,请参阅PHP's String Parsing。作为他们的例子的一部分,他们有这个例子:

// Works, quoted keys only work using the curly brace syntax
echo "This works: {$arr['key']}";

答案 2 :(得分:0)

每当我回复比$complete_name更复杂的东西时,我都不会在引号中包含它:

echo $this->get_name['complete_name'].' has been marked as Left!';

我发现阅读整篇引文很容易,因为在处理对象和数组等时,句子会丢失。所以,如果你想轻松阅读引用行,我可能建议使用:

$complete_name = $this->get_name['complete_name'];   # deal with the dirty stuff here 

echo "$complete_name has been marked as left!";      # so this is much easier to read