user8554313
user8554313

Reputation:

How to compare string and int as int?

$a['id'] = 124;
$b['id'] = '124';

if ($a['id'] == $b['id']) {}

In this case I want id`s are equals. How to properly compare string and int as int? Is there elegant way?

Upvotes: 0

Views: 117

Answers (1)

Himanshu Upadhyay
Himanshu Upadhyay

Reputation: 6565

Parse the string value to integer like this:

if($a['id'] == (int)$b['id']) {    
    // notice `(int)` which will make the '124' to int 124
}

Upvotes: 1

Related Questions