PHP如果不能被3整除,则将变量加1,如果它可被3整除,则将其除以

时间:2017-03-16 20:56:10

标签: php if-statement math

<?php $i = 0; ?>
<?php
    $count_posts = wp_count_posts();

    $published_posts = $count_posts->publish;

    if ($published_posts % 3 == 0) {
        $row_number = '(int) ($published_posts / 3)';
    }
    else {
        $row_number = '(int) ($published_posts / 3 ) + 1';
    };
?>

我正在设置它,以便如果$ published_posts的数量可以被3整除,那么将变量$ row_number设置为$ published_posts除以3的结果。但是如果$ published_posts的数量不能均匀地分割为3,那么为它添加1并将$ row_number设置为结果。

1 个答案:

答案 0 :(得分:1)

也许这就是你想要的全部?

<?php
    $published_posts = rand(0, 100);
    print('Published posts = ' . $published_posts . "\n");
    $row_number = 0;

    if ($published_posts % 3 == 0) {
        print("divided by 3\n");
        $row_number = (int)($published_posts / 3);
    }
    else {
        print("added 1\n");
        $row_number = (int)($published_posts + 1);
    }
    print('row number = ' . $row_number);
?>