Home Backend Development PHP Tutorial PHP training tutorial 10 little-known but very useful functions in PHP

PHP training tutorial 10 little-known but very useful functions in PHP

Nov 07, 2016 pm 04:57 PM

php has a very rich set of built-in functions, many of which we have used, but there are still many functions that most of us are not familiar with, but they are very useful. In this article, the editor of Band of Brothers lists some PHP functions that are little known in PHP training but will make your eyes bright.

 levenshtein()

Have you ever experienced the time when you need to know how different two words are? This function is here to help you solve this problem. It can compare the difference between two strings.

Usage:

 
 $str1 = "carrot";

 $str2 = "carrrott";

 echo levenshtein($str1, $str2); //Outputs 2

     

 Source : http://php.net/manual/en/function.levenshtein.php

 get_defined_vars()

 This is a very useful function during debugging. This function returns a multidimensional array containing all defined variables.

Usage:


print_r(get_defined_vars());

?>

Source: http://php.net/manual/en/function.get-defined-vars.php

php_check_syntax ()

 This function is very useful and can be used to check whether the syntax of PHP is correct. For technical reasons, this function has been removed starting with PHP 5.05.

 Usage:

 
 $error_message = "";

 $filename = "./php_script.php";

 if(!php_check_syntax($filename, &$error_message)) {

 echo " Errors were found in the file $filename: $error_message";

  } else {

 echo "The file $filename contained no syntax errors";

 }

 ?>

  Source: http://www.php .net/manual/en/function.php-check-syntax.php

 ignore_user_abort()

 This function is used to reject the browser user's request to terminate the execution of the script. Under normal circumstances, the client's exit will cause the server-side script to stop running.

Usage:


ignore_user_abort();

?>

Source: http://www.php.net/manual/en/function.ignore-user-abort.php

highlight_string( )

 The highlight_string() function is very useful when you want to display PHP code on the page. This function will highlight the PHP code you provide using the color defined by the built-in PHP syntax highlighting. This function has two parameters. The first parameter is a string, indicating that this string needs to be highlighted. If the second parameter is set to TRUE, this function will return the highlighted code as the return value.

  Usage

  
  Highlight_string(' ');

  ?>

  Source: http://php.net/manual/en/function.highlight-string.php

  Highlight_file

 this It is a very useful PHP function that returns the specified PHP file and highlights the file content with highlight colors according to the syntax and semantics. The highlighted code is processed using HTML markup.

Usage:


highlight_file("php_script.php");

?>

Source: http://www.php.net/manual/en/function.highlight-file.php

  php_strip_whitespace

 This function is also similar to the previous show_source() function, but it will delete comments and spaces in the file.

Usage:


echo php_strip_whitespace("php_script.php");

?>

Source: http://www.php.net/manual/en/function.php-strip-whitespace .php

 get_browser

 This function will read the browsercap.ini file and return browser compatibility information.

Usage:

 
echo $_SERVER['HTTP_USER_AGENT'];

$browser = get_browser();

print_r($browser);

  ?>

 Source: http://www .php.net/manual/en/function.get-browser.php

 Memory_get_usage(), memory_get_peak_usage(), getrusage()

 These functions are used to obtain memory and CPU usage. The memory_get_usage() function returns the memory usage. , the memory_get_peak_usage() function returns the peak memory usage, and getrusage() returns the CUP usage. These functions will provide you with some useful information when debugging PHP code performance. But please note that these functions are not valid on Windows.

Usage:


echo "Initial: ".memory_get_usage()." bytes n";

echo "Peak: ".memory_get_peak_usage()." bytes n";

  $data = getrusage();

  echo "User time: ". ['ru_utime.tv_usec'] / 1000000);

 echo "System time: ".

 ($data['ru_stime.tv_sec'] +

 $data['ru_stime.tv_usec'] / 1000000);

?>

 gzcompress(), gzuncompress()

 These two functions are used to compress and decompress string data. Their compression rate can reach about 50%. The other functions gzencode() and gzdecode() can achieve similar results, but use different compression algorithms.

Usage:

 
 $string =

 "Lorem ipsum dolor sit amet, consectetur
 adipiscing elit. isi. Praesent pulvinar,

 sapien vel feugiat vestibulum, nulla dui pretium orci,

 non ultricies elit lacus quis ante. Lorem ipsum dolor

 sit amet, consectetur adipiscing elit. Curabitur sed nibh Sed magna nisi, ornare in mollis in, mollis

 sed nunc. Etiam at justo in leo congue mollis.

 $compressed = gzcompress($string);

 $original = gzuncompress($compressed);

  ?>

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

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.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

See all articles