8 essential PHP function development_PHP tutorial
Programmers who have done PHP development should know that there are many built-in functions in PHP. Mastering them can help you become more comfortable in PHP development. This article will share 8 essential PHP functions for development, each of which They are all very practical and I hope all PHP developers can master them.
1. Pass any number of function parameters
In .NET or JAVA programming, the number of function parameters is generally fixed, but PHP allows you to use any number of parameters.The following example shows you the default parameters of a PHP function:
Php code
- // Function with two default parameters
- function foo($arg1 = ”, $arg2 = ”) {
- echo “arg1: $arg1n”;
- echo “arg2: $arg2n”;
- }
- foo(‘hello’,’world’);
- /* Output:
- arg1: hello
- arg2: world
- */
- foo();
- /* Output:
- arg1:
- arg2:
- */
- The following example is the usage of variable parameters in PHP, which uses the [url=http://us2.php.net/manual/en/function.func-get-args.php]func_get_args()[/url] method:
- // Yes, the parameter list is empty
- function foo() {
- // Get the array of all incoming parameters
- $args = func_get_args();
- foreach ($args as $k => $v) {
- echo “arg”.($k+1).”: $vn”;
- }
- }
- foo();
- /* Nothing will be output */
- foo(‘hello’);
- /* Output
- arg1: hello
- */
- foo(‘hello’, ‘world’, ‘again’);
- /* Output
- arg1: hello
- arg2: world
- arg3: again
- */
- // Get all files with the suffix PHP
- $files = glob(‘*.php’);
- print_r($files);
- /* Output:
- Array
- (
- [0] => phptest.php
- [1] => pi.php
- [2] => post_output.php
- [3] => test.php
- )
- */
- // Get PHP files and TXT files
- $files = glob(‘*.{php,txt}’, GLOB_BRACE);
- print_r($files);
- /* Output:
- Array
- (
- [0] => phptest.php
- [1] => pi.php
- [2] => post_output.php
- [3] => test.php
- [4] => log.txt
- [5] => test.txt
- )
- */
- $files = glob(‘../images/a*.jpg’);
- print_r($files);
- /* Output:
- Array
- (
- [0] => ../images/apple.jpg
- [1] => ../images/art.jpg
- )
- */
- $files = glob(‘../images/a*.jpg’);
- //applies the function to each array element
- $files = array_map(‘realpath’,$files);
- print_r($files);
- /* output looks like:
- Array
- (
- [0] => C:wampwwwimagesapple.jpg
- [1] => C:wampwwwimagesart.jpg
- )
- */
- echo “Initial: “.memory_get_usage().” bytes n”;
- /* Output
- Initial: 361400 bytes
- */
- //Use memory
- for ($i = 0; $i
- $array []= md5($i);
- }
- // Delete half of the memory
- for ($i = 0; $i
- unset($array[$i]);
- }
- echo “Final: “.memory_get_usage().” bytes n”;
- /* prints
- Final: 885912 bytes
- */
- echo “Peak: “.memory_get_peak_usage().” bytes n”;
- /* Output peak value
- Peak: 13687072 bytes
- */
- print_r(getrusage());
- /* 输出
- Array
- (
- [ru_oublock] => 0
- [ru_inblock] => 0
- [ru_msgsnd] => 2
- [ru_msgrcv] => 3
- [ru_maxrss] => 12692
- [ru_ixrss] => 764
- [ru_idrss] => 3864
- [ru_minflt] => 94
- [ru_majflt] => 0
- [ru_nsignals] => 1
- [ru_nvcsw] => 67
- [ru_nivcsw] => 4
- [ru_nswap] => 0
- [ru_utime.tv_usec] => 0
- [ru_utime.tv_sec] => 0
- [ru_stime.tv_usec] => 6269
- [ru_stime.tv_sec] => 0
- )
- */
- ru_oublock: 块输出操作
- ru_inblock: 块输入操作
- ru_msgsnd: 发送的message
- ru_msgrcv: 收到的message
- ru_maxrss: 最大驻留集大小
- ru_ixrss: 全部共享内存大小
- ru_idrss:全部非共享内存大小
- ru_minflt: 页回收
- ru_majflt: 页失效
- ru_nsignals: 收到的信号
- ru_nvcsw: 主动上下文切换
- ru_nivcsw: 被动上下文切换
- ru_nswap: 交换区
- ru_utime.tv_usec: 用户态时间 (microseconds)
- ru_utime.tv_sec: 用户态时间(seconds)
- ru_stime.tv_usec: 系统内核时间 (microseconds)
- ru_stime.tv_sec: 系统内核时间?(seconds)
- // sleep for 3 seconds (non-busy)
- sleep(3);
- $data = getrusage();
- echo “User time: “.
- ($data['ru_utime.tv_sec'] +
- $data['ru_utime.tv_usec'] / 1000000);
- echo “System time: “.
- ($data['ru_stime.tv_sec'] +
- $data['ru_stime.tv_usec'] / 1000000);
- /* 输出
- User time: 0.011552
- System time: 0
- */
- // loop 10 million times (busy)
- for($i=0;$i
- }
- $data = getrusage();
- echo “User time: “.
- ($data['ru_utime.tv_sec'] +
- $data['ru_utime.tv_usec'] / 1000000);
- echo “System time: “.
- ($data['ru_stime.tv_sec'] +
- $data['ru_stime.tv_usec'] / 1000000);
- /* 输出
- User time: 1.424592
- System time: 0.004204
- */
- $start = microtime(true);
- // keep calling microtime for about 3 seconds
- while(microtime(true) – $start
- }
- $data = getrusage();
- echo “User time: “.
- ($data['ru_utime.tv_sec'] +
- $data['ru_utime.tv_usec'] / 1000000);
- echo “System time: “.
- ($data['ru_stime.tv_sec'] +
- $data['ru_stime.tv_usec'] / 1000000);
- /* prints
- User time: 1.088171
- System time: 1.675315
- */
- // this is relative to the loaded script’s path
- // it may cause problems when running scripts from different directories
- require_once(‘config/database.php’);
- // this is always relative to this file’s path
- // no matter where it was included from
- require_once(dirname(__FILE__) . ‘/config/database.php’);
- // some code
- // …
- my_debug(“some debug message”, __LINE__);
- /* Output
- Line 4: some debug message
- */
- // some more code
- // …
- my_debug(“another debug message”, __LINE__);
- /* Output
- Line 11: another debug message
- */
- function my_debug($msg, $line) {
- echo “Line $line: $msgn”;
- }
- // generate unique string
- echo uniqid();
- /* Output
- 4bd67c947233e
- */
- // generate another unique string
- echo uniqid();
- /* Output
- 4bd67c9472340
- */
- // Prefix
- echo uniqid(‘foo_’);
- /* Output
- foo_4bd67d6cd8b8f
- */
- // There is more entropy
- echo uniqid(”,true);
- /* Output
- 4bd67d6cd8b926.12135106
- */
- // All
- echo uniqid(‘bar_’,true);
- /* Output
- bar_4bd67da367b650.43684647
- */
- // A complex array
- $myvar = array(
- ‘hello’,
- 42,
- array(1,’two’),
- ‘apple’
- );
- // Serialization
- $string = serialize($myvar);
- echo $string;
- /* Output
- a:4:{i:0;s:5:"hello";i:1;i:42;i:2;a:2:{i:0;i:1;i:1;s:3: "two";}i:3;s:5:"apple";}
- */
- //Desequential instantiation
- $newvar = unserialize($string);
- print_r($newvar);
- /* Output
- Array
- (
- [0] => hello
- [1] => 42
- [2] => Array
- (
- [0] => 1
- [1] => two
- )
- [3] => apple
- )
- */
- // a complex array
- $myvar = array(
- ‘hello’,
- 42,
- array(1,’two’),
- ‘apple’
- );
- // convert to a string
- $string = json_encode($myvar);
- echo $string;
- /* prints
- ["hello",42,[1,"two"],"apple"]
- */
- // you can reproduce the original variable
- $newvar = json_decode($string);
- print_r($newvar);
- /* prints
- Array
- (
- [0] => hello
- [1] => 42
- [2] => Array
- (
- [0] => 1
- [1] => two
- )
- [3] => apple
- )
- */
- $string =
- "The truth is that the pain itself is good, it will be successful.
- customer service. Now let's get it out of my hands
- coaching There is no easy way. It's a pillow,
- wisdom or flight of the vestibule, I will not give the price of the hospital,
- He did not raise the lake as much as before. Thank you very much.
- let it be good, it will be able to attract the customer. Some
- the price of any body that is targeted. Yes, and mass
- but it was an ugly time of mourning. I'm sorry but I'm sorry
- soft homework It is the very day, it will be the result of life
- to decorate a, something from now In that great and pushing
- to lay the ground But not my fear, but Lacinia
- advertise But unless it's big, decorate it in soft, soft
- but now. Even at just the right time for homework.
- There is no need to fear chocolate and chocolate
- not football. To drink the unsavory lake of football
- that euismod urn members “;
- $compressed = gzcompress($string);
- echo "Original size:". strlen($string).”n”;
- /* output original size
- Original size: 800
- */
- echo "Compressed size:". strlen($compressed)."n";
- /* output 剧情后 size
- Compressed size: 418
- */
- // 解剧情
- $original = gzuncompress($compressed);
原文出处:8个安全必备的PHP function

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software