• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

PHP Function:strcmp

December 19, 2023

PHP Function: strcmp

In the world of web development, PHP is a widely used scripting language that allows developers to create dynamic and interactive websites. One of the essential functions in PHP is strcmp, which stands for “string compare.” This function is used to compare two strings and determine their relative order.

How strcmp Works

The strcmp function takes two string parameters and returns an integer value based on the comparison result. Here’s the syntax:

int strcmp ( string $str1 , string $str2 )

The function compares $str1 and $str2 and returns:

  • A negative integer if $str1 is less than $str2
  • Zero if $str1 is equal to $str2
  • A positive integer if $str1 is greater than $str2

The comparison is performed based on the ASCII value of each character in the strings. The function starts comparing the characters from the leftmost position and continues until a difference is found or the end of either string is reached.

Examples

Let’s look at some examples to understand how strcmp works:

$str1 = "apple";
$str2 = "banana";
$result = strcmp($str1, $str2);
echo $result; // Output: -1

$str3 = "apple";
$str4 = "apple";
$result = strcmp($str3, $str4);
echo $result; // Output: 0

$str5 = "banana";
$str6 = "apple";
$result = strcmp($str5, $str6);
echo $result; // Output: 1

In the first example, $str1 is “apple” and $str2 is “banana.” Since “apple” comes before “banana” in alphabetical order, the result is -1.

In the second example, both $str3 and $str4 are “apple.” As they are identical, the result is 0.

In the third example, $str5 is “banana” and $str6 is “apple.” Since “banana” comes after “apple” in alphabetical order, the result is 1.

Conclusion

The strcmp function in PHP is a powerful tool for comparing strings. It allows developers to determine the relative order of two strings based on their ASCII values. By understanding how strcmp works, developers can effectively use this function to perform various string comparisons in their PHP applications.

Summary

In summary, the strcmp function in PHP is used to compare two strings and determine their relative order. It returns a negative integer if the first string is less than the second, zero if they are equal, and a positive integer if the first string is greater than the second. This function is a valuable tool for string comparisons in PHP development.

For more information on VPS hosting solutions, consider checking out Server.HK. They offer top-notch VPS hosting services with reliable performance and excellent customer support.

Recent Posts

  • CentOS Stream Explained: Key Differences from CentOS Linux
  • How to Configure FirewallD in CentOS Stream: From Essential to Production-Grade
  • Installing Docker on CentOS: A Practical Setup Guide (CentOS Stream 9/10 – 2026)
  • How to Secure a CentOS Server: 15 Essential Hardening Techniques (CentOS Stream 9/10 – 2026)
  • CentOS End of Life (EOL): What It Means and Migration Options in 2026

Recent Comments

No comments to show.

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot