【问题标题】:How to echo a word based on previous echo word如何根据前一个回声词回声一个词
【发布时间】:2015-07-19 13:03:02
【问题描述】:

我有一个表单输入,如果脚本接收到单词提示(查看 if 语句),它会回显“这样做”这很好

现在我想用 if 语句根据前一个单词“do this”来回显另一个单词,但是 这不起作用

$at=(stripos($a, 'tip') === 0);
if ($at == true) {echo "do this";}
if (echoword == do this) {echo "do that";}

【问题讨论】:

  • 为什么不把两个 echo 语句都放在第一个 if 语句中?!
  • 只是试着解释你想做什么,而不是如何你想象你可能能够做到。
  • 更多专家在这里,但我认为如果$do="do this"; 变量预定义,然后if ($at == true) {echo $do;}if ($do == "do this") {echo "do that";}
  • 我想设置两个结果,首先是这样做,第二个是基于第一个结果,我不知道正确的字符串,所以我使用了 echoword,我想如果第一个 echo 是这样做然后第二个echo 应该这样做
  • 我相信我明白你想做什么,看看我下面的回答,是你想要的吗?还是你想以不同的方式来做?

标签: php if-statement echo


【解决方案1】:

你不能不依赖“以前的回声”,只依赖这样的条件:

$at=(stripos($a, 'tip') === 0);
if ($at == true) {
echo "do this";
echo "do that";
}

因为在这种情况下,您的“先前回声”仅取决于该条件

IF - 是一个基本的控制结构。 http://php.net/manual/en/control-structures.elseif.php

【讨论】:

    【解决方案2】:

    我相信你想这样做

    $at=(stripos($a, 'tip') === 0);
    if ($at == true) {
        $echoword="do this";
        echo $echoword;
    }
    if (isset($echoword) && $echoword == 'do this'){
         echo "do that";
    }
    

    【讨论】:

      【解决方案3】:

      你更喜欢哪一个?
      1.把你的代码存在if声明。

      $at=(stripos($a, 'tip') === 0);
      if ($at == true) {echo "do this"; echo "do that";}
      

      2。每当你想写相关的东西时使用$at

      $at=(stripos($a, 'tip') === 0);
      if ($at == true) {echo "do this";}
      if ($at) {echo "do that";}
      

      3。将回显的单词存储在变量中

      $at=(stripos($a, 'tip') === 0);
      $echoed = '';
      if ($at == true) {echo "do this"; $echoed = "do this";}
      if ($echode == "do this") {echo "do that";}
      

      【讨论】:

      • 谢谢,存储变量工作正常,我得到了两个结果
      • @prabhu 不客气。你也可以标记和指出答案;)