Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the common functions of PHP

Shulou Source: shulou.com Published: 2022-06-02 19:37:04 10月03日 Update

This article mainly shows you the "what are the common functions of PHP", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what common functions PHP has" this article.

PHP common function 1. Generate a random string function

Function random ($length) {

$hash = @ # @

$chars = @ # abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz@#

$max = strlen ($chars)-1

Mt_srand ((double) microtime () * 1000000)

For ($I = 0; $I

< $length; $i++) { $hash .= $chars[mt_rand(0, $max)]; } return $hash; } PHP常用函数2.截取一定长度的字符串 注:该函数对gb2312使用有效 function wordscut($string, $length ,$sss=0) { if(strlen($string) >

$length) {if ($sss) {$length=$length-3; $addstr=@#... @ #;} for ($I = 0; $I

< $length; $i++) { if(ord($string[$i]) >

{$wordscut. = $string [$I]. $string [$I + 1]; $iTunes;} else {$wordscut. = $string [$I];}} return $wordscut.$addstr;} return $string;}

PHP common function 3. Get the client ip address

Function getip () {

If (getenv ("http_client_ip")

& & strcasecmp (getenv ("http_client_ip"), "unknown")

$ip = getenv ("http_client_ip")

Else if (getenv ("http_x_forwarded_for")

& & strcasecmp (getenv ("http_x_forwarded_for"), "unknown")

$ip = getenv ("http_x_forwarded_for")

Else if (getenv ("remote_addr")

& & strcasecmp (getenv ("remote_addr"), "unknown")

$ip = getenv ("remote_addr")

Else if (isset ($_ server [@ # remote_addr@#])

& & $_ server [@ # remote_addr@#]

& & strcasecmp ($_ server [@ # remote_addr@#], "unknown"))

$ip = $_ server [@ # remote_addr@#]

Else

$ip = "unknown"

Return ($ip)

}

PHP common function 4. Create the appropriate folder

Function createdir ($dir=@#@#) {if (! is_dir ($dir)) {$temp = explode (@ # / @ #, $dir); $cur_dir = @ # @ #; for

Tags: Functions frequently used content articles characters strings learning help valid address customer client file folder easy to understand more organization knowledge editing industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Linux Xiaomi Huawei Microsoft