0
<?php
  $typeface = $page->typeface();
    if($typeface == '') {
  } else {
      echo '<li><span>Typeface: </span>'.$typeface.'</li>';
  }
?>


I initially tried the PHP code below but it would still output the HTML:
<li><span>Typeface: </span></li> even if the string was empty.


<?php $typeface =  $page->typeface(); 
  if (!empty($typeface)): ?>
    <li><span>Typeface: </span><?php echo $typeface ?></li> 
  <?php endif; ?>


So my question is, how can I write the code at the very top in a shorter way?

Chris Burton
  • 1,195
  • 6
  • 19
  • 54

5 Answers5

3
$typeface = trim($page->typeface());
if($typeface != '')
    echo '<li><span>Typeface: </span>'.$typeface.'</li>';

EDIT: (removing new lines)

$typeface = trim(str_replace(array("\r", "\r\n", "\n"), '', $page->typeface()));
if($typeface != '')
    echo '<li><span>Typeface: </span>'.$typeface.'</li>';
mychalvlcek
  • 3,956
  • 1
  • 19
  • 34
2

Try this

(isset($typeface) && !empty($typeface))?echo "<li><span>Typeface: </span>".$typeface."</li>":"";
Bhuvan Rikka
  • 2,683
  • 1
  • 17
  • 27
0
($typeface=="")?echo "":echo '<li><span>Typeface: </span>'.$typeface.'</li>';
harry
  • 1,410
  • 3
  • 12
  • 31
0

Depending on the return type of $page->typeface() method, and the placement of the code (looks like you're on a template view), I might opt for:

<?php if ($typeface === $page->typeface()): ?>
    <li><span>Typeface: </span><?=$typeface?></li>
<?php endif; ?>

PHP docs here:

Alternative syntax for control structures

See here for cautions you should consider when using <?=...?> short tags.

An alternative to <?=...?> would be: <?php echo $typeface; ?>.

Hope that helps!


EDIT:

Actually, I second-guessed myself earlier (I blame sleep deprivation). I originally had this code (except for the trim()):

<?php if ($typeface = trim($page->typeface())): ?>
    <li><span>Typeface: </span><?=$typeface?></li>
<?php endif; ?>

Where I'm setting the variable $typeface in the if(). This technique should shorten your code by a line.

Here's a working example using simple strings:

<?php $foo = ""; ?>
...
<?php if ($typeface = trim($foo)): ?>
    <li><span>Typeface: </span><?=$typeface?></li>
<?php endif; ?>

... outputs: Nothing.

<?php $foo = 'I\'m a string!'; ?>
...
<?php if ($typeface = trim($foo)): ?>
    <li><span>Typeface: </span><?=$typeface?></li>
<?php endif; ?>

... outputs: <li><span>Typeface: </span>I'm a string!</li> (obviously, missing the opening and closing <ul></ul>.

Community
  • 1
  • 1
mhulse
  • 4,062
  • 6
  • 28
  • 35
  • @ChristopherBurton When I first posted the code, I had `$typeface = $page->typeface()` (copy/paste error). I changed it to `$typeface === $page->typeface()` (note the `===`). Hopefully that's why it's not working for you? – mhulse Dec 08 '12 at 09:29
  • I still get the same result. – Chris Burton Dec 08 '12 at 09:31
  • Gosh, I'd hate to waste your time... You might trim, like the others are saying: ``? When I test using these vars: ``, where `$foo` is my version of `$page->typeface()`, it works for me... What does $page->typeface() expected return type/value? I assume it's an empty/non-empty string return? Again, I don't mean to waste your time (looks like you're getting a ton of great answers already). :) – mhulse Dec 08 '12 at 09:41
  • @ChristopherBurton Even though you've got your answer, I've updated my answer... My original thinking stands (see my **EDIT** above). I **sometimes** like assigning vars in an `if()` check... Makes for on less line of code to juggle. If that does not work, then you might need to apply a more hardcore trim like the one used in the [solution above](http://stackoverflow.com/a/13776080/922323). Happy coding! – mhulse Dec 08 '12 at 11:15