PHP - 如果语句总是评估为真?

时间:2015-11-11 18:08:07

标签: php if-statement

所以我无法理解这句话。

我想要做的是从$ _SESSION [“user_name”]获取登录的人,如果它与两个可能的“admin”用户中的一个匹配,则提供额外的功能。但它始终是真的。

对于这个例子,我将有三个用户:Emma,John和Robert。艾玛和约翰应该被允许额外的“管理功能”,但罗伯特不被允许。所以......

我的陈述是:

if ($_SESSION["user_name"] === "emma" or "john"){
//give extra functionality as well as basic functionality
} else {
//give just basic functionality

但这总是触发真实,所以当我使用用户名Robert登录我的测试帐户时,他也获得了额外的功能。

我尝试将"的引号$_SESSION更改为撇号',并尝试将运算符从===更改为==。我甚至试过=,但发现这很难将我的$_SESSION变量设置为“emma”。

我做错了什么因为我似乎无法理解它?

如果值得注意,此if语句包含在使用冒号if:而非括号endif的父{}语句中。父{H} if语句纯粹用于根据返回的列为空或用户名称来决定输出哪些功能。

4 个答案:

答案 0 :(得分:4)

您需要重新声明完整条件:

if ($_SESSION["user_name"] === "emma" or $_SESSION["user_name"] === "john"){
    //give extra functionality as well as basic functionality
} else {
   ...
}

更新说明:

if语句的伪代码语法是:

if ([some condition] [and/or] [another condition]) {
    // then do some stuff
}

每个[条件]语句都被评估为"truthy" or "falsey"。因此,您的原始问题可以重写为:

if ([$_SESSION["user_name"] is equal to "emma"] or ["john" is not false, 0, or null]) {
    // we will always get in here
}

由于“john”将永远是“truthy”(它不是false,null,0),它将始终通过条件。

答案 1 :(得分:3)

术语()作为表达式为append

"john"

将评估为true并且不会抛出任何语法错误。

你想要的是:

true

答案 2 :(得分:2)

将此分解为步骤可能会有所帮助。 PHP将评估左侧的所有内容或查看它是真还是假。然后它将评估或者右侧的内容。

事实证明, public void showMeMyDream(ActionEvent e) throws IOException { showSelectedDream(); contentID.getChildren().clear(); contentID.getChildren().add(FXMLLoader.load(getClass().getResource("/fxml/showDreamFXML.fxml"))); } 评估为<?xml version="1.0" encoding="UTF-8"?> <?import javafx.geometry.*?> <?import javafx.scene.text.*?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.layout.VBox?> <AnchorPane maxHeight="1.7976931348623157E308" nodeOrientation="LEFT_TO_RIGHT" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller.ShowDreamController"> <children> <VBox maxHeight="1.7976931348623157E308" nodeOrientation="LEFT_TO_RIGHT" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <children> <GridPane> <columnConstraints> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> </columnConstraints> <rowConstraints> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" vgrow="ALWAYS" /> </rowConstraints> <children> <Label text="Label" GridPane.rowIndex="5" /> <Label fx:id="showDreamContent" text="Label" wrapText="true" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="5" GridPane.vgrow="ALWAYS" /> <Label text="Label" GridPane.rowIndex="4" /> <Label fx:id="showDreamLength" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="4" /> <Label text="Label" GridPane.rowIndex="3" /> <Label fx:id="showDreamType" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="3" /> <Label fx:id="showDreamMem" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2" /> <Label fx:id="showDreamDate" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1" /> <Label text="Label" /> <Label text="Label" GridPane.rowIndex="1" /> <Label text="Label" GridPane.rowIndex="2" /> <Label fx:id="showDreamTitle" text="Label" GridPane.columnIndex="1" /> </children> </GridPane> <HBox> <children> <Button mnemonicParsing="false" text="Button" /> </children> </HBox> </children> </VBox> </children> </AnchorPane>

您正在寻找:

"john"

答案 3 :(得分:2)

尝试这样的事情

if (($_SESSION["user_name"] === "emma") || ($_SESSION["user_name"] === "john"))
{
   //do something
}
else
{
  //other thing
}
相关问题