


Teacher Shen Yi's PHP Devil Special Training Notes (2), Shen Yi Devil_PHP Tutorial
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>
(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() //获取当前工作目录
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 < 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:~$
3、<span>substr</span>()
Substr — Returns the substring of string
说明
string substr ( string $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<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
不在这段文本中,那么将返回一个空字符串。如果提供了值为 0,
FALSE
或NULL
的length
,那么将返回一个空字符串。如果没有提供
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>
如果要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>?>
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>?>
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> ?>

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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)

Hot Topics











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

RESTAPI design principles include resource definition, URI design, HTTP method usage, status code usage, version control, and HATEOAS. 1. Resources should be represented by nouns and maintained at a hierarchy. 2. HTTP methods should conform to their semantics, such as GET is used to obtain resources. 3. The status code should be used correctly, such as 404 means that the resource does not exist. 4. Version control can be implemented through URI or header. 5. HATEOAS boots client operations through links in response.

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

The main function of anonymous classes in PHP is to create one-time objects. 1. Anonymous classes allow classes without names to be directly defined in the code, which is suitable for temporary requirements. 2. They can inherit classes or implement interfaces to increase flexibility. 3. Pay attention to performance and code readability when using it, and avoid repeatedly defining the same anonymous classes.

In PHP, the difference between include, require, include_once, require_once is: 1) include generates a warning and continues to execute, 2) require generates a fatal error and stops execution, 3) include_once and require_once prevent repeated inclusions. The choice of these functions depends on the importance of the file and whether it is necessary to prevent duplicate inclusion. Rational use can improve the readability and maintainability of the code.

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
