lisovaccaro
lisovaccaro

Reputation: 33966

php compare alphabet position?

I have two variables such as:

var1 =  "z";
var2 = "A";

how can I check if var1 is after in the alphabet than var2 (in this case it should return true)?

Upvotes: 0

Views: 2680

Answers (6)

Chris Ostmo
Chris Ostmo

Reputation: 1252

I think everyone who has answered agrees that strcmp() is the right answer, but every answer provided so far will give you incorrect results. Example:

echo strcmp( "Z", "a" );

Result: -1

echo strcmp( "z", "A" );

Result: 1

strcmp() is comparing the binary (ord) position of each character, not the position in the alphabet, as you desire. If you want the correct results (and I assume that you do), you need to convert your strings to the same case before making the comparison. For example:

if( strcmp( strtolower( $str1 ), strtolower( $str2 ) ) < 0 )
{
  echo "String 1 comes before string 2";
}

Edit: you can also use strcasecmp(), but I tend to avoid that because it exhibits behavior that I've not taken the time to understand on multi-byte strings. If you always use an all-Latin character set, it's probably fine.

Upvotes: 3

Luchian Grigore
Luchian Grigore

Reputation: 258618

return strcmp($var1,$var2) > 0?

Upvotes: 1

user244343
user244343

Reputation:

If you're comparing a single character, you can use ord(string). Note that uppercase values compare as less than lowercase values, so convert the char to lowercase before doing the comparison.

function earlierInAlphabet($char1, $char2)
{
    $char1 = strtolower($char1);
    $char2 = strtolower($char2);

    if(ord($char1) < ord($char2))
        return true;
    else
        return false;
}

function laterInAlphabet($char1, $char2)
{
    $char1 = strtolower($char1);
    $char2 = strtolower($char2);

    if(ord($char1) > ord($char2))
        return true;
    else
        return false;
}

If you're comparing a string (or even a character) then you can also use strcasecmp(str1, str2):

if(strcasecmp($str1, $str2) > 0)
  // str1 is later in the alphabet

Upvotes: 1

Devon Bernard
Devon Bernard

Reputation: 2300

This solution may be over-the-top for just two variables; but this is for ever if you need to solve if a bunch of variables (2+) would be in the right order...

   <?php
    $var1='Z';
    $var2='a';
    $array1 = array();
    $array[] = $var1;
    $array1[] = $var2;

    $array2 = sort($array1);
    if($array2 === $array1){
    return true;
    }else{
    return false;
    }
    ?>

Other than that if you only want to do it with two variables this should work just fine.

    <?php
        return (strcmp($var1,$var2) > 0);
    ?>

Upvotes: 0

Aleksandar Janković
Aleksandar Janković

Reputation: 811

You should use http://php.net/manual/en/function.strcmp.php

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

So

return strcmp(var1, var2) > 0  // will return true if var1 is after var2

Upvotes: 0

Philip Whitehouse
Philip Whitehouse

Reputation: 4157

What did you try?... pretty sure this works

<?php
if(strcmp($var1,$var2) > 0) {
   return true;
}

Upvotes: 1

Related Questions