/    Sign up×
Community /Pin to ProfileBookmark

Wrong output of function

Hi All,
Could you please tell me what mistake am I doing with the following function?

#####
function salutation($sex1)
{
if ($sex1=”female”) $out= “Ms. “;
else $out= “Mr. “;
return $out;
}
#####

This function is giving output “Ms. ” irrespective of the input. Am I doing any silly mistake?

Thanks in advance,
Regards,
Anirban.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@Phill_PaffordMar 17.2009 — [code=php]
function salutation($gender) {
// Set default
$prefix = "Mr. ";

// Check if female
if(strtolower($gender) == "female") {
// Reset
$prefix = "Ms. ";
}
return $prefix;
}
[/code]
Copy linkTweet thisAlerts:
@StevishMar 17.2009 — Yes... the silly mistake (one I've been guilty of several times) is that you're not using a comparison operator in your if statement. It should be if ($sex1=="female")... otherwise you are telling php to set $sex1 to equal "female", and then the if statement is seeing if $sex1 is true (which it is because it equals "female").

Phill's code is a lot cleaner though, and he uses strtolower() to make sure that "Female" or "FEMALE" or "FeMaLe" (etc) will also match.
Copy linkTweet thisAlerts:
@anix_anirbanauthorMar 17.2009 — @ Stevish: I was a fool to miss the second =.

@Phill: I am luck that I made the mistake to learn this new trick from you.

Thanks lads ?
×

Success!

Help @anix_anirban spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...