php雜記——1(基礎知識與檔案讀寫)

來源:互聯網
上載者:User

標籤: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();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.