亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 PHP 將 WordPress 短代碼輸出的 2 個值精確相加

如何使用 PHP 將 WordPress 短代碼輸出的 2 個值精確相加

PHP
GCT1015 2023-09-08 21:30:21
對于個人項目,我有兩種重力形式。我正在使用這個插件,它允許我添加帶有表單參數的簡碼,該簡碼將在特定字段中輸入的每個表單條目中的所有值相加,并給出總計https://wordpress.org/plugins/gravitywp-計數/所以我有 2 個數字(每個表格一個)我現在想做的是使用 PHP 將它們添加在一起。<?php    $first_number = do_shortcode("[gravitywp_count formid='1' number_field='3']");    $second_number = do_shortcode("[gravitywp_count formid='2' number_field='3']");    $sum_total = $first_number + $second_number;    $direct_text = 'The two variables added together = ';    print ($direct_text . $sum_total);?>formid='1' 的當前值是 10,969.39,formid='2' 的值是 4,582.92,所以總數應該是 15,552.31,但我的代碼只給我答案 14。所以我猜它只是將 10 加起來4 是第一個逗號之前的千位數字。有人可以建議嗎?
查看完整描述

1 回答

?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

這可以通過PHP str_replace() 函數來實現

您可以從短代碼獲得的結果中刪除逗號并將該值存儲在新變量中。

最后把代碼寫成這樣:

$first_number = do_shortcode("[gravitywp_count formid='1' number_field='3']");

$second_number = do_shortcode("[gravitywp_count formid='2' number_field='3']");

$first_number_sep = str_replace( ',', '', $first_number );

$second_number_sep = str_replace( ',', '', $second_number );


if( is_numeric( $first_number_sep &&? $second_number_sep) ) {

? ? $first_number = $first_number_sep;

? ? $second_number = $second_number_sep;

}


$sum_total = $first_number_sep + $second_number_sep;

$sum_total = $first_number_sep + $second_number_sep;

$total_formatted = number_format($sum_total, 2, '.', ','); //formats the number back to the comma state


echo 'The two variables added together = ' . $total_formatted;


查看完整回答
反對 回復 2023-09-08
  • 1 回答
  • 0 關注
  • 109 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號