Shen Yi's notes on PHP devil training (2), Shen Yi's devil
1. This course will learn a few lazy functions:
1、file_put_contents
(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 is the same as calling the fopen (), fwrite (), and fclose () functions in turn.
Iffilename
Does not exist, the file is created. Otherwise, the existing file is overwritten, unlessFILE_APPEND
Flag is set.
Parameters
-
filename
-
The name of the file to be written.
-
data
-
The data to be written. The type can be string, array, or stream resources (as mentioned above ).
Ifdata
If it is specified as a stream resource, the cached data stored in the stream will be written to the specified file. This usage is similar to using the stream_copy_to_stream () function.
Parametersdata
It can be an array (but not a multi-dimensional array), which is equivalentFile_put_contents ($ filename, join ('', $ array )).
-
flags
-
flags
The value can be the following flag OR (|) Operator.
For example(Derived from P
HP.net)
<?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 () // get the current working directory
PHP 4, PHP 5, PHP 7
Getcwd-get the current working directory
Description
String
Getcwd(Void)
Obtain the current working directory.
Return Value
If the job is successful, the current working directory is returned. If the job failsFALSE
.
In some Unix variants, if no parent directory is set to readable or search mode, even if the current directory is set,Getcwd ()Will returnFALSE
. For more information about modes and permissions, see chmod ().
1 example: In ubuntu Terminal 2 tiger @ xz1024 :~ $ Php-r "echo getcwd ();" 3/home/tigertiger @ xz1024 :~ $
3、substr()
(PHP 4, PHP 5, PHP 7)
Substr-returns the substring of the string.
Description
String
Substr(String
$string
, Int
$start
[, Int
$length
])
Returns a string.string
Bystart
Andlength
The substring specified by the parameter.
Parameters
-
string
-
Input string. It must contain at least one character.
-
start
-
Ifstart
The return string is fromstring
Ofstart
Start from 0. For example, in the string"Abcdef", In the location0The character is"A", Location2The string is"CAnd so on.
Ifstart
Is a negative number, and the returned string isstring
Forward number at the endstart
Characters.
Ifstring
The length is lessstart
, Will returnFALSE
.
Example #1 use a negative numberstart
<? Php
$ Rest = substr ("abcdef",-1); // return "f"
$ Rest = substr ("abcdef",-2); // return "ef"
$ Rest = substr ("abcdef",-3, 1); // return "d"
?>
length
If a positive number is providedlength
, The returned string will bestart
Which can start at mostlength
Characters (depending onstring
Length ).
If a negativelength
, Thenstring
Many characters at the end will be missed (ifstart
Is a negative number, which is counted from the end of the string ). Ifstart
Otherwise, an empty string is returned.
If the value is0,FALSE
OrNULL
Oflength
Returns an empty string.
If nolength
, The returned substring isstart
The position starts until the end of the string.
Example #2 use a negative numberlength
<? Php
$ Rest = substr ("abcdef", 0,-1); // return "abcde"
$ Rest = substr ("abcdef", 2,-1); // return "cde"
$ Rest = substr ("abcdef", 4,-4); // return ""
$ Rest = substr ("abcdef",-3,-1); // return "de"
?>
Ii. Define a UDF
PHP-defined functions
Function Name (parameter 1, parameter 2, parameter n) // you must have the keyword funciton {function body ;}
If you want to return, ruturn. It doesn't matter if you forget the return value. If a function returns a value, it must be returned.
Iii. php 7 features:
PHP 7 allows adding return values to functions. Such as string, int, array, object, etc.
Function Name (): string // note the colon
{
}
Iv. course code:
In the first lesson, we created the GOD file. In this lesson, we created the GOD_FUNC file and introduced the function file god_func in the god file through reuqire.
At the same time, in order to learn the new features of PHP 7, we create the god_func7 file and judge and introduce it in the god file.
1. god
#! /Usr/local/php/bin/php <? Php require ('God _ fun '. substr (PHP_VERSION,); // after determining the PHP version, introduce different 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 <?php2 function genConfig():string3 {4 return file_put_contents(getcwd().'/god.json','{}').' of bytes is written.'.PHP_EOL.'god config is created';5 6 }7 ?>