Broad Network


Comparing Strings in PHP

PHP String with Security Considerations - Part 3

Foreword: In this part of the series, I explain how to compare strings in PHP.

By: Chrysanthus Date Published: 29 Oct 2018

Introduction

This is part 3 of my series, PHP String with Security Considerations. In this part of the series, I explain how to compare strings in PHP. You should have read the previous parts of the series before reaching here, as this is the continuation.

The == Operator and Strings
If two strings have the same characters in the same order, then the two strings are equal to one another. The casings (upper and lower) of letters also have to be the same. Try the following code:

<?php

    $myIdent = 'xyz abc';
    $hisIdent = 'xyz abc';
    if ($myIdent == $hisIdent)
        {
            echo 'The strings of the two variables are equal.';
        }

?>

The output is:

    The strings of the two variables are equal.

The != Operator and Strings
This operator is the opposite of the previous. Try the following program:

<?php

    $myIdent = 'xyz abc';
    $hisIdent = 'xyzabc';
    if ($myIdent != $hisIdent)
        {
            echo 'The strings of the two variables are not equal.';
        }

?>

In this case the strings are not equal because one has the space character and the other does not.

Note: same characters with same casing and same placements will always result in equal strings. If any of these three parameters is changed, the strings become unequal.

Relational Operators
<, >, <= and >= are relational operators.

> is the Greater Than Operator. It compares two strings alphabetically, the way they are placed in the dictionary. A string that would be typed last in the dictionary, is the greater string. That is, “bbb” is greater than “aaa”. Try the following program that illustrates this:

<?php

    $ident1 = "aaaa ccccccc";
    $ident2 = "d";
    if ($ident2 > $ident1)
        {
            echo 'The value of $ident2 is greater than the value of $ident1.';
        }

?>

The output is:

    The value of $ident2 is greater than the value of $ident1.

The if-block is executed, because "d" is greater than "aaaa ccccccc".

In the PHP alphabetic series, uppercase letters come before lowercase letters. That is, the series is A, B, C, …Z, a, b, c …z. Try the following code that illustrates this:

<?php

    $ident1 = "QQQQ";
    $ident2 = "eeee";
    if ($ident2 > $ident1)
        {
            echo 'The value of $ident2 is greater than the value of $ident1.';
        }

?>

The if-block is executed.

When numbers are considered as characters (in a string), they fit alphabetically in front of ‘A’. Here, the PHP numeric-alpha series is, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, …a, b, c,…. So “BB” is greater than “33”. Try the following program that illustrates this:

<?php

    $ident1 = "22aa";
    $ident2 = "aa22";
    if ($ident2 > $ident1)
        {
            echo 'The value of $ident2 is greater than the value of $ident1.';
        }

?>

The if-block is executed.

>= returns true if the left argument is the same in content (characters and order) as the right argument. It also returns true if the left argument is greater than the right argument, just like in the above code samples. Try the following program, where the two arguments are the same:

<?php

    $ident1 = "e e e";
    $ident2 = "e e e";
    if ($ident2 >= $ident1)
        {
            echo 'The value of $ident2 is greater than or equal to the value of $ident1.';
        }

?>

The output is:

    The value of $ident2 is greater than or equal to the value of $ident1.

The other relational operators are similarly explained.

The Spaceship Operator, <=>
This operator returns -1 if the left argument is stringwise less than the right argument. It returns 0 if the two arguments are stringwise equal. It returns 1 if the left argument is stringwise greater than the right argument. Try the following code:

<?php

    echo 'alphabetical' <=> 'order', '<br>';
    echo 'bcde' <=> 'bcde', '<br>';
    echo 'man' <=> 'MAN', '<br>';

?>

The output is:

-1
0
1

To use this in a condition, remember that -1 and 1 are the same as true, while 0 is the same as false.

That is it for this part of the series. We stop here and continue in the next part.

Chrys


Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

BACK NEXT

Comments