添加 isset 时是否始终设置为 true

发布于 2024-12-31 22:28:04 字数 1029 浏览 0 评论 0原文

乍一看,这个片段似乎没问题。

// my_controller.php
$data['errors'] = (object)$this->session->userdata('errors_registration');
$this->load->view('registration/my_view',$data);

// print_r($data['errors'])
// stdClass Object
// (
//    [domain_err] => 0
//    [wbholder_err] => 0
//    [methodpayment_err] => 0
//    [createaccount_err] => 2
//    [gtc_err] => 0
// )

// my_view.php
if($errors->domain_err == 1) echo "TRUE"; else echo "FALSE"; // FALSE

$data['errors'] 为空时,

// print_r($data['errors'])
// stdClass Object
// (
//    [scalar] => 
// )

// my_view.php
if($errors->domain_err == 1) echo "TRUE"; else echo "FALSE";

我明白了,

遇到 PHP 错误 严重性:通知消息:未定义属性:stdClass::$domain_err

为了解决该通知,我添加了 isset(),现在将我的代码片段添加到 if(isset($errors->domain_err) == 1),但是,由于此更改,条件似乎始终设置为 true,从而输出 TRUE。有没有其他方法可以克服通知,同时保持我期望的条件输出?

At first look, this snippet seems to be fine.

// my_controller.php
$data['errors'] = (object)$this->session->userdata('errors_registration');
$this->load->view('registration/my_view',$data);

// print_r($data['errors'])
// stdClass Object
// (
//    [domain_err] => 0
//    [wbholder_err] => 0
//    [methodpayment_err] => 0
//    [createaccount_err] => 2
//    [gtc_err] => 0
// )

// my_view.php
if($errors->domain_err == 1) echo "TRUE"; else echo "FALSE"; // FALSE

And when, $data['errors'] is empty,

// print_r($data['errors'])
// stdClass Object
// (
//    [scalar] => 
// )

// my_view.php
if($errors->domain_err == 1) echo "TRUE"; else echo "FALSE";

I get this,

A PHP Error was encountered Severity: Notice Message: Undefined property: stdClass::$domain_err

To address the notice, I added isset(), making my snippet now to if(isset($errors->domain_err) == 1), however, since this change, the condition seems to be always set to true, thus outputting TRUE. Is there other way around to get over the notice while maintaining the conditional output I am expecting?

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

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

发布评论

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

评论(3

雪若未夕 2025-01-07 22:28:04

它始终为 true 的原因是因为您现在将其与函数返回的内容进行比较,这是 true 因为 $errors->domain_err 已设置。您需要做的是将条件更改为:

if(isset($errors->domain_err) && $errors->domain_err == 1)

isset 不返回变量的值,只返回变量是否存在或不。

The reason for it always being true, is because you are comparing it now to what the function returns, which is true because $errors->domain_err is set. What you need to do is change the condition to:

if(isset($errors->domain_err) && $errors->domain_err == 1)

isset does not return the value of the variable, just whether it exists or not.

梦过后 2025-01-07 22:28:04

为了避免重复这种模式,

if (isset($errors->domain_err) && $errors->domain_err == 1)

您可以编写这样的函数

function check_value(&$variable) {
  return (isset($variable) ? &$variable : false);
}
if (check_value($errors->domain_err)) { echo "ERROR"; }
else { echo "NO ERROR"; }

to avoid repeating this pattern

if (isset($errors->domain_err) && $errors->domain_err == 1)

you can write a function like this

function check_value(&$variable) {
  return (isset($variable) ? &$variable : false);
}
if (check_value($errors->domain_err)) { echo "ERROR"; }
else { echo "NO ERROR"; }
另类 2025-01-07 22:28:04

您为什么要尝试“克服”错误消息?修复导致错误的任何原因。在这种情况下,您引用的成员在您正在使用的对象中不存在。在您发布的代码中,我没有看到您实例化 $errors 类的位置,也没有从中发布任何代码。去查看任何类,看看是否看到定义了名为 $domain_err 的成员变量,并确保它被定义为公开可用。就我个人而言,我不喜欢给成员变量提供公共范围,因为我更喜欢提供 get/set 类型函数来设置或返回它们的值。

Why would you try to "get over" the error message? Fix whatever is causing the error. In this case you are referencing a member that doesn't exist in the object you are using. In the code you posted I do not see where you instantiated the $errors class nor did you post any code from it. Go look in whatever class that is and see if you see a member variable named $domain_err defined and make sure it is defined to be publicly available. Personally I don't like giving member variables public scope as I prefer to provide a get/set type function to set or return the values of them.

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