栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

为什么要修复E_NOTICE错误?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

为什么要修复E_NOTICE错误?

摘要

在PHP运行时配置文件给你一些想法,为什么:

在开发过程中启用E_NOTICE有一些好处。

出于调试目的:NOTICE消息将警告您代码中可能存在的错误。例如,警告使用未分配的值。查找输入错误并节省调试时间非常有用。

NOTICE消息将警告您样式不良。例如,最好将$ arr [item]编写为$ arr [‘item’],因为PHP试图将“
item”视为常量。如果不是常量,PHP会假定它是数组的字符串索引。

这是每个的更详细的说明…


1.检测打字错误

E_NOTICE
错误的主要原因是错别字。

示例-notice.php

<?php$username = 'joe';        // in real life this would be from $_SESSION// and then much further down in the pre...if ($usernmae) { // typo, $usernmae expands to null    echo "Logged in";}else {    echo "Please log in...";}?>

没有E_NOTICE的输出

Please log in...

错误!你不是那个意思!

用E_NOTICE输出

Notice: Undefined variable: usernmae in /home/user/notice.php on line 3Please log in...

在PHP中,不存在的变量将返回null而不是导致错误,并且可能导致代码的行为与预期不同,因此最好注意

E_NOTICE
警告。


2.检测歧义索引

它还警告您可能会改变的数组索引,例如

示例-今天的代码看起来像这样

<?php$arr = array();$arr['username'] = 'fred';// then further downecho $arr[username];?>

没有E_NOTICE的输出

fred

示例-明天您将包括图书馆

<?php// tomorrow someone adds thisinclude_once('somelib.php');$arr = array();$arr['username'] = 'fred';// then further downecho $arr[username];?>

库执行以下操作:

<?phpdefine("username", "Mary");?>

新的输出

空的,因为现在它扩展为:

echo $arr["Mary"];

并没有关键

Mary
$arr

用E_NOTICE输出

如果只有程序员

E_NOTICE
使用,PHP会显示一条错误消息:

Notice: Use of undefined constant username - assumed 'username' in /home/user/example2.php on line 8fred

3.最佳原因

如果您没有解决所有

E_NOTICE
您认为不是错误的错误,则您可能会变得自满,并开始忽略消息,然后有一天发生真正的错误,您将不会注意到它。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/412081.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号