標籤:style blog http color 使用 strong
1.變數前面需要加貨幣符號"$",常量則不需要:
define(‘PRICE‘,100);echo PRICE;
2.用一個變數的值作為另一個變數的名稱可以得到類似C中的指標變數:
1 $varname = ‘tireqty‘;2 $$varname = 5;3 //等價於下面這條語句4 $tireqty = 5;
View Code
3.number_format()函數可用來格式化一個浮點數的輸出精度:
$pi = 3.1415926536;echo number_format($pi,2).‘<br/>‘;
4."@"是錯誤控制運算子,當將其放置在一個 PHP 運算式之前,該運算式可能產生的任何錯誤資訊都被忽略掉。可配合$php_errormsg一起使用(但是僅當配置了track_errors特性為true時有效,此時運算式所產生的任何錯誤資訊都被存放在變數 $php_errormsg 中,此變數在每次出錯時都會被覆蓋)。gettype()函數可以得到一個變數的類型。
1 <?php 2 $a = 23; 3 $b = 0; 4 $c = @($a/$b); 5 if($c==‘‘) 6 echo ‘<font color="#ff0000"><strong>error: ‘.$php_errormsg.‘</strong></font>‘; 7 else 8 echo number_format($c,3); 9 echo ‘<br>type(b): ‘.gettype($b)."\ttype(b): ".gettype($c).‘<br>‘;10 ?>
5.條件陳述式:if...elseif ...else...
6.通過迭代實現重複動作:
<html ><body><table border="1" cellpadding="3"><tr> <td bgcolor="#cccccc" align="center">Distance</td> <td bgcolor="#cccccc" align="center">Cost</td></tr><?php $distance = 50; while($distance<=250){ echo "<tr>\n <td align=‘right‘>$distance</td>\n"; echo " <td align=‘right‘>".$distance / 10 ."</td>\n</tr>\n"; $distance+=50; }?></table><body></html>View Code
7.如果希望結束整個PHP指令碼的運行,可以使用exit語句,從而不執行剩餘的指令碼。
8.讀寫檔案相關函數:
開啟關閉檔案:fopen(), fclose();
檔案結束標誌函數:feof()
寫檔案:fwrite(), file_put_contents() (該函數不需要開啟關閉檔案);
每次讀取一行結果fgets(), fgetss(), fgetcsv();
讀取整個檔案:readfile(), fpassthru(), file(), file_get_contents() (該函數不需要開啟關閉檔案);
讀取一個字元:fgetc();
讀取任意長度:fread();
查看檔案是否存在:file_exists();
確定檔案大小:file_size();
刪除一個檔案:unlink();
在檔案中定位:rewind(), fseek(), ftell();