【问题标题】:How to set a variable value based on conditions set by other variables如何根据其他变量设置的条件设置变量值
【发布时间】:2015-11-11 23:40:24
【问题描述】:

所以我是 PHP 新手,目前只是在做一个小项目作为练习,我已经设法写下了几行代码而没有摔倒……但我有点卡在这里。

基本上,我的脚本当前所做的是检查我设置的三个不同的变量(每个都是真/假选项)并区分是否只选择了一个真选项(在 3 个选项中,只有一个可以为真,其他 2 个必须是假的)。如果只有 1 个值设置为 true,则其余代码运行;如果多个值设置为 true,或者没有值设置为 true,则会向用户显示错误提示。

一旦检查完成,我想然后设置 $name 的值,例如,基于链接到相关变量的记录是真的......这是我想出的,但它没有似乎工作......

if ($value1 == "true") {$name = $result1;}

else if ($value2 == "true") {$name = $result2;}

else if ($value3 == "true") {$name = $result3;}

否则退出 (0)

所以我基本上想通过识别 3 个值变量中的哪一个为真来设置 $name 变量,然后使用在 $result 中检索到的相关变量设置 $name

任何帮助将不胜感激。在任何人开始之前...我知道我可能听起来有点生气...但我们都必须从某个地方开始!!

谢谢

【问题讨论】:

  • 您需要记住的一件事是,如果您想检查boleantrue/false 或stringtrue/false。因为现在您正在检查变量是否等于字符串:true.

标签: php


【解决方案1】:

加个开关会更好看:

switch(true){
   case $value1:
      $name = $result1;
      break;
   case $value2:
      $name = $result2;
      break;
   case $value3:
      $name = $result3;
      break;
   default:
      exit();
}

如果您需要确保只有一个陈述是正确的,请在使用此之前验证:

//In case you need to make there is only a single true statement
$found = false;
for($i=1; $i<4; $i++) {
   $value = "value".$i;
   if($$value) {
      if($found) {
          exit("More than one 'true' statement");
      }
      $found = true;
   }
}

【讨论】:

  • 问题是虽然我不想将“$name”设置为常量“即'name 3'”,但我需要将 $name 设置为另一个变量从页面中检索,因此需要: $name = $result $result 例如可以是 1-1000 之间的任何值,这一切都取决于在 $value 中输入的值
  • 这也可以,只需将 'name1' 更改为 $result1 等。
  • 不会 $result1 虽然仍然是一个定义的变量,但我需要通过交叉引用 $value 中定义的值来获取 $result 的值
  • 另请注意,如果您的值为true, false, true,则此代码将运行。所以这不满足条件如果多个值设置为true
【解决方案2】:

Dracony 的答案看起来确实不错,但是当多个值设置为 true 时会失败。为了获得更大的灵活性,您应该考虑将值映射到数组中,并使用 flag 变量跟踪状态(true 的值的数量)。找到一个满足以下所有条件的完整注释示例。此外,此代码适用于任意长度的数组(您可以通过在 $values$results 中简单地添加更多值来添加条件)。

// store the data in arrays for easier manipulation
$values = array($value1, $value2, $value3);
$results = array($result1, $result2, $result3);

// set a flag to check the state of the condition inside the loop
// it keeps track of the index for which the value is true
$flag = -1;
$name = NULL;

// use for and not foreach so we can easily track index
for ($i = 0; $i < count($values); $i++) {
  // if no index has been found 'true' yet, assign the current result.
  if ($values[$i] === true) {
    if ($flag === -1) {
      $flag = $i;
      $name = $results[$i];
    }
    // if a 'true' value has been found for another index
    // reset the name var & stop the loop
    if ($flag > -1 && $flag !== $i) {
      $name = NULL;
      break;
    }
  }   
}

if ($name) {
  // run code when only 1 value is true
} else {
  exit();
}

【讨论】:

  • 这样做真的没有意义。您需要做的就是按照优先级顺序对 switch 中的语句进行排序。如果还需要验证只有一个 true 语句最好在 switch 语句之前验证它
  • @Dracony 你是对的,这可能不是最简单的方法,但它确实有效,并且具有灵活性的优势,因为你不需要添加 @987654326 @case 如果您想添加更多潜在价值。您修改后的答案比我的更有效,但您可以通过在循环中将 $resultx 分配给 $found 来完全避免 switch 语句
猜你喜欢
  • 1970-01-01
  • 2021-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 2012-09-01
相关资源
最近更新 更多