Reputation: 4579
I want to compare two string "numerically". I mean like 2C
is less than 11A
. I tried this and it's not working:
if("2A" < "11A"){
echo "First corect";
}
if(strcmp("2A", "11A") < 0){
echo "Last corect";
}
echo "Tests completed";
Upvotes: 2
Views: 149
Reputation: 1
Write a function that:
<Object>
where each object can be a String or Integer with the Integers being created from a contiguous string of digits between non-digits, the Strings being contiguous non-digits between any 2 digits.Nth elements
in the list are equal, go on to compare the N+1th elements
, otherwise return t/f
based on the integer to integer or string to string comparison.Upvotes: 0
Reputation: 455
if(intval(0x2A) < intval(0x11A)){
echo "First corect";
}
else
{
echo "Tests incompleted";
}
try this code
Upvotes: 0
Reputation: 437366
You are looking for strnatcmp
(or its case-insensitive sibling, strnatcasecmp
).
This will compare the numeric parts of your input as numbers (placing "2whatever" before "11whatever") and the textual parts as text (placing "2a" before "2b").
Upvotes: 7
Reputation: 2835
Try it like this:
if((int) '2A' < (int) '11A'){
echo "First correct";
}
You can also take a look at: http://php.net/manual/en/function.intval.php
Upvotes: 2