0

I'm fairly new to PHP so forgive me if this function is badly done.

I have a function:

function socialLink($sm_type = NULL) {
   if ($sm_type = 'twitter') {
     echo 'https://www.twitter.com';
   } else {
     echo 'https://www.facebook.com';
   }
}

In my code when I call the function socialLink('facebook'); it echo's the Twitter URL.

Surely it should echo the Facebook URL since $sm_type would be equal to 'facebook' not twitter ?

Any help would be appreciated.

4
  • your test is a classic error : should be $sm_type == 'twitter'. As written, you assigne a value to $sm_type, always true. Commented Jul 21, 2017 at 11:31
  • Make sure that you read proper documentation about the Comparison Operators (in any programming language): php.net/manual/en/language.operators.comparison.php Commented Jul 21, 2017 at 11:32
  • Thank you for the link. I could definitely do with revising more of the PHP Manual. Commented Jul 21, 2017 at 11:36
  • Possible duplicate of The 3 different equals Commented Jul 21, 2017 at 11:52

4 Answers 4

5

Set your if condition with this,

function socialLink($sm_type = NULL) {
   if ($sm_type == 'twitter') {
     echo 'https://www.twitter.com';
   } else {
     echo 'https://www.facebook.com';
   }
}

See this.

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

2 Comments

It was something so simple all along. Can't believe I couldn't see that. Thank you.
Happy to help dude. Be careful for small mistakes. Please accept the answer if you got your output.. Cheerss..!!
3
function socialLink($sm_type = NULL) {

   if ($sm_type == 'twitter') {
     echo 'https://www.twitter.com';
   } else {
     echo 'https://www.facebook.com';
   }
}

NOTE: Single = use to assign the value and = = use to compare values

Different's Between = , = = , = = =

  • = operator Used to just assign the value.
  • = = operator Used to just compares the values not datatype
  • = = = operator Used to Compare the values as well as datatype.

Comments

2

Your if statement does not use a comparison operator, it is an assignment (=). For a comparison, please use "==".

if ($sm_type == 'twitter') {
     echo 'https://www.twitter.com';
} else {
    echo 'https://www.facebook.com';
}

1 Comment

It was something so simple all along. Can't believe I couldn't see that. Thank you.
2
if ($sm_type == 'twitter') {
 echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}

In php == is use for string comparison so, In this case you can't used = for that, simple :)

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.