-2

Hi everyone below is my PHP code. I have basically made an algorithm so when a name is inputted in the $string section, that name is searched and depending on the first letter in the name it will be placed within a group, either group 1 or 2.

I run into a problem as whenever i search for a name beginning with P it outputs group 2 when it should be group 1. any idea why this is? i need it to output group 1 with names a-p and group 2 for names after p.

$string = "Peter"; //Input surname here!

$firstCharacter = substr($string, 0, 1); //Finds the first letter in the String
$firstCharacter = strtoupper($string); //Converts string into uppercase

echo ($string > 'P') ? 'Group1' : 'Group2';
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • 4
    Try `>=` for greater-than-or-equal. I'd make this an answer, but really, this is basic comparison. – aynber Jul 10 '17 at 15:48
  • This might be useful too... https://stackoverflow.com/questions/34571330/php-ternary-operator-vs-null-coalescing-operator – Difster Jul 10 '17 at 15:49
  • also more of a code-style thing but helps code-readability, if you keep the inline if statement all inside brackets, it's easier to see where it begins and where it ends – treyBake Jul 10 '17 at 15:49

1 Answers1

3

'P' is not greater than 'P', it is equal to 'P', thus your condition is marked false for 'P'. You can either change your condition to >=, or condition on the letter after 'P', 'Q'.

yanman1234
  • 1,009
  • 9
  • 27