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.

if (strcmp($sstring1, $sstring2)

what is the default value for this??

 

less than 0,0 or greater than 0

 

anyway i use if(!(strcmp(case1,case2)){ echo "SELECTED"

in order to retrieve the value from the database into my list menu

 

and for sure it works

Can you explain this to me

 

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.