创建一个基于两个数字的喜欢/不喜欢栏-PHP


Create a like/dislike bar based on two numbers - PHP

我有两个数字想尝试创建一个喜欢/不喜欢的栏。下面是我的PHP和HTML。HTML使用一个宽度值来显示不喜欢栏中"喜欢"的数量。见下文:

<div class="dislike_base">
    <div class="like" style="width: 52%"></div>
</div>

我只是不知道如何对这两个数字进行数学比较来得到百分比。

PHP:

$like_post_num = 13;
$hate_post_num = 10;
$total = $like_post_num + $hate_post_num
//how do I compare the above information to get the percent of 100 of likes vs dislikes.

请告诉我,如果这没有意义?

$total = $like_post_num + $hate_post_num;
$percent = round(($like_post_num / $total) * 100);
<?php
$like_post_num = 13;
$hate_post_num = 10;
$sum = $like_post_num + $hate_post_num;
$like_percent = round($like_post_num / $sum * 100);
$hate_percent = round($hate_post_num / $sum * 100);
?>
<div class="dislike_base">
    <div class="like" style="width: <?php echo $like_percent; ?>"px></div>
    <div class="dislike" style="width: <?php echo $hate_percent; ?>"px></div>
</div>