国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Description
Parameters
Return value
Home php教程 php手冊(cè) Teacher Shen Yi's special PHP training notes (2)

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

Jul 06, 2016 pm 01:28 PM

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() //獲取當(dāng)前工作目錄

 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 參數(shù)指定的子字符串。

  參數(shù)

    string

  輸入字符串。必須至少有一個(gè)字符。

    start

  如果 start 是非負(fù)數(shù),返回的字符串將從 stringstart 位置開始,從 0 開始計(jì)算。例如,在字符串 “abcdef” 中,在位置 0 的字符是 “a”,位置 2 的字符串是 “c” 等等。

  如果 start 是負(fù)數(shù),返回的字符串將從 string 結(jié)尾處向前數(shù)第 start 個(gè)字符開始。

  如果 string 的長(zhǎng)度小于 start,將返回 FALSE。 ? ?

??

  Example #1 使用負(fù)數(shù) start

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

  length

  如果提供了正數(shù)的 length,返回的字符串將從 start 處開始最多包括 length 個(gè)字符(取決于 string 的長(zhǎng)度)。

  如果提供了負(fù)數(shù)的 length,那么 string 末尾處的許多字符將會(huì)被漏掉(若 start 是負(fù)數(shù)則從字符串尾部算起)。如果 start 不在這段文本中,那么將返回一個(gè)空字符串。

  如果提供了值為 0FALSENULLlength,那么將返回一個(gè)空字符串。

  如果沒有提供 length,返回的子字符串將從 start 位置開始直到字符串結(jié)尾。

  Example #2 使用負(fù)數(shù) 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 />  ?>

?

二、定義個(gè)自定義函數(shù)

PHP定義函數(shù)

<span style="color: #0000ff;">function</span> 函數(shù)名(參數(shù)1,參數(shù)2,參數(shù)n)    <span style="color: #008000;">//</span><span style="color: #008000;">必須有關(guān)鍵字funciton</span>
<span style="color: #000000;">{
      函數(shù)體;        
}</span>

如果要return就ruturn.忘記return返回值,也無所謂。如果函數(shù)有返回值,那必須返回。

三、PHP7特性:

PHP7允許在函數(shù)中增加返回值。比如string、int、array、object等

function 函數(shù)名(): string //注意冒號(hào)

{

}

四、課程代碼:

 第一課我們建立了GOD這個(gè)文件,這一課,我們建立GOD_FUNC文件,通過reuqire在god文件中引入函數(shù)文件god_func。

 同時(shí),我們?yōu)榱藢W(xué)習(xí)PHP7新特性,專門建立god_func7這個(gè)文件,并在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 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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)