Using the string functions

Functions that compare two strings

strcmp($str1, $str2)
strcasecmp($str1, $str2)
strnatcmp($str1, $str2)
strnatcasecmp($str1, $str2)

How a case-sensitive comparison works

<?php
  $result = strcmp('Anders', 'Zylka');     // $result is -1
  $result = strcmp('anders', 'Zylka');     // $result is 1
  $result = strcasecmp('Anders', 'zylka'); // $result is -25
?>

How a "natural" number comparison works

<?php
  $names = implode('|', $names); // $names is 'Mike|Anne|Joel|Ray'
?>

How to compare two strings

<?php
  $result = strnatcasecmp($name_1, $name_2);
  if ($result < 0) {
    echo $name_1 . ' before ' . $name_2;
  } else if ($result == 0) {
    echo $name_1 . ' matches ' . $name_2;
  } else {
    echo $name_1 . ' after ' . $name_2;
  }
?>

Back