PHP 的行为因计算机而异
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这两台机器可能有不同的警告级别或错误报告级别。
我很困惑,你肯定应该在任何 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
这段代码对我来说没有什么意义:
它应该这样写:
当你说它“中断”时,你能给出一个具体的例子/你得到的错误以及你如何使用该函数吗? 我认为您使用它的方式不正确。
This code makes very little sense to me:
It should be written like so:
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.
为了简化这个问题的调试,有许多最佳实践值得尊重,这些 BP 也将使问题更加清晰地出现,并有望从根本上解决它......
1)首先你应该确保你的php.ini 一致,并且以下参数设置为最佳值:
2)避免使用“@”运算符来截取错误消息,必须处理该错误消息,而不是隐藏! 此外,这是一个性能问题
3) 选择正确的构造、运算符或函数来测试条件。
IE,如果您正在测试变量不是“空”,则不要针对空字符串($v==“”)测试它,这似乎通常有效,但它依赖于运算符 == 强制的隐式强制转换,如果您使用了 === 运算符,那么测试将不是您想要的,当然
4)正如 Daniel 指出的,该函数可以用更好的方式重写:
如果你[想要|不能]改变你的 php ini ,这是一个很好的做法
运行时设置日志级别,将其设置在配置文件中作为调试功能,例如
我的答案是完全启用每个错误报告,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:
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:
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
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 :)
是否其中一台计算机正在使用旧版本的 PHP?
Could it be one of the computers is using an older version of PHP?