Have you checked all the PHP methods? I have made a rough statistics on 5025 PHP reference methods. If you are a beginner in this web programming language, I will introduce several very practical PHP methods, hoping to help you!
Have you checked all the PHP methods? I have made a rough statistics on 5025 PHP reference methods. If you are a beginner in this web programming language, I will introduce several very practical PHP methods, hoping to help you!
Function1: array_rand
First, we will introduce this simple method array_rand.
Do you want to randomly retrieve one from an array? You can use rand or mt_rand to get a random number. pass 0 and the last index value as the minimum and maximum parameters of the array, this will give you a random key that you can use to obtain the value of the array.
Of course, there is a more convenient way to implement the above function: array_rand, you only need to pass an array, it will automatically return a random key to you. As follows:
$ Sites = ["BBB.com", "AAA.com", "baidu.com", "google.com"];
$ K = array_rand ($ sites );
$ Sites [$ k];
If you need more than one random value, you can also pass the second parameter to set the number of returned values so that you can get a random array.
Function2: strip_tags
We often get large text content from the client: it may be a message or a personal introduction. Of course, you may not want to include any HTML tags. If you want to remove html, you can use strip_tags:
$ Message = "This is www. ***. com ";
Echo strip_tags ($ message); // return "This is www. ***. com"
Of course, you may want to allow some labels, such,You can pass the second parameter as follows:
$ Message = "This isWww. ***. com";
Echo strip_tags ($ message ,""); // "This is www.***.com"
Function3:strftime
Date is an object that we often process in web programs. how do you output the date in the format you need? It is easy to get timestamp, but how to get the format? The strftime method can help you quickly obtain the format you need. you only need to pass the format string and timestamp, as shown below:
strftime("%B %d, %Y", time()); // July 28, 2012
Of course, if you do not remember the date format, you can use this convenient strfti. me address to query.
Function4:basename
When we need to process a file, you need to know its absolute path. However, if you want to display the file information to users, you may need to display the file name, not the entire path. You can use the basename method to find the file name or suffix corresponding to the path, as shown below:
$path = "/some/long/path/to/the/BBB.html";
$filename1 = basename($path); // BBB.html
$filename2 = basename($path, ".html"); // BBB
Function5:list
This method is superb. for example, if you have an array and want to assign the array content to the variable, this method will be super simple:
$array = ["BBB", "AAA"];
list($first_website, $last_website) = $array;
echo $first_website; // BBB
echo $last_website; // AAA
As you can see, we only pass a variable name parameter to the list method and set it to an array. There is a difference with general syntax, because the method is called on the left side, but it does work. Here, the PHP document provides a very good example:
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
Function6:range
If you want to list an array, you need to look at the range method. You only need to input a start and end values. it returns an array of numbers:
range(0, 10); // array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
range('a', 'f'); // array('a', 'b', 'c', 'd', 'e'. 'f');
You can see that the boundary number is included, that is, the start and end values you provide. You can also specify to return only the numbers in the middle of them:
range(2, 10, 2); // array(2, 4, 6, 8, 10);
Function7:isset
This practical method can be used to check whether a variable has been set. You pass a variable name. if the variable is saved or a value already set to a non-null value, true is returned.
$name = "BBB.com";
isset($name); // true
isset($age); // false
This method can also process arrays, so it can be used to check whether the keys specified in the global array $ _ GET and $ _ POST exist. As follows:
if(isset($_GET['query'])) {
// get results and display them
} else {
// show some default content
}
You have learned 7 very useful PHP methods. if you are a beginner in PHP, you can read related PHP books. if you also have your favorite Practical PHP methods, please leave us a message! Thank you for reading!