PHP strncasecmp() Function
In this chapter you will learn:
- Definition for PHP strncasecmp() Function
- Syntax for PHP strncasecmp() Function
- Parameter for PHP strncasecmp() Function
- Return for PHP strncasecmp() Function
- Example - Compare two strings (case-insensitive)
- Example - Compare two strings (case-insensitive = Hello and hELLo will output the same)
Definition
The strncasecmp() function compares two strings case-insensitive.
Similar to the strcasecmp() function, strncasecmp() has the length parameter.
Syntax
PHP strncasecmp() Function has the following synax.
strncasecmp(string1,string2,length)
Parameter
Parameter | Is Required | Description |
---|---|---|
string1 | Required. | First string to compare |
string2 | Required. | Second string to compare |
length | Required. | Number of characters from each string to be used in the comparison |
Return
This function returns:
- 0 - if the two strings are equal
- <0 - if string1 is less than string2
- >0 - if string1 is greater than string2
Example 1
Compare two strings (case-insensitive):
<?php
echo strncasecmp("Hello world!","hello earth!",6);
?>
The code above generates the following result.
Example 2
Compare two strings (case-insensitive = Hello and hELLo will output the same):
<?php/* j ava 2s.c om*/
echo strncasecmp("Hello","Hello",6);
echo "<br>";
echo strncasecmp("Hello","hELLo",6);
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP strncmp() Function
- Syntax for PHP strncmp() Function
- Parameter for PHP strncmp() Function
- Return for PHP strncmp() Function
- Example - Compare two strings
- Example - Compare two string values for the first 6 characters
Home » PHP Tutorial » PHP String Functions