简体   繁体   中英

Correct PHP Syntax with IF and Echo

I was hoping I could get some help from you guys. The code below works but I doubt that this is a good way to write the syntax. Especially since it will require an enormous amount of code.

The thing I'm trying to do is create a list which will be populated only if an item has a value. I have already specified if the variable has a value or not (I think that part of the code could use some work as well, but that's a different question). And I've found a way to show or hide the item in the list. But I don't think it's efficient. And that's where I could use some help.

            <?php
                if(empty($orderNum))
                    {
                    echo "<li><a href='#' title='Brodit'>Tomte</a></li>";
                    }
                else
                    {
                    echo "<li><a href='unordered.php?SupName=2&SupStatus=3' title='Brodit'>Order ("; echo "$orderNum"; echo ")</a></li>";
                    }
            ?>

You can use php alternative syntax widely used in php templates and suggested in docs like zend manual as well as symfony:

<?php if(empty($orderNum)): ?>
    <li><a href="#" title="Brodit">Tomte</a></li>
<?php else: ?>
    <li><a href="unordered.php?SupName=2&SupStatus=3"title="Brodit">
      Order (<?php echo $orderNum; ?>)</a>
    </li>
<?php endif; ?>
<?php
  if(empty($orderNum)){
    echo "<li><a href='#' title='Brodit'>Tomte</a></li>";
  } else {
    echo "<li><a href='unordered.php?SupName=2&SupStatus=3' title='Brodit'>Order ({$orderNum})</a>  </li>";
  }
?>

You can use "foo {$var} bar" instead of "foo ".$var." bar" . This is (in my opinion) more elegant.

If the HTML Code gets longer I would prefer:

<?php if(empty($orderNum)): ?>
    <li><a href="#" title="Brodit">Tomte</a></li>
<?php else: ?>
    <li><a href="unordered.php?SupName=2&SupStatus=3"title="Brodit">
      Order (<?php echo $orderNum; ?>)</a>
    </li>
<?php endif; ?>

To add another alternative to what was already answered, you could focus on the HTML rather than the PHP:

 <?php if(empty($orderNum)) { ?>
     <li><a href='#' title='Brodit'>Tomte</a></li>
 <?php } else {?>
     <li><a href='unordered.php?SupName=2&SupStatus=3' title='Brodit'>Order ("<?php echo $orderNum;?>")</a></li>";
 <?php }?>

This especially useful if you want to take advantage of your HTML editor (syntax validation, auto-completion, ...). I would choose this syntax for big chunks of HTML and when working with integrators, because it is less likely that they will screw up the PHP part.

 <?php
            if(empty($orderNum))
                {
                echo "<li><a href='#' title='Brodit'>Tomte</a></li>";
                }
            else
                {
                echo "<li><a href='unordered.php?SupName=2&SupStatus=3' title='Brodit'>Order (". $orderNum .")</a></li>";
                }
        ?>

One thing you could do is assign the string to a variable and then have one echo statement at the end. Also you can do variable substitution in the double quotes. For example:

"Order ($orderNum)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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