[英]PHP. by using “global $var” to get a global variable, do we get a copy of the global var or get a reference to that global variable?
[英]Simply trying to multiple a variable in PHP. ($Var * 0.75)
因此,我是一個學習HTML,CSS,JavaScript的初學者,現在只是從PHP開始,因為它是解決我的問題的唯一語言。
我在我的網站上添加了一個點擊計數器,每次獲得新訪問者時,該計數器都會將log.txt文件更新+1,並在頁面上打印/回顯它。
我想讓PHP對訪問者數量進行一些數學運算。 我希望可以打印兩件事。
無論我嘗試什么,我總是會得到公式的打印而不是答案。 我假設這是因為我正在嘗試對一個字符串進行多重處理,但似乎無法修復它。
我嘗試使用:-settype將字符串更改為int。
<?php
// Add correct path to your countlog.txt file.
$path = 'hit_counter_log.txt';
// Opens hit_counter_log.txt to read the number of hits.
$file = fopen( $path, 'r' );
$count = fgets( $file, 1000 );
$money = 0.75;
fclose( $file );
// Update the count.
$count = abs( intval( $count ) ) + 1;
// Output the updated count.
echo "{$count} Views\n";
echo "{$count + $money} Saved";
// Opens hit_counter_log.txt to change new hit number.
$file = fopen( $path, 'w' );
fwrite( $file, $count );
fclose( $file );
假設我的訪客帳戶為100,我將收到打印輸出:100 + 0.75
我要打印:$ 7,500
通過將變量放在方括號內,它僅打印這些變量的值。 您不能對括號內的變量執行類似的操作。
echo "$" . $count + $money . " Saved";
...將打印出“已保存$ 100.75”。 至於如何打印出所需的“ 7,500美元”,我將由數學負責。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.