Jump to content

explanation regarding strcmp


robert_gsfame

Recommended Posts

Hi robert_gsfame,

 

strcmp compares two strings, and returns less than (<0), greater than (>0) or identical (0).

 

So:

 

<?php
echo strcmp("Hello world","Hello world");
?> 

 

Would return:

 

0.

 

<?php
echo strcmp("1","2");
?> 

 

Would return:

 

<0.

 

<?php 

$string1 = "abc"; 
$string2 = "def"; 
$string3 = "XYZ"; 

if (strcmp($sstring1, $sstring2) < 0) { 
   print "$sstring1 is smaller than $string2<br>"; 
} else { 
   print "$string2 is smaller than $string1<br>"; 
} 

if (strcmp($string1, $string3) < 0) { 
   print "$string1 is smaller than $string3<br>"; 
} else { 
   print "$string3 is smaller than $string1<br>"; 
} 

?> 

 

Would return:

 

abc is smaller than def

XYZ is smaller than abc

 

Your code example is using the incorrect syntax, so would not work.  The PHP manual on strcmp (http://us.php.net/manual/en/function.strcmp.php) also has some good code snippets at the bottom of the page which should help explain this function further.

 

I hope this helps.

Link to comment
Share on other sites

Hi robert_gsfame,

 

Sorry, I misread the location of the ! in your original post!

 

Anyway, the default value for:

 

if (strcmp($sstring1, $sstring2)

 

all depends on what $sstring1 and $sstring2 are.  There is no "default" value as such, strcmp takes two strings and compares them.  It all depends on what the strings contain as to what value will be returned.

 

Hope this helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.