Home  >  Article  >  php教程  >  解决关于PHP“Undefined variable”变量未定义

解决关于PHP“Undefined variable”变量未定义

WBOY
WBOYOriginal
2016-05-25 16:45:461985browse

php中变量可以不定义的但是我们如果不对错误进行一些处理在使用未定义的变量时会提示Undefined variable错误了,下面我给大家举几个实例.

PHP错误提示"Undefined variable":变量未定义,如果你在php.ini设置中屏蔽了Notice这个等级的提示那么他就不会显示任何提示了,但是为了程序的严谨性考虑,我们尽量避免任何警告、错误等明显的程序缺陷.

例一:class.Chinese.php中以下代码就会出错"Undefined variable".

for ( $i=0; $i<strlen($hexdata); $i+=2 )
$bindata.=chr(hexdec(substr($hexdata,$i,2)));

正确的写法应该是如下代码:

$bindata='';
for ( $i=0; $i<strlen($hexdata); $i+=2 )
$bindata.=chr(hexdec(substr($hexdata,$i,2)));

例二:以下代码也会出错"Undefined variable":

$sl = "zh-CN"; 
$tl = "en"; 
function app_out($c,$gbk){ 
    $data = app_get_translate($c,$sl,$tl); 
    $out = str_replace($c,$data,$c); 
    return app_js_out($out,$gbk); 
}

正确的写法应该是:

$sl = "zh-CN"; 
$tl = "en"; 
function app_out($c,$gbk){ 
    global $sl,$tl; //将此函数体内的这两个变量定义为全局变量,以便使用开头设定的值 
    $data = app_get_translate($c,$sl,$tl); 
    $out = str_replace($c,$data,$c); 
    return app_js_out($out,$gbk); 
}

本来php是不需要定义变量的,但是出现这种情况应该怎么办呢?只要在C:WINDOWS找出php.ini,在php.ini中的302行 error_reporting = E_ALL修改成

error_reporting = E_ALL & ~E_NOTICE再重启apache2.2就行了

如果什么错误都不想让显示,直接修改:display_errors = Off

如果你没有php.ini的修改权限,可在php头部加入

ini_set("error_reporting","E_ALL & ~E_NOTICE");


教程链接:

随意转载~但请保留教程地址★

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn