如何防止 PHP 通知已定义的变量?

发布于 2024-08-15 22:27:33 字数 312 浏览 4 评论 0原文

基本上,我在一个页面上有一堆链接,我会

<?PHP echo $SITE_PATH ?>

在同一页面上多次使用类似的链接,但它会在 PHP 中显示这样做的通知。

我知道你应该使用像 isset() 这样的东西,但我真的需要每次打电话时都使用它吗?

<?PHP echo $SITE_PATH ?>

---编辑:
如果我改用已定义的变量,那么通知似乎就会消失

Basicly I have a bunch of links on a page and I will use something like this

<?PHP echo $SITE_PATH ?>

Many many times on the same page but it will show a Notice in PHP for doing so.

I know you are supposed to use things like isset() but would I really need to use it every time I call?

<?PHP echo $SITE_PATH ?>

--- EDIT :
If I switch to using a defined variable, then the notices seem to go away

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鹿童谣 2024-08-22 22:27:33

这里有两个解决方案:

  • 仅使用存在的变量——这可能是您应该做的。
  • 或者在尝试使用它们之前测试它们是否存在(使用 isset)。

没有什么神奇的^^

如果您的应用程序使用了许多未设置的变量,则您的设计可能存在一些问题。

在您提出的情况下,对于多次使用的变量,我会确保它存在,如果不存在,则将其设置为“”,例如在脚本的开头。

这看起来不太好,但它会起作用——这样,您就不必检查整个应用程序,修补所有内容。

(或者我也可能禁用 E_NOTICE error_reporting 级别 - 我不喜欢这个想法,但是,有时,这确实是处理某些代码库的唯一方法)

Two solutions, here :

  • only use variables that exist -- which is what you should probably do.
  • or test if they exist (with isset) before trying to use them.

There is no magic ^^

If your application is using many not-set variables, you probably have some problem in your design.

In a case such as the one you presented, for a variable that's used lots of times, I would make sure it exists, and, if not, set it to '', for instance, at the beginning of my script.

That's not really looking great, but it'll work -- and, this way, you won't have to go through your whole application, patching everything.

(Or I might also disable the E_NOTICE error_reporting level -- I don't like that idea, but, sometimes, it's really the only way to deal with some code base)

口干舌燥 2024-08-22 22:27:33

您可以使用@来抑制消息:

print @$site_path;

或者您可以使用三元运算来获取默认值:

print (isset($site_path)) ? $site_path : "default_path" ;

但最后,您不应该使用未设置的变量。如果是,您需要重新考虑您的方法。预先处理此信息,以便其余脚本可以运行而不会出现此类问题。

You can supress messages by using @:

print @$site_path;

Or you can use a ternary operation to get a default value:

print (isset($site_path)) ? $site_path : "default_path" ;

In the end though, you shouldn't be using variables that aren't set. If you are, you need to re-think your approach. Handle this information up-front, so the rest of your scripts can run without problems like this.

油焖大侠 2024-08-22 22:27:33

您确实应该确保在使用变量之前已设置该变量(尤其是将其回显到页面)。如果变量来自不安全的来源($_GET、$_POST、数据库),那么您应该执行某种类型的过滤以防止安全漏洞(跨站点脚本(XSS)、跨站点请求伪造(CSRF)等)。 .) 但如果您觉得一切都很安全并且您只是不想显示错误(例如在生产中),请将错误报告设置为 0。

例如 error_reporting(0);

您可以在php.ini 级别或每页(在页面顶部设置 error_reporting(0);)。

附带说明一下,在生产过程中您永远不想显示错误。相反,记录它们。在开发过程中,您希望看到所有错误(E_STRICT)。

You really should make sure a variable is set before using it (especially echoing it to the page). If the variable is coming from an insecure source ($_GET, $_POST, database) then you should do some type of filtering to prevent a security breach (cross site scripting (XSS), cross site request forgery (CSRF), etc...) but if you feel like everything is safe and you just don't want to show errors (e.g. in production) set your error reporting to 0.

E.g. error_reporting(0);

You can do this at the php.ini level or per page (set error_reporting(0); at the top of the page).

On a side note, when in production you don't ever want to display errors. Log them instead. In development you want to see all of your errors (E_STRICT).

生生不灭 2024-08-22 22:27:33

您可以创建一个函数来检查变量是否已设置并返回它:

function EchoVar() {
    global $SITE_PATH;
    return isset($SITE_PATH) ? $SITE_PATH : '';
}

// calling it
echo EchoVar();

You could create a function to check if the variable is set and return it:

function EchoVar() {
    global $SITE_PATH;
    return isset($SITE_PATH) ? $SITE_PATH : '';
}

// calling it
echo EchoVar();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文