php uses system built-in functions

Brother Zhu from the PHP Chinese website summarized these two sentences:

1. If the code can make money, the thing that helps you make the most money is basic syntax

2. If you are still a programmer, you may spend more time reading the manual than you do looking at your wife

Now let’s learn how to use the system’s built-in functions.

Confucius said: I would rather give you a fish than teach you how to fish.

Let’s learn how to fish. Before using the system’s built-in functions, you must learn the following things:

1. Download one The latest manual

2. Update the manual frequently

3. Place the PHP manual where you can find it most easily. You can check the manual at any time when needed

4. Learn how to use the functions in the manual

You can access it, download it and put it on the desktop, you can open it and check it anytime you need:

http://www.php.cn (updated once a week)

The key points of using functions are three parts:

1. Understand the functions of functions, especially the functions of commonly used functions

2. Understand the parameters of the function

3. Understand the return value of the function

We will explain 6 functions based on the above three blocks. These 6 functions are summarized. All notes on the basic usage of functions:

1. Directly return Boolean type, such as bool copy ()

2. How to call functions with MIXED parameters. Mixed represents any type of data. For example, Array_unshift()

3. For parameters with ampersand, a variable must be passed as a parameter. Its value is changed in the function.

4. Parameters with [] indicate optional options.

5. Parameters with... indicate that any number of parameters can be passed.

6. Parameters with callback represent the callback function. A function needs to be passed in. Array_map()

7. You need to know the versions supported by the function

Experiment
1, take the copy() function as an example: the bool value returned is usually whether the operation is successful, Check whether the verification is passed, whether the check is correct, etc.

Let’s take a look at the copy function:

bool copy ( string $source , string $dest [, resource $context ] )

The function of this function is: Copy a file
The return value is: bool type value, which means returning true if successful, false if failed
The parameters are: Two string values, one is the source file of the copy, and the other is the target file. The third parameter is optional and not commonly used, so we don't care about it.

So, we can deduce the following experiment:

<?php

if(copy('abc.txt','bcd.txt')){
   echo '复制成功';
}else{
   echo '复制失败';
}
?>

2, Mixed represents any type of data. Such as Array_unshift()

Let’s take a look at this function:

int array_unshift ( array &$array , mixed $value1 [, mixed $... ] )

Function: Operate an array and insert other types of parameters into the array.

Return value: int type, may be the last number of successful insertions

Parameters: The first parameter is the ampersand, that is During the operation, the value of the first parameter is changed. Pass parameters by reference. That is to operate this array and pass parameters into this array. will directly change the value of this array.

The second parameter is mixed, because the array can store multiple different types. Mixed means mixed. Therefore, mixed means that any type can be passed in.

The third number has brackets added, and we all encounter brackets. This all means that the following parameters may or may not be passed.

Fourth, finally I saw three...(ellipses). It means you can pass in any number of parameters.

Example:

<?php
$queue = array("凤姐", "芙蓉");
array_unshift($queue, "杨幂", "姚晨");
print_r($queue);
?>

You can experiment it yourself and see the results.

3. When encountering callback transfer functions or anonymous functions, go in to assist in processing, making the function more powerful.

bool array_walk ( array &$array , callable $callback [, mixed $userdata = NULL ] )

Let’s take a look.

Function:

Pass in a callback function to operate the original group of the array and make changes.

Return value:
bool value means prompting success or failure

Parameters:
The first parameter is the array to be operated on.

The second parameter is callback, which means you can pass in a function or anonymous function.

Let’s write an example to enhance understanding:

<?php
$shuaige = array("a" => "wuyanzhu", "b" => "huangxiaoming", "c" => "ninzetao");

function test_print($item2, $key)
{
   echo $key ." ---". strtoupper($item2) . "<br />\n";
}

echo '<pre>';
var_dump($shuaige);
echo '</pre>';


array_walk($shuaige, 'test_print');

echo '用自定义函数test_print执行后的效果:';

echo '<pre>';
var_dump($shuaige);
echo '</pre>';

?>

Through the above example, we found that every parameter and value in the array has been modified. In the above example, you only need to understand that when you see that callback is passed, you need to pass in a function to assist in processing. No need to understand. We have finished studying arrays in the array part of the next chapter, and you can take a look again.

The version number supported by the function is very important

Let’s look at a screenshot of a function in the manual:

document_2015-08-25_55dbdb30ae6ae.png

This is a system function, not a custom function. This function exists in the system function manual, but why can't it be called and executed? Please note that (PHP 5 >= 5.5.0), you can use phpinfo() to see your current version. Sometimes it may be because your version is too low, or the function does not exist in the version you are in, and you will be prompted that the function does not exist.

Check the manual whenever you need it. The manual is more intimate than my wife.

QQ图片20161114111742.jpg

Continuing Learning
||
<?php $shuaige = array("a" => "wuyanzhu", "b" => "huangxiaoming", "c" => "ninzetao"); function test_print($item2, $key) { echo $key ." ---". strtoupper($item2) . "<br />\n"; } echo '<pre>'; var_dump($shuaige); echo '</pre>'; array_walk($shuaige, 'test_print'); echo '用自定义函数test_print执行后的效果:'; echo '<pre>'; var_dump($shuaige); echo '</pre>'; ?>
submitReset Code