0

I have the following if statement:

<?php if ( isset ( $prfx_stored_meta['wedding-user'] ) ) selected( $prfx_stored_meta['wedding-user'][0], 'select-one' ); ?>

which I need to insert into this echo statement:

$users = get_users();
$i = 0;
// Array of WP_User objects.
foreach ( $users as $user ) {
    echo "<option value='select-$i' >" . esc_html( $user->display_name ) . "</option>";
    $i++;
}

Just before closing the <option> tag
I'm not sure how to add an if statement inside of an echo

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Sam Skirrow
  • 3,647
  • 15
  • 54
  • 101

3 Answers3

2

Don't.

Ignoring for a moment that outputting your HTML embedded inside echo statements like this is an anti-pattern (consider using a templating engine!), you will make your code really hard to read by mixing your logic and your content even more, which you will be doing if you employ the conditional operator* here.

In fact, in general, if you have anything more than basic string interpolation in your output statements, you're not separating your logic and content enough.

Here, it will look something like this:

$users = get_users();
$i = 0;
// Array of WP_User objects.
foreach ( $users as $user ) {
   echo "<option value='select-$i' ";
   if (isset($prfx_stored_meta['wedding-user']))
      echo selected($prfx_stored_meta['wedding-user'][0], 'select-one');
   echo ">" . esc_html( $user->display_name ) . "</option>";

   $i++;
}

See how messy it is? And with a long line using the conditional operator it would be even worse!

* Not the "ternary operator"! Being "ternary" is its arity, not its name.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Just updated my code, the echo statement is in an array, should make more sense now – Sam Skirrow Nov 29 '15 at 18:40
  • @SamSkirrow: Better, but we still don't know what `selected(...)` is supposed to do. – Lightness Races in Orbit Nov 29 '15 at 18:41
  • Ah yes I see what you mean. selected() is a function within wordpress http://codex.wordpress.org/Function_Reference/selected - allows wordpress to remember the selection – Sam Skirrow Nov 29 '15 at 18:49
  • Again, I see what you mean, it is messy - but is there a way to neaten it up? – Sam Skirrow Nov 29 '15 at 18:56
  • @SamSkirrow: Yeah, use a templating engine. Though Wordpress may have overly constrained you here. Why are you using Wordpress? – Lightness Races in Orbit Nov 29 '15 at 18:57
  • *"Being "ternary" is its arity, not its name."* - I'm afraid that ain't true in PHP, at least if you consider the PHP manual to be an authoritative source on the names of things in PHP; according to http://php.net/manual/en/language.operators.comparison.php it *is* called the Ternary Operator. – Mark Amery Nov 12 '18 at 13:58
  • The *best* part of it is that those same docs explain that the binary `?:` operator introduced in PHP 5.3 (which takes two operands and evaluates to the first one if it's truthy and the second one otherwise) is *also* a form of the "Ternary Operator". So officially PHP has an operator called the "Ternary Operator" which is not ternary. Somehow I suspect that this observation will not make you happy. – Mark Amery Nov 12 '18 at 14:06
1

You have to use ternary operator : www.php.net/ternary

Try this :

echo "<option value='select-$i' >" . esc_html( $user->display_name ) . ((isset($prfx_stored_meta['wedding-user'])?selected( $prfx_stored_meta['wedding-user'][0], 'select-one'):'')."</option>";

Source : if block inside echo statement?

Community
  • 1
  • 1
Pranay Bhardwaj
  • 1,059
  • 8
  • 9
0

You will want to use the a ternary operator which acts as a shortened IF/Else statement.

Kinshuk Lahiri
  • 1,468
  • 10
  • 23