Home >php教程 >php手册 >Teacher Shen Yi's special PHP training notes (2)

Teacher Shen Yi's special PHP training notes (2)

WBOY
WBOYOriginal
2016-07-06 13:28:171462browse

1. In this lesson, you will learn several lazy functions:

<span style="font-size: 14px;">1、file_put_contents</span>

 (PHP 5, PHP 7)

 file_put_contents — Write a string to a file

Description

 int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )

 It has the same function as calling fopen(), fwrite() and fclose() in sequence.

If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.

Parameters

    filename

The name of the file to which data will be written.

    data

Data to be written. The type can be string, array or stream resource (as mentioned above).

If data is specified as a stream resource, the cached data saved in the stream will be written to the specified file. This usage is similar to using the stream_copy_to_stream() function.

 The parameter data can be an array (but not a multi-dimensional array), which is equivalent to file_put_contents($filename, join('', $array)).

    flags
The value of

  flags can be a combination of the following flags using the OR (|) operator.

For example (from PHP.net)

  <?php<br />  $file = 'people.txt';<br />  // Open the file to get existing content<br />  $current = file_get_contents($file);<br />  // Append a new person to the file<br />  $current .= "John Smithn";<br />  // Write the contents back to the file<br />  file_put_contents($file, $current);<br />  ?>


2、getcwd() //获取当前工作目录

 PHP 4, PHP 5, PHP 7

 getcwd — Get the current working directory

   <strong>说明</strong>
  string getcwd ( void )

Get the current working directory.


Return value

If successful, the current working directory will be returned. If failed, FALSE will be returned.

 Under some Unix variants, if any parent directory does not have readable or search mode set, getcwd() will still return FALSE. See chmod() for more information about modes and permissions.

<span style="color: #008080;">  1</span> <span style="color: #000000;">例如:在ubuntu终端
  </span><span style="color: #008080;">2</span> tiger@xz1024:~$ php -r "echo getcwd();"
  <span style="color: #008080;">3</span> /home/tigertiger@xz1024:~$ 

 

3、<span style="color: #008080;">substr</span>()

    (PHP 4, PHP 5, PHP 7)

    substr — 返回字符串的子串

  说明

    string substr ( string $string , int $start [, int $length ] )

    返回字符串 stringstartlength 参数指定的子字符串。

  参数

    string

  输入字符串。必须至少有一个字符。

    start

  如果 start 是非负数,返回的字符串将从 stringstart 位置开始,从 0 开始计算。例如,在字符串 “abcdef” 中,在位置 0 的字符是 “a”,位置 2 的字符串是 “c” 等等。

  如果 start 是负数,返回的字符串将从 string 结尾处向前数第 start 个字符开始。

  如果 string 的长度小于 start,将返回 FALSE。    

  

  Example #1 使用负数 start

  <?php<br />  $rest = substr("abcdef", -1); // 返回 "f"
  $rest = substr("abcdef", -2); // 返回 "ef"
  $rest = substr("abcdef", -3, 1); // 返回 "d"<br />  ?>

  length

  如果提供了正数的 length,返回的字符串将从 start 处开始最多包括 length 个字符(取决于 string 的长度)。

  如果提供了负数的 length,那么 string 末尾处的许多字符将会被漏掉(若 start 是负数则从字符串尾部算起)。如果 start 不在这段文本中,那么将返回一个空字符串。

  如果提供了值为 0FALSENULLlength,那么将返回一个空字符串。

  如果没有提供 length,返回的子字符串将从 start 位置开始直到字符串结尾。

  Example #2 使用负数 length

  <?php<br />  $rest = substr("abcdef", 0, -1); // 返回 "abcde"
  $rest = substr("abcdef", 2, -1); // 返回 "cde"
  $rest = substr("abcdef", 4, -4); // 返回 ""
  $rest = substr("abcdef", -3, -1); // 返回 "de"<br />  ?>

 

二、定义个自定义函数

PHP定义函数

<span style="color: #0000ff;">function</span> 函数名(参数1,参数2,参数n)    <span style="color: #008000;">//</span><span style="color: #008000;">必须有关键字funciton</span>
<span style="color: #000000;">{
      函数体;        
}</span>

如果要return就ruturn.忘记return返回值,也无所谓。如果函数有返回值,那必须返回。

三、PHP7特性:

PHP7允许在函数中增加返回值。比如string、int、array、object等

function 函数名(): string //注意冒号

{

}

四、课程代码:

 第一课我们建立了GOD这个文件,这一课,我们建立GOD_FUNC文件,通过reuqire在god文件中引入函数文件god_func。

 同时,我们为了学习PHP7新特性,专门建立god_func7这个文件,并在god文件中判断引入。

  1、god

<span style="color: #008000;">#</span><span style="color: #008000;">!/usr/local/php/bin/php</span>
<?<span style="color: #000000;">php
  
 </span><span style="color: #0000ff;">require</span>('god_fun'.<span style="color: #008080;">substr</span>(<span style="color: #ff00ff;">PHP_VERSION</span>,0,1<span style="color: #000000;">));  //判断PHP版本后引入不同的god_func

 </span><span style="color: #800080;">$result</span> =''<span style="color: #000000;">;
 </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$argc</span> >=2<span style="color: #000000;"> )
 {
   </span>'-v'==<span style="color: #800080;">$argv</span>[1] && <span style="color: #800080;">$result</span> = 'god version is 1.0 '<span style="color: #000000;">;
    </span>'init' == <span style="color: #800080;">$argv</span>[1] && <span style="color: #800080;">$result</span> =<span style="color: #000000;"> genConfig();
 }

  </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$result</span><span style="color: #000000;">; 
  </span><span style="color: #0000ff;">echo</span> <span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;
  
</span>?>

  2、god_func

<?<span style="color: #000000;">php
  </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> genConfig()
  {
    </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">file_put_contents</span>(<span style="color: #008080;">getcwd</span>().'/god.json','{}').' of bytes is written.'.<span style="color: #ff00ff;">PHP_EOL</span>.'god config is created'<span style="color: #000000;">;

  }
</span>?>

  3、god_func7

<span style="color: #008080;">1</span> <?<span style="color: #000000;">php
</span><span style="color: #008080;">2</span>   <span style="color: #0000ff;">function</span> genConfig():<span style="color: #0000ff;">string</span>
<span style="color: #008080;">3</span> <span style="color: #000000;">   {
</span><span style="color: #008080;">4</span>     <span style="color: #0000ff;">return</span> <span style="color: #008080;">file_put_contents</span>(<span style="color: #008080;">getcwd</span>().'/god.json','{}').' of bytes is written.'.<span style="color: #ff00ff;">PHP_EOL</span>.'god config is created'<span style="color: #000000;">;
</span><span style="color: #008080;">5</span> 
<span style="color: #008080;">6</span> <span style="color: #000000;">   }
</span><span style="color: #008080;">7</span> ?>

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn