标签:
一、这一课会学习到几个懒人函数:
1、file_put_contents
(PHP 5, PHP 7)
file_put_contents — 将一个字符串写入文件
$filename , mixed $data [, int $flags = 0 [, resource $context ]] )和依次调用 fopen(),fwrite() 以及 fclose() 功能一样。
If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.
filename要被写入数据的文件名。
data要写入的数据。类型可以是 string,array 或者是 stream 资源(如上面所说的那样)。
如果 data 指定为 stream 资源,这里 stream 中所保存的缓存数据将被写入到指定文件中,这种用法就相似于使用 stream_copy_to_stream() 函数。
参数 data 可以是数组(但不能为多维数组),这就相当于 file_put_contents($filename, join(‘‘, $array))。
flags flags 的值可以是 以下 flag 使用 OR (|) 运算符进行的组合。
<?php $file = ‘people.txt‘;
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file file_put_contents($file, $current);
?>
2、getcwd() //获取当前工作目录
PHP 4, PHP 5, PHP 7
getcwd — 取得当前工作目录
说明
取得当前工作目录。
成功则返回当前工作目录,失败返回 FALSE。
在某些 Unix 的变种下,如果任何父目录没有设定可读或搜索模式,即使当前目录设定了,getcwd() 还是会返回 FALSE。有关模式与权限的更多信息见 chmod()。
1 例如:在ubuntu终端 2 tiger@xz1024:~$ php -r "echo getcwd();" 3 /home/tigertiger@xz1024:~$
3、substr()
(PHP 4, PHP 5, PHP 7)
substr — 返回字符串的子串
$string , int $start [, int $length ] ) 返回字符串 string 由 start 和 length 参数指定的子字符串。
string输入字符串。必须至少有一个字符。
start 如果 start 是非负数,返回的字符串将从 string 的 start 位置开始,从 0 开始计算。例如,在字符串 “abcdef” 中,在位置 0 的字符是 “a”,位置 2 的字符串是 “c” 等等。
如果 start 是负数,返回的字符串将从 string 结尾处向前数第 start 个字符开始。
如果 string 的长度小于 start,将返回 FALSE。
Example #1 使用负数 start
<?php
$rest = substr("abcdef", -1); // 返回 "f"
$rest = substr("abcdef", -2); // 返回 "ef"
$rest = substr("abcdef", -3, 1); // 返回 "d"
?>
length
如果提供了正数的 length,返回的字符串将从 start 处开始最多包括 length 个字符(取决于 string 的长度)。
如果提供了负数的 length,那么 string 末尾处的许多字符将会被漏掉(若 start 是负数则从字符串尾部算起)。如果 start 不在这段文本中,那么将返回一个空字符串。
如果提供了值为 0,FALSE 或 NULL 的 length,那么将返回一个空字符串。
如果没有提供 length,返回的子字符串将从 start 位置开始直到字符串结尾。
Example #2 使用负数 length
<?php
$rest = substr("abcdef", 0, -1); // 返回 "abcde"
$rest = substr("abcdef", 2, -1); // 返回 "cde"
$rest = substr("abcdef", 4, -4); // 返回 ""
$rest = substr("abcdef", -3, -1); // 返回 "de"
?>
二、定义个自定义函数
PHP定义函数
function 函数名(参数1,参数2,参数n) //必须有关键字funciton { 函数体; }
如果要return就ruturn.忘记return返回值,也无所谓。如果函数有返回值,那必须返回。
三、PHP7特性:
PHP7允许在函数中增加返回值。比如string、int、array、object等
function 函数名(): string //注意冒号
{
}
四、课程代码:
第一课我们建立了GOD这个文件,这一课,我们建立GOD_FUNC文件,通过reuqire在god文件中引入函数文件god_func。
同时,我们为了学习PHP7新特性,专门建立god_func7这个文件,并在god文件中判断引入。
1、god
#!/usr/local/php/bin/php <?php require(‘god_fun‘.substr(PHP_VERSION,0,1)); //判断PHP版本后引入不同的god_func $result =‘‘; if($argc >=2 ) { ‘-v‘==$argv[1] && $result = ‘god version is 1.0 ‘; ‘init‘ == $argv[1] && $result = genConfig(); } echo $result; echo PHP_EOL; ?>
2、god_func
<?php function genConfig() { return file_put_contents(getcwd().‘/god.json‘,‘{}‘).‘ of bytes is written.‘.PHP_EOL.‘god config is created‘; } ?>
3、god_func7
1 <?php 2 function genConfig():string 3 { 4 return file_put_contents(getcwd().‘/god.json‘,‘{}‘).‘ of bytes is written.‘.PHP_EOL.‘god config is created‘; 5 6 } 7 ?>
标签:
原文地址:http://www.cnblogs.com/xz1024/p/5628377.html