zoukankan      html  css  js  c++  java
  • php杂记——1(基础知识与文件读写)

    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)."	type(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>
        <td align='right'>$distance</td>
    ";
        echo "    <td align='right'>".$distance / 10 ."</td>
    </tr>
    ";
        $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();

  • 相关阅读:
    使用mail架包发送邮件javax.mail.AuthenticationFailedException: failed to connect at javax.mail.Service.connec
    java容器 Map Set List
    COJ 1686:记忆化搜索
    POJ 3694:桥
    COJ 1685:贪心+set
    COJ 1687:Set
    COJ 1684:线段树
    POJ 3693:RMQ+后缀数组
    URAL 1297:后缀数组求最长回文串
    POJ 1743:后缀数组
  • 原文地址:https://www.cnblogs.com/webary/p/3854202.html
Copyright © 2011-2022 走看看