Home Backend Development PHP Tutorial Summary of common PHP functions and function libraries

Summary of common PHP functions and function libraries

Jun 27, 2017 am 11:30 AM
php function Summarize

First of all, I will introduce some relatively simple but essential and practical knowledge. It can be used as a manual for reference and is suitable for novices like me.

Introduction to common PHP library functions

1. Common functions for PHP string operations
1. Determine the string length
int strlen(string str)
2. Compare two strings
a. The strcmp function performs a binary-safe comparison of two strings and is case-sensitive
int strcmp(string str1,string str2)
b. Compare two strings in a case-insensitive manner
int strcasecmp(string str1,string str2)

3.Find the same parts of two strings
int strspn(string str1,string str2)
4. Find the different parts of two strings
5.int strcspn(string str1,string str2)
6. Handle the case of strings
a. Convert all strings to lowercase
string strtolower(string str)
b. Convert all strings to uppercase
string strtoupper(string str)
c. Capitalize the first character of the string
string ucfirst(string str)
d. Convert the first character of each word in the string to uppercase
string ucwords(string str)
7. Convert strings to HTML
a. Convert newlines to HTML termination tags
string bl2br(string str)
b. Convert special characters to wildHTML equivalents (no format parsing)
string htmlentities(string str[,int quote_style[ ,int charset]])
string htmlspecialchars(string str[,int quote_style[,string charset]])
c. Convert HTML to plain text, remove all php and html Tags
string strip_tags(string str[,string allowable_tags])
d. Convert text to HTML equivalent
array get_html_translaction_table(int table[,int quote_style])
e. Create a self- Defined conversion list
string strtr(string str,array replacements)
8.Regular expressionReplacement function of function
a. The strtok function parses according to a predefined string list String
string strtok(string str,string tokens): Returns all content until tokens are encountered
b. Analyze string according to predefined delimiters
array explode(string separator,string str [,int limit]): Split string
c. Convert array to string
string implode(string delimiter, array array)
d. Find the first occurrence of string
int strpos(string str,string substr[,int offset])
e. Find the last occurrence of the string
int strrpos(string str,char substr[,offset])
f. Use another character String replacement for all instances of string
mixed str_replace(string occurrence,mixed replacement,mixed str[,int count])
g. Get a part of the string strstr returns the first occurrence of a predefined string in the string The remaining part of the beginning
string strstr(string str,string occurrence)
h. Return a part of the string according to the predefined offset
string substr(string str,int start[,ing length]): start Can be a negative number, indicating the beginning of the countdown
i. Determine the frequency of occurrence of a string
int substr_count(string str,string substring)
j. Replace part of a string with another string
string substr_replace(string str,string replacement,int start[,int length])
9. Filling and removing strings
a. Cut characters from the beginning of the string
string ltrim(string str[,string charliset])
b. Trim characters from the end of the string
string rtrim(string str[,string charliset])
c. Trim characters from both ends of the string
string trim(string str[,string charliset])
d. Fill string
string str_pad(string str,int length[,string pad_string[,int pad_type]])
10.Character and word count
a. Count of characters in the string
mixed count_chars(string str[,mode])
b. Count of total words in the string
mixed str_word_count(string str[,mode] int format])
2. Three form validation functions commonly used in PHP Web development

(1) isset(); - suitable for detecting whether this parameter exists. Used to avoid referencing non-existent variables

Definition and scope: used to test whether a variable has a value (including 0, FALSE, or an empty string returns true, but cannot be NULL), that is: "http://localhost/?fo=" also passes the test, so it is not applicable. But if the "http://localhost/" parameter does not contain the fo parameter, you can use isset to detect it. At this time, isset($_GET['fo']) returns false

does not apply to: This function is not suitable for efficient way of validating text in html form. To check whether the user input text is valid, you can use empty();

(2)empty(); - the best function to check whether the variable has a null value

Definition and scope: used to check whether the variable has a null value: including: empty string, 0, null or false, these all return false, that is: "http://localhost/?fo=" or "http:// localhost/?fo=0", the results detected by empty are all true

Not applicable: not applicable to detecting parameters that can be 0

(3)is_numeric();——检查变量是否为数字

定义和作用范围:检查变量是否为数字,只适用于检测数字

不适用范围:但假如参数名不存在,会出错,因此不适合于第一层检测

另外还有一个好用的验证函数是checkdate(month,day,$year),用来确认某个日期是否存在或在过去是否存在

综合示例:

这是表单:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>表单验证示例</title>
</head>
<body>
</body>
<p>
<a href="?fo=Jack">传有效值</a> <a href="?fo=">传空值</a> <a href="?fo=0">传0值</a>
<br /><br />
<a href="?sex=m">性别:男</a> <a href="?sex=f">性别:女</a>
<br /><br />
<a href="/">清空</a>
<br /><br />
<input type="text" value="<?php echo $_GET[&#39;fo&#39;]!=&#39;&#39;?$_GET[&#39;fo&#39;]:&#39;&#39;;?>" size="155" />
</p>
</html>[code]
这是验证
[code]<?php
ini_set("display_errors",1);
//ini_set("
error_reporting
",E_ALL); print_r
error_reporting(E_ALL);
$a=NULL;
if(isset($a))echo &#39;变量$a的isset为真&#39;;
echo &#39;<h2>isset的情形:</h2>&#39;;
if(isset($_GET[&#39;fo&#39;])){
echo &#39;变量\&#39;fo\&#39;的isset为真,变量可用&#39;;
}else{
echo &#39;变量\&#39;fo\&#39;的isset为假,无变量设置&#39;;
}
echo &#39;<h2>empty的情形:</h2>&#39;;
if(empty($_GET[&#39;fo&#39;])){
echo &#39;变量\&#39;fo\&#39;的empty为真,即空值或无效值&#39;;
}else{
echo &#39;变量\&#39;fo\&#39;的empty为假,有值&#39;;
}
echo &#39;<h2>is_numeric的情形:</h2>&#39;;
if(is_numeric($_GET[&#39;fo&#39;])){ //在参数中无fo参数时,则出错。
echo &#39;变量\&#39;fo\&#39;的is_numeric为真,是数字&#39;;
}else{
echo &#39;变量\&#39;fo\&#39;的is_numeric为假,不是数字&#39;;
}
echo "<h2>\$_GET[&#39;fo&#39;]=&#39;&#39;的情形:</h2>";
if($_GET[&#39;fo&#39;]==&#39;&#39;){ //在参数中无fo参数时,则出错。
echo &#39;fo无值,空的字符串&#39;;
}elseif($_GET[&#39;fo&#39;]!=&#39;&#39;){
echo &#39;fo有值,不为\&#39;\&#39;.&#39;;
}
echo "<h2>\$_GET[&#39;sex&#39;]=&#39;m&#39;的情形:</h2>";
if($_GET[&#39;sex&#39;]==&#39;m&#39;){ //当参数中无sex变量时就会出错。
echo &#39;男的&#39;;
}elseif($_GET[&#39;sex&#39;]==&#39;f&#39;){
echo &#39;女的&#39;;
}
?>
Copy after login

三、其他常用库函数

(1)ini_set ini_get——可操作配置参数列表
为了使自己的程序在不同的平台中拥有更好的兼容性,很多时候我们都要获取当前Php的运行环境参数。
比如我们常用到的:
获取 magic_quotes_gpc 状态,来决定当表单提交时我们是否转义(addslashes)数据;
设定 max_execution_time 来延长程序的执行时间;
设定 error_reporting 使自己的项目在开发与运营阶段切换;
设定 memory_limit 加大内存等等…
(2)ini_set(string varname, string newvalue ) : //设定环境配置的参数
ini_get(string varname) : //获取环境配置的参数
PHP ini_set函数是设置选项中的值,在执行函数后生效,脚本结束的时候,这个设置也失效。不是所有的选项都能被改函数设置的。具体那些值能够设置,可以查看手册中的列表
其实你把PHP ini_set函数和ini_get结合使的话,非常好。比如你想在配置文件里添加自己的包含文件路径,但是你有没有权限更改php.ini,那么你可以结合两个函数:
ini_set ( 'include_path' , ini_get ( 'include_path' ). ':/your_include_dir:' );
(3)chdir(dirname(FILE)); //切换到global.php所在目录
(4)ob_start(‘ui_handler');//设置输出缓冲区句柄为ui_handler,即系统首页面为ui_handler函数所定义的内容
(5)int intval(mixed var, int [base]);
本函数可将变量转成整数类型。可省略的参数 base 是转换的基底,默认值为 10。转换的变量 var 可以为数组或类之外的任何类型变量。
(6)error_reporting(report_level) 函数—— 设置 PHP 的报错级别并返回当前级别
其中report_level可取值为0、1、2、4、8、16、32、……、4096、8191
例子:任意数目的以上选项都可以用“或”来连接(用 OR 或 |),这样可以报告所有需要的各级别错误。例如,下面的代码关闭了用户自定义的错误和警告,执行了某些操作,然后恢复到原始的报错级别:

<?php
//禁用错误报告
error_reporting(0);
//报告运行时错误
error_reporting(E_ERROR | E_WARNING | E_PARSE);
//报告所有错误
error_reporting(E_ALL);
?>
Copy after login

The above is the detailed content of Summary of common PHP functions and function libraries. For more information, please follow other related articles on the PHP Chinese website!

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

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.

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.

See all articles