strcmp function

Support, Snippets & Projects

Moderators: Moderator, Global Moderator

Post Reply
Trinity
Hero Member
Hero Member
Posts: 808
Joined: Wed May 05, 2004 9:19 am

strcmp function

Post by Trinity »

strcmp is a function that some people may not know about; it is a binary safe string comparison:

[code]<?

   // Set up variables
   $password = "rand0m";
   $subpass = "rand0m";
   
   // Use function, will return true
   if (strcmp($password, $subpass) == 0) { #if the functions are the same, it will return 0
       echo "These strings are equal<br>";
   }
   else {
       echo "These strings are not equal<br>";
   }
   
   // Unequal
   $password = "sprand0m";
   
   if (strcmp($password, $subpass) == 0) { will not return 0
       echo "These strings are equal";
   }
   else {
       echo "These strings are not equal";
   }
?>[/code]

As you can see, the function could be useful in a simple password comparison. It can also detirmine if numbers are greater or smaller than each other.

For more info: www.php.net/strcmp
Post Reply

Return to “PHP &amp; MySQL”