PHP 的行为因计算机而异

发布于 2024-07-26 20:25:42 字数 776 浏览 4 评论 0原文

我有一个 php 问题,其中代码可以在计算机上运行,​​但不能在另一台

    function appendParam(&$req, $name, $value) {
    if (req == null) {
        return;
    }
    if (name == null) {
        return;
    }
    if (value != null) {
        $req[$name] = $value;
    }
}

计算机上运行上面的代码可以在一台计算机上运行,​​并且能够正确检查 req 和 name 是否为空,并且变量是 if 条件不需要美元符号(当我把美元符号放进去,它们会在这台计算机上损坏)

但我需要在另一台计算机上使用以下代码才能

    function appendParam(&$req, $name, $value) {
    if ($value != null) {
        $req[$name] = $value;
    }   
    if ($name == null) {
        return;
    }
    if ($req == null) {
        return;
    }
}

在另一台计算机上获得相同的最终结果,它无法检查名称或请求是否为空(它会损坏) 我需要 if 条件中的变量上有美元符号。

作为旁注,这台计算机似乎也无法从尚未初始化的数组索引中读取。

任何帮助表示赞赏

I have an issue with php where code works on on computer but wont work on another

    function appendParam(&$req, $name, $value) {
    if (req == null) {
        return;
    }
    if (name == null) {
        return;
    }
    if (value != null) {
        $req[$name] = $value;
    }
}

The above works on one computer and is capable of checking req and name against null properly and the variables is the if condition don't need dollar signs (when i put the dollar signs in they break on this computer)

but i need to use the following code on another computer to get the same end result

    function appendParam(&$req, $name, $value) {
    if ($value != null) {
        $req[$name] = $value;
    }   
    if ($name == null) {
        return;
    }
    if ($req == null) {
        return;
    }
}

on this other computer it isn't capable of checking name or req against null (it breaks)
and i need the dollar signs on the variables in the if condition.

As a side note it also seems that this computer can't read from an array index that isn't already initialized.

Any help is appreciated

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

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

发布评论

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

评论(4

妄想挽回 2024-08-02 20:25:42

这两台机器可能有不同的警告级别或错误报告级别。

我很困惑,你肯定应该在任何 php 变量名之前有 $ 前缀,否则你实际上并没有检查变量? 您想通过不使用 $ 前缀来实现什么目的

The 2 machines might have different warning levels or error reporting levels.

I'm confused, surely you should have the $ prefix before any php variable name otherwise you're not actually checking on the variable? What are you trying to achieve by not using the $ prefix

昵称有卵用 2024-08-02 20:25:42

这段代码对我来说没有什么意义:

    function appendParam(&$req, $name, $value) {
    if ($value != null) {
                $req[$name] = $value;
        }       
    if ($name == null) {
                return;
        }
    if ($req == null) {
                return;
        }
}

它应该这样写:

function appendParam(&$req, $name, $value)
{
 if(empty($name) || empty($value) || empty($req))
  return;

 $req[$name] = $value;
}

当你说它“中断”时,你能给出一个具体的例子/你得到的错误以及你如何使用该函数吗? 我认为您使用它的方式不正确。

This code makes very little sense to me:

    function appendParam(&$req, $name, $value) {
    if ($value != null) {
                $req[$name] = $value;
        }       
    if ($name == null) {
                return;
        }
    if ($req == null) {
                return;
        }
}

It should be written like so:

function appendParam(&$req, $name, $value)
{
 if(empty($name) || empty($value) || empty($req))
  return;

 $req[$name] = $value;
}

When you say it "breaks" can you give a specific example/error you're getting and how you're using the function? I'm thinking you're using it incorrectly.

忘东忘西忘不掉你 2024-08-02 20:25:42

为了简化这个问题的调试,有许多最佳实践值得尊重,这些 BP 也将使问题更加清晰地出现,并有望从根本上解决它......

1)首先你应该确保你的php.ini 一致,并且以下参数设置为最佳值:

allow_asp_tag = 关闭; 通常不需要有这样的标签
Short_open_tags = 关闭; 这可以避免处理 xml 数据/文档时的麻烦
register_globals = 关闭; 这是 4.1 之前邪恶的默认设置
log_errors = 开; 这两行
显示错误=关闭; 当需要安全性时,对生产环境充满希望
error_reporting = E_ALL & E_STRICT; 这使得每个错误都被显示

2)避免使用“@”运算符来截取错误消息,必须处理该错误消息,而不是隐藏! 此外,这是一个性能问题

3) 选择正确的构造、运算符或函数来测试条件。
IE,如果您正在测试变量不是“空”,则不要针对空字符串($v==“”)测试它,这似乎通常有效,但它依赖于运算符 == 强制的隐式强制转换,如果您使用了 === 运算符,那么测试将不是您想要的,当然

4)正如 Daniel 指出的,该函数可以用更好的方式重写:


function appendParam(&$req, $name, $value)
{
    if (empty($name) || empty($value) || empty($req)) {
return; } $req[$name] = $value; }

如果你[想要|不能]改变你的 php ini ,这是一个很好的做法
运行时设置日志级别,将其设置在配置文件中作为调试功能,例如

    error_reporting = E_ALL | E_STRICT;

我的答案是完全启用每个错误报告,PHP 会告诉你一些奇怪的事情,这种奇怪的情况(可能是通知)将是你问题的根源。

我的 2 美分:)

There are a number of best practices to be honored in order to simplify the debugging of this issue, these BP also will make the problem arise more clearly and hopefully solve it at its root ...

1) first of all you should assure that your php.ini are consistent and that the following parameters are set to they optimal value:

allow_asp_tag = Off ; there is no need to have such a tag usually
short_open_tags = Off ; this avoids headaches when working with xml data/documents
register_globals = Off ; this is the evil pre-4.1 default
log_errors = On ; this two lines
show_errors = Off ; are hopeful in production envs, when security is a need
error_reporting = E_ALL & E_STRICT ; This makes every error to be displayed

2) avoid the use of the "@" operator to cut out error messages, the have to be handled, not hidden! Moreover this is a performance issue

3) choose the right construct, operator or function to test conditions.
I.E., if you are testing a variable is not "empty" not test it against the empty string ($v==""), this seems to work in general, but it relies on the implicit cast that the operator == forces, if you'd have used the === operator the test would not been what you wanted, of course

4) as Daniel pointed out, the function may be rewritten in a better way:


function appendParam(&$req, $name, $value)
{
    if (empty($name) || empty($value) || empty($req)) {
return; } $req[$name] = $value; }

If you [want|can]'t change your php ini it's a good practice to
runtime set the log level, setting it in a config file as a debugging feature for example

    error_reporting = E_ALL | E_STRICT;

My answer is to fully enable every error reporting, and PHP will tell you something is strange, that strangeness (probably it will be a NOTICE) will be the root of you issue.

My 2 cents :)

谷夏 2024-08-02 20:25:42

是否其中一台计算机正在使用旧版本的 PHP?

Could it be one of the computers is using an older version of PHP?

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