Teacher Shen Yi's special PHP training notes (2)
Jul 06, 2016 pm 01:28 PM1. 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
$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>
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
, int $start
[, int $length
] ) 返回字符串 string
由 start
和 length
參數(shù)指定的子字符串。
參數(shù)
string
-
輸入字符串。必須至少有一個(gè)字符。
start
-
如果
start
是非負(fù)數(shù),返回的字符串將從string
的start
位置開始,從 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è)空字符串。如果提供了值為 0,
FALSE
或NULL
的length
,那么將返回一個(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> ?>
?

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
