Home >Backend Development >PHP Tutorial >Basic operations of php
This article mainly introduces the basic operations of php, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
php basic knowledge
array function
count($arr) Calculate the number of elements in the array
item){} Traverse the values of the array
##key=>$value) Traverse the keys and values of the array
##shuffle ();Random sort
array_reverse(); Reverse the original order of elements
##Array pointer operation
reset(); Point the array pointer to the first element
Other simple operations on the array
Array assignment to variables
##item1) = array(' ha','haha','hahaha'); Directly assign the first two elements of the array to variablesQuickly create an array
##arr1,$arr2 value and index are both To do the sameExchange the index and element of the array
array_filp($arr);
Quickly fill array elements
array_file(2,10,'haha') Fill a value for the index 2-10
Calculate the element in The number of occurrences in the array
array_count_values($arr) generates an array to count how many times each element in the array appears
Judge the number of occurrences in the array Whether the index exists
array_key_exists('haha',$arr)
Get the index in the value
array_key($arr)
Traverse each element in the array execution method
array_map('cube'// Method name, $arr);
Other functions
in_array('haha',$arr); Determine whether a value exists in the array Medium
key('haha',$arr); Get the index of the current element
implode(',',$arr) concatenates the array elements into a string
of the string Compare and Replace
str_replace('str'//The content to be replaced,
str2The string being replaced); Replace part of the content
str_replace('haha',
str2) Single content replacement
str-replace(
arr2,$str) Replace multiple words, replace the two arrays one by one
##num1 ,$num2) Intercept a string
Remove the whitespace characters of the string'strtolower($str) Convert to lower case
##Prevent the compilation of html tags in the string < -> &it
htmlentities($str)
html_entity_decode($str);
md5($str1);
Operation directories and files
Open and close directories
path) Open the directory to obtain the handle closedir($dh) Complete the file operation and close the handle Release resources
Output sequence./../The following is sorted by the first character of the file
getcwd() gets the path of the current php
r Read only from scratch
mode); Open the file to get the handlemode and open the file in the mode
feof($fp) Determine whether the end of the file is reached
file($path) returns an array, each element is the content of each line
get_file_content returns the content of the entire file
Write file
fwrite(
str,$length) Write file contents
file_put_contents(
content) Equivalent to fopen; fwrite(); fclose(); three-in-one
path2 )Copy the file to the specified path
path2) Move files
group) Change the group of the file
chmod($file,0777) Change the file mode
require($path);Introduction of files If the file is lost, the entire program will make an error
Y year
UNIX timestamp (from 1970 1 The number of seconds calculated starting at 0:00 on the 1st of the month)
mktime(
minute,
month,
##year) If the parameter is omitted, the current time isday,$year) Determine whether it is valid
url address analysis
##urlencode($str) Encoding the url is to convert the non-digits and non-letters in the url Into %xx two-digit hexadecimal number##urldecode($str); Convert %xx into the original character
parse_url($url) returns an array containing various information of the url
host host ip address And domain name
port port number
pass user name
pass user password
path access path
query query parameter is the information behind the url
fragment parameter fragment
header($str) header is used to describe the server communication process and communication time must be in all actual The predefined variables obtained in front of the output
Get the header information
$_HTTP_HOST http host
$_SERVER_NAME server name
$_SERVER_ADDR server address
$_SERVER_PORT Server port
$_SERVER_PROTOCOL Server protocol
setcookie(name //cookie name,[value//cookie value, exprire//cookie expiration time, path//valid path, domain//valid domain name, secure//https secure transmission valid]) Generate cookie
$_COOKIE['cookiename'] Get cookie
setcookie('mycookie[love]','yisayu '); Generate array cookie
setcookie('mycookie','yisayu',time() 60*60)/mktime(0,12,12,45,2017);set Cookie validity period: The cookie will expire after 1 hour. If not set or set to 0, the cookie will expire when the browser is closed.
The cookie will be sent back to the page in the same directory by default. If you want to send it back to a different page, Pages in the directory
abs($num) absolute value
ceil($num) ceiling
floor($num)floor
sqrt($num) square root
round($num, 1) rounding The second parameter is reserved Rounding of decimal places
Decimal conversion
decbin($num) ten to two
dechex($num) ten to sixteen
decoct($num) ten to eight
bindec($num) two to Ten
octdec($num)eight to ten
base_convert($num,16,2) Convert num from the base of form To the base of to
Generate a random number
mt-rand(
max) Generate random numbers
Get system informationGetting php environment variables has the same effect as $_SERVER
##getenv('DOCUMENT_ROOT') The environment variable is the file root directory
putenv('NAME =$env_var');
get_current_user() returns the name of the owner of the current program
Related recommendations:
A brief introduction to the basics of PHP
The above is the detailed content of Basic operations of php. For more information, please follow other related articles on the PHP Chinese website!