这是将两个相等的语句合而为一的正确方法吗?

发布于 2024-12-08 19:48:55 字数 325 浏览 0 评论 0原文

我正在做同样的声明,但它不起作用。

if ( $1 === $one ) && ( $2 == $two ) {
    require("one.php");
} elseIF($2 === $two) {
    require("two.php");
}else{ 
    require("three.php");
    die("");
}

这是我收到的错误消息:

   PHP Parse error:  syntax error, unexpected T_BOOLEAN_AND 

I am doing this equal statement and it's not working.

if ( $1 === $one ) && ( $2 == $two ) {
    require("one.php");
} elseIF($2 === $two) {
    require("two.php");
}else{ 
    require("three.php");
    die("");
}

And this is the error message i get:

   PHP Parse error:  syntax error, unexpected T_BOOLEAN_AND 

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

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

发布评论

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

评论(5

何其悲哀 2024-12-15 19:48:55

您需要执行以下操作:

if (( $1 === $one ) && ( $2 == $two )) {

或者:

if ( $1 === $one  &&  $2 == $two ) {

请记住将其视为放置方括号的代数,需要将 'IF@ 语句作为一个整体进行评估,并将您希望评估的所有标准包含在 ( 和 ) 中。根据您希望进行评估的复杂程度(以公式化方式构建),您可以将后续标准括在括号中,例如

x || y
x && y
x && (y || z)
x || (y && z)

You need to do:

if (( $1 === $one ) && ( $2 == $two )) {

Or:

if ( $1 === $one  &&  $2 == $two ) {

Remember to think of it as algebra in where you put brackets, the 'IF@ statement needs to be evaluated as a whole with all criteria you wish to evaluate being wrapped within ( and ). Depending on how complex you wish to make your evaluations (build it formulaically) you can then wrap subsequent criteria in brackets, e.g.

x || y
x && y
x && (y || z)
x || (y && z)
稀香 2024-12-15 19:48:55

应该是:

if (( $1 === $one ) && ( $2 == $two )) {
    require("one.php");
} elseIF($2 === $two) {
    require("two.php");
} else { 
    require("three.php");
    die("");
}

Should be:

if (( $1 === $one ) && ( $2 == $two )) {
    require("one.php");
} elseIF($2 === $two) {
    require("two.php");
} else { 
    require("three.php");
    die("");
}
偷得浮生 2024-12-15 19:48:55

您需要将第一个 IF 语句中的两个条件括在附加括号中:

if (( $1 === $one ) && ( $2 == $two ))

You need to wrap the two conditions in your first IF statement in additional brackets:

if (( $1 === $one ) && ( $2 == $two ))
止于盛夏 2024-12-15 19:48:55
if (( $1 === $one ) && ( $2 == $two )) {
    require "one.php";
} elseif ($2 === $two) {
    require "two.php";
} else { 
    require "three.php";
    die("Some message");
}
if (( $1 === $one ) && ( $2 == $two )) {
    require "one.php";
} elseif ($2 === $two) {
    require "two.php";
} else { 
    require "three.php";
    die("Some message");
}
月棠 2024-12-15 19:48:55

不,问题出在第一行。

do:

if ($1 === $one && $2 == $two) { ...

“if”条件始终位于单个“()”内。您可以嵌套它们,但 if 条件在第一个条件关闭后停止。所以 if (($1 === $one) && ($2 == $two)) 也是有效的,但在这种情况下,不需要嵌套它,除非你发现它提高可读性。

一些批评:

这些变量名非常糟糕,因为它们没有解释发生的事情。与文件名相同。

请在上面加上糖,修复你的缩进(查看 draevor 的答案以获得合适的缩进)。立即习惯正确的缩进,因为以后习惯改变的做法只会变得更加困难。当其他人开始使用你的代码时,如果你继续这样缩进,他们会诅咒你的名字。这对于可读性确实很重要。

No, the problem is in the first line.

do:

if ($1 === $one && $2 == $two) { ...

The "if" condition is always within a single "()". You can nest them, but the if-condition stops after the first one is closed. So if (($1 === $one) && ($2 == $two)) is valid as well, but in this case there's no need for nesting it unless you find that it improves readability.

A bit of criticism to the side:

Those variable names are very bad, since they don't explain anything about what's going on. Same with the file names.

And pretty please, with sugar on top, fix your indenting (check out draevor's answer for decent indenting). Get used to proper indenting right away, since it will only get more difficult to get used to a changed practice later. When someone else starts to work with your code, they'll curse your name if you keep indenting like that. It really does matter for readability.

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