0

I have 2 inputs, one for username, and one for ID.

  • Username value: <?php echo esc_attr( get_option('username') ); ?>

  • ID value: <?php echo esc_attr( get_option('id') ); ?>

and a division:

<div id="info"><?php echo esc_attr( get_option('username') ); ?></div>

I want a PHP function to echo <?php echo esc_attr( get_option('ID') ); ?> in div#info ONLY IF the username field is not filled by the user.

Thank you.

P.S: Apologize if similar topics already exist, but I have looked'em up and couldn't find a straight answer for such a beginner.

6
  • if(empty....){...} else{...} or use a ternary operator which would work best. Commented Apr 5, 2015 at 2:30
  • Thanks! apply that to my code ;) Commented Apr 5, 2015 at 2:31
  • 4
    This isn't meant to sound rude but have you applied it to your code? @Fred-ii- provided everything you need to answer your question. However it's not his job or anyone else's to write your code for you. Commented Apr 5, 2015 at 2:35
  • You've got the best of both worlds below. Try 'em out ;-) Commented Apr 5, 2015 at 2:46
  • 3
    You're welcome, cheers - Ternary's one of the best thing since sliced bread ;-) Commented Apr 5, 2015 at 2:53

2 Answers 2

2

you dont need a function a simple if else will do your job

if(empty(get_option('username')) {
    echo '<div id="info">'. esc_attr( get_option('username') ) . '</div>';
} else {
echo esc_attr( get_option('username') );
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, yiiframe !
2

Can go for ternary

<div id="info"><?php echo (!empty(esc_attr( get_option('username') ))) ? esc_attr( get_option('username') ) : esc_attr( get_option('id') ); ?></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.