PHP - Concatenate if statement?

前端 未结 4 893
暗喜
暗喜 2020-12-10 07:06

I want to concatenate in the middle of an echo to write an if statement, is this possible? Here is what I have.

echo \"
  • 相关标签:
    4条回答
    • 2020-12-10 07:44

      Like this, using the ternary operator:

      echo "<li class='". (($_GET["p"] == "home") ? "active" : "") . "'><a href='#'>Home</a>        </li>";  
      
      0 讨论(0)
    • 2020-12-10 07:46

      Do like this:

      echo "<li class='".($_GET["p"] == "home" ? 'active' : '') ."'><a href='#'>Home</a>        </li>";
      
      0 讨论(0)
    • 2020-12-10 07:47

      Instead of messy inline concatenations, might I suggest getting cozy with printf()?

      $format = '<li class="%s"><a href="#">Home</a>        </li>';
      printf($format, ($_GET['p'] == 'home') ? 'active' : '');
      
      0 讨论(0)
    • 2020-12-10 07:49
      echo "<li class='".(($_GET["p"] == "home") ? "active" : "")."'><a href='#'>Home</a>        </li>";
      
      0 讨论(0)
    提交回复
    热议问题