3

This may be simple and silly question but first I am learning php so your help would be appreciated.

I am trying to get variable with assigned conditional statement.

$gender = $curauth->gender; // getting from wordpress user profile.

if($gender === 'Male') {
    echo 'his';
} else {
    echo 'her';
}

So what I want to do is it will check if user is Male than in some description it will use his and if female it will use her. Something like below

echo 'jatin soni doesn't have set this option yet. His option will be deactivated soon.';

So here His will be set with above conditional code.

9
  • 2
    I'm not sure what your question is. Commented Aug 16, 2012 at 19:19
  • Is that inside a function? Otherwise I fail to see why you would use return. Commented Aug 16, 2012 at 19:20
  • Okay, I wan to set His or Her in echo '....here....' with variable. So if user set Male in his profile the variable will add HIS and if user set Female in profile the variable will add HER in echo. So i want one variable which I can add in between string to render His or Her Commented Aug 16, 2012 at 19:21
  • Oops sorry! that is my mistake. let me change the code. I was trying to make it works so forgot to remove return. Commented Aug 16, 2012 at 19:22
  • 1
    @PeeHaa return may be called from outside a function to stop the execution of the script. See php.net/manual/en/function.return.php Commented Aug 16, 2012 at 19:22

4 Answers 4

5

You can echo it straight out:

echo 'jatin soni doesn\'t have set this option yet. ', 
     ($gender === 'Male' ? 'His' : 'Her'), 
     ' option will be deactivated soon.';

If you need that more than once or for readability reasons, you should assign it to a variable:

# Default Female:
$gender = empty($curauth->gender) ? 'Female' : $curauth->gender;

$hisHer = $gender === 'Male' ? 'His' : 'Her';
echo 'jatin soni doesn\'t have set this option yet. ', 
     $hisHer, 
     ' option will be deactivated soon.';

Next step could be variable substitution in double-quoted stringsDocs or the use of the printfDocs function for formatted output.

Sign up to request clarification or add additional context in comments.

4 Comments

you should escape the ' in "doesn't".
Is it possible to add default state if user has not set the option?
@pixelngrain: I added you an example. It uses again the is ? then-a : else-b; ternary operator, this time for a default gender.
Yes I check and it works great only one thing is it is either give His or Her even in default state but not giving me like his/her means not set yet. But I found your code is very short and easy to use. So I am going to use it. Thanks a lot.. :)
4

How about this?

<?php
$pronoun = $curauth->gender == 'Male' ? 'his' : 'her';
echo "Jatin Soni doesn't have set this option yet. " .
     ucfirst($pronoun) . " option will be deactivated.\n"
?>

Comments

3

The most common way of doing things like this is to assign the dynamic part to a variable, and then use the variable in your output:

$gender = $curauth->gender; // getting from wordpress user profile.

if ($gender === 'Male') {
    $hisHer = 'His';
} else {
    $hisHer = 'Her';
}

echo "jatin soni doesn't have set this option yet. $hisHer option will be deactivated soon.";

2 Comments

I found your answer is promising just only trying few more things than select final one.. thanks a lot
I have voted your answer. Your code also was promising just hakra code was compact and easy to use. Thanks a lot
1

If you want a variable, you can do it in one line:

$gender = $curauth->gender; // getting from wordpress user profile.
$their = $gender == 'Male' ? $gender = 'His' : $gender = 'Her';
echo "$username doesn't have set this option yet. $their option will be deactivated soon.";

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.