


Detailed introduction to related functions about system information when PHP scripts are run
This article will give you a detailed introduction to the functions related to system information when the current PHP script is running. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
When our PHP is executed, we can actually obtain a lot of information related to the current system. Just like many open source CMSs, they generally detect some environmental information during installation. This information can be easily and dynamically obtained.
System user related information when the script file is running
First, let’s take a look at obtaining some user information related to the current system. This user information is the system user used by our system when running php scripts.
echo '当前脚本拥有者:' . get_current_user(), PHP_EOL; // 当前脚本拥有者:zhangyue echo '当前脚本属组:' . getmygid(), PHP_EOL; // 当前脚本属组:20 echo '当前脚本的用户属主:' . getmyuid(), PHP_EOL; // 当前脚本的用户属主:501
Did you see it? In fact, these three functions are the corresponding file owners and groups in Linux. get_current_user() returns the user name, and getmyuid() returns the user's UID. They two correspond to the same user. getmygid() returns the user group to which the current user belongs.
Get system-related information of the currently running script
This set of functions allows us to obtain the innode information of the system, the process ID of the current script when it is running, and the service interface type. , operating system information and resource usage on which PHP is running.
echo '当前脚本的索引节点:' . getmyinode(), PHP_EOL; // 当前脚本的索引节点:8691989143 echo '当前脚本的进程ID:' . getmypid(), PHP_EOL; // 当前脚本的进程ID:1854 // Nginx:当前脚本的进程ID:711(php-fpm的进程ID) echo "web服务器和PHP之间的接口类型:" . php_sapi_name(), PHP_EOL; // web服务器和PHP之间的接口类型:cli // Nginx:web服务器和PHP之间的接口类型:fpm-fcgi echo "运行 PHP 的系统:" . php_uname("a"), PHP_EOL; // 运行 PHP 的系统:Darwin zhangyuedeMBP 19.4.0 Darwin Kernel Version 19.4.0: Wed Mar 4 22:28:40 PST 2020; root:xnu-6153.101.6~15/RELEASE_X86_64 x86_64 // echo "运行PHP的系统:" . PHP_OS, PHP_EOL; // 运行 PHP 的系统:Darwin // 当前脚本的资源使用情况 print_r(getrusage()); // Array // ( // [ru_oublock] => 0 // [ru_inblock] => 0 // [ru_msgsnd] => 0 // [ru_msgrcv] => 0 // [ru_maxrss] => 16809984 // [ru_ixrss] => 0 // [ru_idrss] => 0 // [ru_minflt] => 4410 // [ru_majflt] => 1 // [ru_nsignals] => 0 // [ru_nvcsw] => 0 // [ru_nivcsw] => 86 // [ru_nswap] => 0 // [ru_utime.tv_usec] => 41586 // [ru_utime.tv_sec] => 0 // [ru_stime.tv_usec] => 41276 // [ru_stime.tv_sec] => 0 // )
From the comments, we can see that getmypid() returns the process ID of the current execution when using the command line, and returns the process ID of PHP-FPM when accessed in the web page. In the same way, php_sapi_name() will also return different content according to the current running environment.
php_uname() The default parameter is 'a', which means to return complete operating system information. It also has other parameters that can return separate and different information, or when we just need to know what system we are currently operating on, it will be more convenient to use the PHP_OS constant directly.
getrusage() can return the status of system resources. For example, ru_nswap is the usage of the current swap area of the system, but these parameters are not explained in detail. After all, this function is still used relatively rarely.
Get the version information of PHP and related extension components
echo "当前的PHP版本:" . phpversion(), PHP_EOL; // 当前的PHP版本:7.3.0 echo "当前的PHP版本:" . PHP_VERSION, PHP_EOL; // 当前的PHP版本:7.3.0 echo "当前某个扩展的版本(Swoole):" . phpversion('swoole'), PHP_EOL; // 当前某个扩展的版本(Swoole):4.4.12 echo "当前的PHP的zend引擎版本:" . zend_version(), PHP_EOL; // 当前的PHP的zend引擎版本:3.3.0-dev if (version_compare(PHP_VERSION, '7.0.0') >= 0) { echo '我的版本大于7.0.0,当前版本是:' . PHP_VERSION . "\n"; } else { echo '我的版本还在5,要赶紧升级了,当前版本是:' . PHP_VERSION . "\n"; }
phpversion() has the same effect as the PHP_VERSION constant without parameters, and returns the current The version number of PHP that is running. However, phpversion() can be given an extension name as a parameter, so that it can return the version number of the extension. Just like in the example, we get the version number of Swoole in the current environment. zend_version() simply returns the Zend engine version number in the current running environment.
version_compare() can help us compare version numbers conveniently. It is a comma-separated version comparison, which means that any string version number we define can be compared using it. For specific comparison rules, please refer to the official documentation.
The modification time and script running time of the current script file
echo "当前脚本文件的最后修改时间: " . date("Y-m-d H:i:s.", getlastmod()), PHP_EOL; // 当前脚本文件的最后修改时间: 2020-06-01 08:55:49. // nginx环境下 set_time_limit(84600); // while(1){ // }
getlastmod() is very simple, it returns the last modified time of the currently running PHP file. And set_time_limit() I believe everyone is familiar with it. By default, web requests will not last for a long time before they are actively disconnected.
For example, in the php.ini file, the max_execution_time we define by default is 30 seconds. When a request consumes more than this time, the request will be disconnected. However, there are always some requests that really take us longer to execute. For example, generating documents such as Excel often takes longer.
At this time, we can use set_time_limit() to set the maximum execution time of the script to extend the execution timeout of the web request.
Test code:
https://github.com/zhangyue0503/dev-blog/blob/master/php/202006/source/%E5%85%B3%E4%BA%8E%E5%BD%93%E5%89%8DPHP%E8%84%9A%E6%9C%AC%E8%BF%90%E8%A1%8C%E6%97%B6%E7%B3%BB%E7%BB%9F%E4%BF%A1%E6%81%AF%E7%9B%B8%E5%85%B3%E5%87%BD%E6%95%B0.php
Recommended learning: php video tutorial
The above is the detailed content of Detailed introduction to related functions about system information when PHP scripts are run. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
