CopyPastehas never been so tasty!

Compare Strings and Numbers in php

by anonymous

  • 0
  • 0
  • 0
76 views

Orignal Source:http://codingvilla.com/compare-strings-numbers-php-single-article-592.aspx

 

Comparison Operators in php and their symbols:

 

Equal(==) 

Not equal ( !=)

Less than(<)

Greater than(>)

Less than or equal to(=<)

Greater than or equal to(>=)

 

 

Equal Operator (==)

 

Code:

 

<?php

 

// values of the strings are compare as dictionary order

$var1="za57321";

$var2="za57321";

if ($var1 == $var2) {

    echo "The string "."$var1". "is equal to  the string" ."$var2.";

} else {

   echo "The string "."$var1". "is not equal to  the string" ."$var2.";

}

 

  ?>

 

Output:

 

 

The string za57321is equal to the string za57321.

 

Explanation:

 

In the example above we compare two strings that have both alphabets and numbers. They are compared using dictionary order. Both strings have same order means the place of alphabets and numbers so both are equal.

 

Equal Operator (!=)

 

Code:

 

<?php

 

// values of the strings are compare as dictionary order

$var1="za54321";

$var2="za57321";

if ($var1 != $var2) {

    echo "The string "."$var1". "is not  equal to  the string" ."$var2.";

} else {

   echo "The string "."$var1". "is equal to  the string" ."$var2.";

}

echo "<br/>";

  ?>

 

Output:

 

The string za54321 is not equal to the string za57321.

 

Explanation:

 

In the example above we compare two strings that have both alphabets and numbers. They are compared using dictionary order. Both strings have different number of alphabets and numbers on different index so they are not equal. Output is The string za54321is not equal to the string za57321”.

 

Less than (<)

 

Code:

 

<?php

 

// values of the strings are compare as dictionary order

$var1="za648321";

$var2="za57321";

if ($var2 < $var1) {

    echo "The string "."$var2". "is less than  the string" ."$var1.";

} else {

   echo "The string "."$var2". "is not less than  the string" ."$var1.";

}

echo "<br/>";

  ?>

 

Output:

 

The string za57321 is less than the string za648321.

 

Explanation:

 

In the example above we compare two strings that have both alphabets and numbers. They are compared using dictionary order. Both strings have different number of alphabets and numbers on different index so the string has greater integer value on initial indexes is greater and the string has less value on initial indexes are smaller. For example $var1="za648321" has 6 after ‘z’,’a’and $var2 has 5 after ‘z’,’a’ in dictionary order ‘6’ is greater than’ 5’ so strings $var2 is less than string $var1.

 

 

Greater than (>)

 

Code:

 

 

<?php

 

// values of the strings are compare as dictionary order

$var1="zaa648321";

$var2="ya57321333";

if ($var1 >$var2 ) {

    echo "The string "."$var1". "is greater  than  the string" ."$var2.";

} else {

   echo "The string "."$var1". "is not greater than  the string" ."$var2.";

}

echo "<br/>";

  ?>

 

Output:

 

The string zaa648321 is greater than the string ya57321333.

 

Explanation:

 

In the example above we compare two strings that have both alphabets and numbers. They are compared using dictionary order. Both strings have different number of alphabets and numbers on different index so the string has greater integer or alphabet value on initial indexes is greater and the string has less value on initial indexes are smaller. For example $var1="zaa648321" has‘z’ on first index and $var2 has ‘y’ on first index in alphabets ‘z’ comes after‘y’ so the value that comes after in the dictionary have greater value.

So if the string in $var is greater than message will be printed” The string "."$var1". "is greater  than  the string" ."$var2."”.

 

Less than or equal to(<=)

 

Code:

 

<?php

// values of the strings are compare as dictionary order

$var1="a648321";

$var2="zaa57321333";

if ($var1 <=$var2 ) {

    echo "The string "."$var1". "is less than or equal the string.." ."$var2.";

} else {

   echo "The string "."$var1". "is not less than not equal the string.." ."$var2.";

}

echo "<br/>";

  ?>

 

Output:

 

The string a648321is less than or equal the string zaa57321333.

 

Explanation:

 

In the example above we compare two strings that have both alphabets and numbers. They are compared using dictionary order. Both strings have different number of alphabets and numbers on different index so the string that has greater integer or alphabet value on initial indexes is greater and the string that has less value on initial indexes are smaller. For example $var1="a648321" has ‘a’ on first index and $var2="zaa57321333" has ‘z’ on first index in alphabets z comes after a so the value that comes after in the dictionary is greater.

So if the string in $var is less message will be printed “The string a648321is less than or equal the string zaa57321333”.

 

Greater than or equal to (>=)

 

Code:

 

<?php

 

// values of the strings are compare as dictionary order

$var1="zaa648321";

$var2="ya57321333";

if ($var1 >=$var2 ) {

    echo "The string "."$var1". "is greater  than or equal the string.." ."$var2.";

} else {

   echo "The string "."$var1". "is not greater than not equal the string.." ."$var2.";

}

echo "<br/>";

  ?>

 

Output:

 

The string zaa648321is greater than or equal the string ya57321333.

 

Explanation:

 

In the example above we compare two strings that have both alphabets and numbers. They are compared using dictionary order. Both strings have different number of alphabets and numbers on different index so the string that has greater integer or alphabet value on initial indexes is greater and the string that has less value on initial indexes are smaller. For example $var1="zaa648321" has z on first index and $var2="ya57321333" has y on first index in alphabets z comes after y so the value that comes after in the dictionary have greater value.

So if the string in $var1 is greater than message will be printed “The string zaa648321 is greater than or equal the string..ya57321333.."$var2."”.

Add A Comment: