简体   繁体   English

PHP“if”里面的变量echo

[英]PHP “if” inside of variable echo

I am creating a navigation bar that automatically changes the class to "active" if the page is currently active (using php if statements [using the current URL to match]) 我正在创建一个导航栏,如果页面当前处于活动状态,则会自动将类更改为“活动”(使用php if语句[使用当前URL匹配])

I also want to be able to change the header depending on if a user is logged in or not...now i usually would have no issue with this, however, because there are if statements inside of the variable, I do not know how to proceed. 我还希望能够根据用户是否登录来更改标题...现在我通常不会对此有任何问题,因为变量内部有if语句,我不知道如何继续。

My Problem is, it's impossible to do if statements inside of variable estabilsihing...for example this is what I'm attempting to do, however it's not working...is there a way of doing this, and actually making it work... thank you in advanced! 我的问题是,如果在变量建立中发表声明是不可能的......例如,这是我正在尝试做的事情,但它不起作用......有没有办法做到这一点,并且实际上使它工作。 ..先谢谢你!

MY CODE 我的代码

---THE PHP--- --- PHP ---

In the head: 在头部:

<?php
///// (GETS THE PARTS OF THE CURRENT URL)
error_reporting(0);
$directoryURIbody = $_SERVER['REQUEST_URI'];
$pathbody = parse_url($directoryURIbody, PHP_URL_PATH);
$componentsbody = explode('/', $pathbody);
$first_partsbody = $componentsbody[1];
$second_partsbody = $componentsbody[2];
$third_partsbody = $componentsbody[3];
$fourth_partsbody = $componentsbody[4];
$fifth_partsbody = $componentsbody[5];
?>

In Body: 身体:

:

<?php echo $navbar; ?>

 <?php echo $navbar; ?> 

You can use ternary operator and use string concatenation instead of echoing: 您可以使用三元运算符并使用字符串连接而不是回显:

<?php

if (!isset($_SESSION['idx'])) { ///////////IF NOT LOGGED IN
  if (!isset($_COOKIE['idCookie'])) {//////IF NOT LOGGED IN
  $navbar = '
    <li class="'. ($first_partmainnav=="" ? "active" : "noactive")  .'"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>

?>

And the rest by analogy 其余的类比

There is nothing wrong with your variables inside the li tags, the problem is with the way you mix HTML and PHP code and tags inside single quotes. li标签中的变量没有任何问题,问题在于将HTML和PHP代码和标签混合在单引号中。 Nothing will work unless you correct that. 除非你纠正,否则什么都不会奏效。 Here is the way to do it correctly, using your own code: 以下是使用您自己的代码正确执行此操作的方法:

<?php
if ( !isset( $_SESSION[ 'idx' ] ) ) { ///////////IF NOT LOGGED IN
  if ( !isset( $_COOKIE[ 'idCookie' ] ) ) { //////IF NOT LOGGED IN
    ?>

  <li class="<?php if ( $first_partmainnav == "" ) { echo "active";   } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>
  <li class="<?php if ( $first_partmainnav == "tutorials" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>tutorials">Tutorials</a></li>

  <li class="<?php if ( $first_partmainnav == "resources" ) { echo "active"; } else { echo "noactive";  }?>"><a href="<?php echo $dyn_wwwFULL; ?>resources">Resources</a></li>
  <li class="<?php if ( $first_partmainnav == "library" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>library">Library</a></li>

  <li class="<?php if ( $first_partmainnav == "our-projects" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>our-projects">Our Projects</a></li>
  <li class="<?php if ( $first_partmainnav == "community" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>community">Community</a></li>

  <?php }
  if ( isset( $_SESSION[ 'idx' ] ) ) { ////////////IF LOGGED IN (WITHOUT COOKIES)
  ?>

  <li class="<?php if ( $first_partmainnav == "" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>
  <li class="<?php if ( $first_partmainnav == "whatever" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>whatever">whatever</a></li>

  <li class="<?php if ( $first_partmainnav == "justanother" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>justanother">Just Another</a></li>

  <?php }  else {
    if ( isset( $_COOKIE[ 'idCookie' ] ) ) { //IF LOGGED IN (WITH COOKIES)
  ?>

    <li class="<?php if ( $first_partmainnav == "" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>
    <li class="<?php if ( $first_partmainnav == "whatever" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>whatever">whatever</a></li>
    <li class="<?php if ( $first_partmainnav == "justanother" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>justanother">Just Another</a></li>
   <?php 
    }
  }
}
?>

While personally I would change the whole method of doing this (have an array that corresponds with your list and echo accordingly), with your method it can be done by separating the variable from the code, for example: 虽然我个人会改变这样做的整个方法(有一个与你的列表相对应的数组并相应地回显),你的方法可以通过将变量与代码分开来完成,例如:

$array['tutorials'] = ($first_partmainnav == "tutorials") ? 'active' : 'noactive';
$array['resources'] = ($first_partmainnav == "resources") ? 'active' : 'noactive';
// etc...

Then concatenate it with your output. 然后将它与您的输出连接起来。

In general you should use string concatenation: 通常,您应该使用字符串连接:

Wrong: 错误:

echo '<a href="<?php echo $dyn_wwwFULL; ?>tutorials ...';

Right: 对:

echo '<a href="'.$dyn_wwwFULL.'tutorials ...';

I think you are looking for something like this: 我想你正在寻找这样的东西:

$first_partmainnav = 'tutorials';
echo 'some text ' . ($first_partmainnav == 'tutorials' ? 'active' : '') . ' some more text';

It is a while back that I coded in PHP but to be it looks like you are 我用PHP编写了一段时间,但它看起来像你

How would the following piece of code work? 以下代码如何工作?

<?php

if (!isset($_SESSION['idx'])) { ///////////IF NOT LOGGED IN
if (!isset($_COOKIE['idCookie'])) {//////IF NOT LOGGED IN
$navbar = '
<li class="' if($first_partmainnav=="") {echo "active"; } else  {echo "noactive";} '"><a href="' echo $dyn_wwwFULL; '">Home</a></li>'
....
?>

Trust you can fill in the rest yourself. 相信你可以自己填写其余部分。

When I looked at syntax highlighting of the first line of < li> lines you notice that there is something odd happening. 当我查看第一行<li>行的语法高亮显示时,您会注意到发生了奇怪的事情。

The real problem is you cannot have PHP tags inside quoted PHP code. 真正的问题是你不能在引用的PHP代码中包含PHP标签。 Let's check the last line of the first li block, in the question: 让我们在问题中检查第一个li块的最后一行:

    <?php
    //NOTE: Had to add a space to separate php tags in comments for proper code highlighting, like this: < ? php --- ? >

    $navbar = '
            <li class="<?php if ($first_partmainnav=="community") {echo "active"; } else  {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>community">Community</a></li>'; ?>

    <?php
    /* Although the whole $navbar value is quoted (Single quotes), there is php code surrounded by php tags in 2 places: 
    < ? php if ($first_partmainnav=="community") {echo "active"; } else  {echo "noactive";} ? > Here
    < ? php echo $dyn_wwwFULL; ? > And here
    Same happens with the rest of the lines, so it is impossible for this code to work.
    */
    ?>

The selected answer is also wrong. 选择的答案也是错误的。 Let's see: 让我们来看看:

    <?php
      $navbar = '<li class="'. ($first_partmainnav=="" ? "active" : "noactive")  .'"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>
      '; // Add the single quote an semicolon missing.

    /* Same problem: $navbar value is quoted but there is php code surrounded by php tags:
    <a href="< ? php echo $dyn_wwwFULL; ? > Here.                                       
    This code can't work either.
    */
    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM