Hướng dẫn dùng comparing examples trong PHP

The substr_compare[] function compares two strings from a specified start position, up to a specified length of characters. The comparison is case-sensitive by default.

The following table summarizes the technical details of this function.

Return Value:

Returns a negative value [< 0] if main_str from start position is less than str; a positive value [> 0] if it is greater than str, and 0 if they are equal.

If start is set to a value greater than the length of main_str, or the length is set to a value less than 0, this function returns FALSE.

Changelog:Since PHP 7.3.5, the start may now be equal to the length of main_str.Version:PHP 5+

Syntax

The basic syntax of the substr_compare[] function is given with:

substr_compare[main_str, str, start, length, case_insensitivity];

The following example shows the substr_compare[] function in action.

Parameters

The substr_compare[] function accepts the following parameters.

ParameterDescriptionmain_strRequired. Specifies the main string being compared.strRequired. Specifies the secondary string being compared.startRequired. Specifies the start position for the comparison. If it is negative, counting will start from the end of the string.lengthOptional. Specifies the length of the comparison. The default value is the largest of the length of the str compared to the length of main_str minus the start.case_insensitivityOptional. If set to

";

// The main string is greater than the secondary string
echo substr_compare["Blackbird", "Black", 0]."
"; // The main string is less than the secondary string echo substr_compare["Black", "Blackbird", 0]; ?>
1, the comparison will be case-insensitive. Default value is
";

// The main string is greater than the secondary string
echo substr_compare["Blackbird", "Black", 0]."
"; // The main string is less than the secondary string echo substr_compare["Black", "Blackbird", 0]; ?>
2 which perform a case-sensitive comparison.

// Integers
echo 1  1; // 0
echo 1  2; // -1
echo 2  1; // 1

// Floats
echo 1.5  1.5; // 0
echo 1.5  2.5; // -1
echo 2.5  1.5; // 1

// Strings
echo "a"  "a"; // 0
echo "a"  "b"; // -1
echo "b"  "a"; // 1

echo "a"  "aa"; // -1
echo "zz"  "aa"; // 1

// Arrays
echo []  []; // 0
echo [1, 2, 3]  [1, 2, 3]; // 0
echo [1, 2, 3]  []; // 1
echo [1, 2, 3]  [1, 2, 1]; // 1
echo [1, 2, 3]  [1, 2, 4]; // -1

// Objects
$a = [object] ["a" => "b"];
$b = [object] ["a" => "b"];
echo $a  $b; // 0

$a = [object] ["a" => "b"];
$b = [object] ["a" => "c"];
echo $a  $b; // -1

$a = [object] ["a" => "c"];
$b = [object] ["a" => "b"];
echo $a  $b; // 1

// not only values are compared; keys must match
$a = [object] ["a" => "b"];
$b = [object] ["b" => "b"];
echo $a  $b; // 1

?>

When using the comparison operator [==], object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values [values are compared with ==], and are instances of the same class.

When using the identity operator [===], object variables are identical if and only if they refer to the same instance of the same class.

An example will clarify these rules.

Example #1 Example of object comparison

The above example will output:

Two instances of the same class
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : FALSE
o1 !== o2 : TRUE

Two references to the same instance
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : TRUE
o1 !== o2 : FALSE

Instances of two different classes
o1 == o2 : FALSE
o1 != o2 : TRUE
o1 === o2 : FALSE
o1 !== o2 : TRUE

Note:

Extensions can define own rules for their objects comparison [==].

jazfresh at hotmail.com ¶

15 years ago

Note that when comparing object attributes, the comparison is recursive [at least, it is with PHP 5.2]. That is, if $a->x contains an object then that will be compared with $b->x in the same manner. Be aware that this can lead to recursion errors:

Results in:
PHP Fatal error:  Nesting level too deep - recursive dependency? in test.php on line 11

Anonymous ¶

12 years ago

Comparison using operators should be documented.  Between two objects, at least in PHP5.3, the comparison operation stops and returns at the first unequal property found.

==0

==1

==2

rnealxp at yahoo dot com ¶

2 years ago

==3

rnealxp at yahoo dot com ¶

5 years ago

==4

==5

==6

==7

nhuhoai ¶

8 years ago

==8

==9

==0

wbcarts at juno dot com ¶

14 years ago

==1

==2

==3

==4

==5

rune at zedeler dot dk ¶

15 years ago

==6

==7

cross+php at distal dot com ¶

14 years ago

==8

==9

===0

rune at zedeler dot dk ¶

15 years ago

===1

===2

===3

===4

===5

===6

Hayley Watson ¶

13 years ago

===7

===8

===9

Chủ Đề