


Summary of commonly used functions and global variables in PHP (recommended)
This article introduces to you a summary (recommended) of commonly used functions and global variables in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. PHP built-in function
A
array_shift() deletes the first element in the array
array_pop() Delete the last element in the array
array_multisort() Return an ascending array
C
call_user_func_array() Call the callback function, and Call an array as a parameter of the callback function
D
define() define constant
debug_backtrace() trace code call information
E
error_reporting(0) Turn off error reporting
error_reporting(E_ERROR | E_WARNING | E_PARSE) Report runtime errors
error_reporting(E_ALL) Report all errors
ini_set("error_reporting", E_ALL) Equivalent to error_reporting(E_ALL)
error_reporting(E_ALL & ~E_NOTICE) Reports all errors except E_NOTICE
explode() will Split the string into an array
implode() Splice the elements in the array into a string
G
1.getenv() Get the value of the environment variable
$_SERVER['HTTP_HOST'] = getenv('HTTP_HOST');
H
header() Sends the original HTTP header to the client
I
is_file() Determines whether it is a file
ini_get() Gets the value of a configuration option
is_set() Whether it has been defined
is_null() Whether it is empty
P
1. phpversion() Current PHP version
R
require_once() Loading File
S
str_replace() Replace some characters in a string with other characters (case sensitive)
strlen() Get a string Length
substr() Return part of the string
str_repalce() Replace part of the string with another part
strtolower() Convert to lowercase
spl_autoload_register() Automatic loading
spl_autoload_extensions() Returns the automatically loaded file extension
U
unset() Destroy
V
1. version_compare( string $version1
, string $version2
[, string $operator
] ) Compare the first version number of PHP version
version1
. version2 The second version number operator operator , lt, , le, >、 gt、>=、 ge、==、 =、eq , !=, and ne
For example:
if (version_compare(phpversion(), '5.6.0', '<')) { exit('PHP5.6+ Required'); }
2. Super global variables
$_SERVER
$_SERVER['DOCUMENT_ROOT']; The document root where the currently running script is located
$_SERVER['DOCUMENT_FILENAME'] The absolute path of the currently executing script
$_SERVER['PHP_SELF'] The file name of the currently executing script
$_SERVER['HTTP_HOST'] Get the current host
$_SERVER['PATH_TRANSLATED'] The base path where the current script is located, non-document root directory
$_SERVER['REQUEST_URI'] The current request path
$ _SERVER['QUERY_STRING'] Query string
$_SERVER['SERVER_PORT'] Server port
$_SERVER['HTTPS'] HTTPS request
Type operator
instanceof Determines whether a variable is an instance of a class
3. Common PHP encapsulated functions:
1. Determination Is it an HTTPS request?
// 检查是否是HTTPS请求 if ((isset($_SERVER['HTTPS']) && (($_SERVER['HTTPS'] == 'on') || ($_SERVER['HTTPS'] == '1'))) || (isset($_SERVER['HTTPS']) && (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443))) { $_SERVER['HTTPS'] = true; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') { $_SERVER['HTTPS'] = true; } else { $_SERVER['HTTPS'] = false; }
Recommended related articles:
Code implementation of the construction method and destructor method in php
How to implement counting sorting code in PHP
The above is the detailed content of Summary of commonly used functions and global variables in PHP (recommended). For more information, please follow other related articles on the PHP Chinese website!

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor
