Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,939 questions

51,876 answers

573 users

How to compare two strings alphabetically (lexicographically) in PHP

4 Answers

0 votes
$s1 = "php";
$s2 = "javascript";

if (strcmp($s1, $s2) !== 0) 
    echo "$s1 is not equal to $s2";

 
/*
run: 

php is not equal to javascript

*/

 



answered Jun 2, 2017 by avibootz
0 votes
$s1 = "php";
$s2 = "PHP";

if (strcmp($s1, $s2) !== 0) 
    echo "$s1 is not equal to $s2";

 
/*
run: 

php is not equal to PHP

*/

 



answered Jun 2, 2017 by avibootz
0 votes
$s1 = "php";
$s2 = "PHP";

if (strcasecmp($s1, $s2) !== 0) // ignores case
    echo "$s1 is not equal to $s2";
else    
    echo "$s1 is equal to $s2";

 
/*
run: 

php is equal to PHP

*/

 



answered Jun 2, 2017 by avibootz
0 votes
// strcmp(string $string1, string $string2): int
// Returns:
// -1 (< 0) if string1 is less than string2
// 1 (> 1) if string1 is greater than string2
// 0 if they are equal
 
echo strcmp("aa", "ab") . "\n"; // <
echo strcmp("a", "b") . "\n"; // <
echo strcmp("a", "c") . "\n"; // <

echo strcmp("aa", "aa") . "\n"; // ==

echo strcmp("ba", "aa") . "\n"; // >
echo strcmp("ca", "aa") . "\n"; // >
echo strcmp("ab", "aa") . "\n"; // >

 
  
/*
run: 
 
-256
-1
-2
0
1
2
256

 
*/

 



answered Aug 22, 2024 by avibootz
edited Aug 22, 2024 by avibootz

Related questions

1 answer 113 views
1 answer 106 views
1 answer 99 views
1 answer 91 views
1 answer 90 views
1 answer 88 views
2 answers 93 views
...