Php basic functions encountered method

PHP 0x01  File () function

file () function to read the entire file into an array.

Each element in the array is a file in one row, including line breaks included.

Example:

<?php
print_r(file("test.txt"));
?>

The above will output:

Array
(
[0] => Hello World. Testing testing!
[1] => Another day, another line.
[2] => If the array picks up this line,
[3] => then is it a pickup line?
)

0x02PHP print_r () function

print_r ()  function is used to print variable, displayed in a form easier to understand.

PHP Version: PHP 4, PHP 5, PHP 7

Example:

<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x','y','z'));
print_r ($a);
?>

The output is:

Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )

)

0x03 php difference function system (), exec (), passthru () of

php provides a system (), exec (), passthru () function to call these external commands.

Difference: 
System () returns the output of the last row and shell results. 
exec () does not output the result, return to the last row shell results, all the results can be saved to a return inside the array. 
passthru () call only command, the operation result of the command output as it directly to the standard output device. 
The same point: can get a status code of command execution

0x04 php eval function

eval () function is calculated in accordance with the PHP code string. The string must be a valid PHP code, and must end with a semicolon.

E.g:

eval("echo'hello world';");
Identical to this top code below:
echo"hello world";

In the browser will output: hello world
end of the string parameter 1.eval function must have a semicolon, but also plus a semicolon at the end (this is the semicolon php limit)

2. Note that in the command string formula (including semicolon) on both sides of double quotes must be enclosed in single quotes, or as needed. Otherwise error. It refers to a command string type: string including echo, print and the like when the command. If the parameter is only one variable, you can not.

3. Note the single quotes, use double quotes or escapes. If the parameters with variables and assignment variables, then the variable $ symbol before the money must be \ escaped. If there is no assignment operator may not be required.

The role of shielding an error message on the front @ function.

PHP 0x05  file_get_contents () function

file_get_contents () to read the entire file into a string.

This function is used to read the contents of the file into a string of the preferred method. If the server operating system support, will use memory mapping techniques to enhance performance.

Example:

<?php
echo file_get_contents("test.txt");
?>

The above will output:

This is a test file with test text.

0x06 PHP's get_defined_functions () function

List all functions

php built-in functions and all the functions you define

print_r(get_defined_functions());

() Is the difference between 0x07 PHP array_merge function of the array + array

It may be used in PHP array_merge function and adding the two arrays into an array array + array manner, but the two are not the same effect, the following specific use to introduce the difference between the two.

Differences are as follows:

 1, when the value of the subscript, The array_merge () does not overwrite the original value, but the array + array merge array will return the value that appears first as a final result, and the back of the array have the same values ​​as those of the key name "abandon "out (not covered). 

2, and when lower case character, array + array is still the value that appears first as the final results are returned, and the back of the array have those values ​​for the same key name of "abandoned" out, but array_merge () at this time will overwrite the front of the same key value name. 

example:

$ arr1 = [ "PHP", "Apache" ];
$ arr2 = [ 'PHP', 'MySQL', 'HTML', 'TLC' ];
$ mergeArr = array_merge ( $ arr1 , $ arr2 );
$ plusArr = $ arr1 + $ arr2 ;
var_dump ( $ mergeArr );
var_dump ( $ plusArr );

result:

$mergeArrarray (size=6)
  0 => string 'PHP' (length=3)
  1 => string 'apache' (length=5)
  2 => string 'PHP' (length=3)
  3 => string 'MySQl' (length=5)
  4 => string 'HTML' (length=4)
  5 => string 'CSS' (length=3)

$plusArrarray (size=4)
  0 => string 'PHP' (length=3)
  1 => string 'apache' (length=5)
  2 => string 'HTML' (length=4)
  3 => string 'CSS' (length=3

 

Guess you like

Origin www.cnblogs.com/-chenxs/p/11461475.html