PHP -如果字符串中的最后一个数字等于9,做点什么


PHP - if last number in string is equal to 9 do something

如果我在php中有一个字符串,我需要做一些事情,如果最后一个数字等于9,我该怎么做?例如

$cost = '23829';
TRUE

   $cost = '2382';
   FALSE

   $cost = '9';
   TRUE

 $cost = '9999998';
   FALSE

基本上,如果最后一个数字是原始数字的9倍乘以2,我尝试了以下方法,但它们不起作用。

 $cost = strrev($cost);
   if ($cost[0] == 9) $cost = strrev($cost) * 2;

if (substr($cost, -1) === '9') .......

它们没有按预期工作

尝试:

if (substr($cost, -1) == 9) .......

或:

if ( (int) substr($cost, -1) === 9) .......