如何转换喜欢在php中计数

Assuming I have the code below in PHP

<?php 
$thousands_likes = 1100;
$millions_likes = 2100000;

?>

how do I convert it to something like

1.1k likes or 2.1M likes as case may be.

Something simple based on just millions and thousands:

if($likes >= 1000000) {
    $result = ($likes / 1000000) . 'M';
} elseif($likes >= 1000) {
    $result = ($likes / 1000) . 'K';
}

Maybe try creating a utility function for this kind of task? You can use plain math to calculate the suffix:

function human_readable_likes($likes): string
{
    $i = \floor(\log($likes, 1000));
    return ound($likes / 1000 ** $i, [0, 1, 2, 2, 2][$i]).' '.['', 'K', 'M', 'G', 'T'][$i];
}

but for your use case this might be too much.