Ismail
Ismail

Reputation: 743

need PHP function to echo a value when another is empty

I have 2 inputs, one for username, and one for 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.

Upvotes: 0

Views: 200

Answers (2)

Raja
Raja

Reputation: 608

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>

Upvotes: 2

Kuldeep Dangi
Kuldeep Dangi

Reputation: 4422

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') );
}

Upvotes: 2

Related Questions