当前位置:首页> PHP教程> PHP精通
关键字
文章内容
外部字符串里出现还未定义
 
 
修改时间:[2008/10/01 05:54]    阅读次数:[955]    发表者:[起缘]
 
先在函数外定义一个字符串:
PHP代码:

$strings
="<TR align=center onMouseOver=this.style.backgroundColor='#ffffff' onMouseOut=this.style.backgroundColor='#F5F9FD' bgcolor='#F5F9FD'><TD width=191><a href='info.php?id=$row[4]'>$row[0]</TD><TD width=212>$row[1]</TD><TD width=202>$row[2]</TD><TD width=189>$row[3]</TD></TR>";



想要用后面函数里的循环
PHP代码:

while($row=mysql_fetch_array($result)){echo $strings; }

显示n列这样的字符串。
但是php会把字符串里的$row解释为变量(而不是字符串常量)并提示未定义变量row。

 

解决办法:

PHP代码:




$strings
='<TR align=center onMouseOver=this.style.backgroundColor='#ffffff' onMouseOut=this.style.backgroundColor='#F5F9FD' bgcolor='#F5F9FD'><TD width=191><a href='info.php?id=$row[4]'>$row[0]</TD><TD width=212>$row[1]</TD><TD width=202>$row[2]</TD><TD width=189>$row[3]</TD></TR>';



while($row=mysql_fetch_row($result)) {

  eval(
"echo \"$strings\";");

}



定义$strings时用单引号,最后使用eval("echo \"$strings\";");