Multiply Function Results in PHP

2020-04-19 19:46发布

问题:

I'm still beginner in PHP. I have a small problem, I would to multiply the value get_formatted_order_total(); with 3.75 and what I did is

get_formatted_order_total(); * 3.75

but It didn't work.

here is the code

<li class="total">
    <?php _e('Total:', 'woocommerce'); ?>
    <strong><?php echo $order->get_formatted_order_total(); ?></strong>
</li>

Thank You

回答1:

Semi-colons go at the end of an instruction. Since this is all one instruction, you need to move the semi-colon to the end of the line:

echo get_formatted_order_total() * 3.75;

Example: http://codepad.org/HH3RLZCR



回答2:

Try so

echo get_formatted_order_total() * 3.75;

Without ; after (). ; should be after 3.75



回答3:

The problem is, that get_formatted_order_total() returns a string, formatted for display on the website, not a numerical value that you can use for calculation.

The unformatted value is available as a member variable of the Order class, so

echo $order->order_total * 3.75;

should work.

In order to understand this, look at the source code of Woocommerce: Order::get_formatted_order_total() simply formats Order::order_total with a helper function woocommerce_price():

/** Gets order total - formatted for display */
function get_formatted_order_total() {
    $formatted_total = woocommerce_price( $this->order_total );

    return apply_filters( 'woocommerce_get_formatted_order_total', $formatted_total, $this );
}


标签: php wordpress