Home > Backend Development > PHP Tutorial > Teacher Shen Yi's PHP Devil Special Training Notes (2), Shen Yi Devil_PHP Tutorial

Teacher Shen Yi's PHP Devil Special Training Notes (2), Shen Yi Devil_PHP Tutorial

WBOY
Release: 2016-07-12 08:49:22
Original
1060 people have browsed it

Teacher Shen Yi’s special PHP training notes (2), Shen Yi’s devil

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

<span>1、file_put_contents</span>
Copy after login

 (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 />  ?>

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

 PHP 4, PHP 5, PHP 7

 getcwd — Get the current working directory

   <strong>说明</strong>
Copy after login
  string getcwd ( void )

Get the current working directory.

Copy after login
Copy after login
Copy after login

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 < even if the current directory is set. 🎜>FALSE. See chmod() for more information about modes and permissions.

<span>  1</span> <span>例如:在ubuntu终端
  </span><span>2</span> tiger@xz1024:~$ php -r "echo getcwd();"
  <span>3</span> /home/tigertiger@xz1024:~$ 
Copy after login

3、<span>substr</span>()
Copy after login
 (PHP 4, PHP 5, PHP 7)

Substr — Returns the substring of string

  说明

    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>function</span> 函数名(参数1,参数2,参数n)    <span>//</span><span>必须有关键字funciton</span>
<span>{
      函数体;        
}</span>
Copy after login

如果要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>#</span><span>!/usr/local/php/bin/php</span>
<?<span>php
  
 </span><span>require</span>('god_fun'.<span>substr</span>(<span>PHP_VERSION</span>,0,1<span>));  //判断PHP版本后引入不同的god_func

 </span><span>$result</span> =''<span>;
 </span><span>if</span>(<span>$argc</span> >=2<span> )
 {
   </span>'-v'==<span>$argv</span>[1] && <span>$result</span> = 'god version is 1.0 '<span>;
    </span>'init' == <span>$argv</span>[1] && <span>$result</span> =<span> genConfig();
 }

  </span><span>echo</span> <span>$result</span><span>; 
  </span><span>echo</span> <span>PHP_EOL</span><span>;
  
</span>?>
Copy after login

  2、god_func

<?<span>php
  </span><span>function</span><span> genConfig()
  {
    </span><span>return</span> <span>file_put_contents</span>(<span>getcwd</span>().'/god.json','{}').' of bytes is written.'.<span>PHP_EOL</span>.'god config is created'<span>;

  }
</span>?>
Copy after login

  3、god_func7

<span>1</span> <?<span>php
</span><span>2</span>   <span>function</span> genConfig():<span>string</span>
<span>3</span> <span>   {
</span><span>4</span>     <span>return</span> <span>file_put_contents</span>(<span>getcwd</span>().'/god.json','{}').' of bytes is written.'.<span>PHP_EOL</span>.'god config is created'<span>;
</span><span>5</span> 
<span>6</span> <span>   }
</span><span>7</span> ?>
Copy after login

 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1138632.htmlTechArticleTeacher Shen Yi’s PHP Devil Special Training Notes (2), Shen Yi Devil 1. How much will you learn in this lesson? A lazy function: 1. file_put_contents (PHP 5, PHP 7) file_put_contents writes a string...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template