-2

I am trying to attach a string on php variable this is my code

if ($tipy = "2"){
        $username = "dr_" + $username;
    }

and output to be dr_James

But this doesnt work gives me result as 0.

testedwho
  • 73
  • 6

1 Answers1

0

You should use a dot.

if ($tipy = "2"){
        $username = "dr_" . $username;
    }

See php documentation https://www.php.net/manual/de/language.operators.string.php

JrWebDev
  • 54
  • 6
  • Quick answer, but not very thorough. What about the if condition? – KIKO Software Apr 29 '22 at 11:23
  • Hi Kiko, if you check for a number than you should not check for a string. Instead use a number if ($tipy == 2){ $username = "dr_" . $username; } – JrWebDev Apr 29 '22 at 11:24
  • No, I mean it should probably be `==` (is equal) instead of `=` (assignment). – KIKO Software Apr 29 '22 at 11:25
  • The if condition is bad : if ($tipy == "2") -> (double = or triple for exact match) – svgta Apr 29 '22 at 11:25
  • @JrWebDev this worked but you dont need "==" one is enough its also in php. – testedwho Apr 29 '22 at 11:28
  • @testedwho If at least three people here doubt it should be `=` you should consider it could maybe be `==`. Yes, the assignment operator works, but it is highly unusual. – KIKO Software Apr 29 '22 at 11:31
  • In this case i have only 2 values 1 or 2 so i just check if tipy is 2 or not – testedwho Apr 29 '22 at 11:43
  • @testedwho: Yes, I understand that that is what you intend to do, but instead you're now assigning the value 2 to tipy. This means your condition is always true, regardless of what the value of tipy is. – KIKO Software Apr 29 '22 at 12:11