


PHP common functions and common troubleshooting questions_PHP tutorial
首先介绍下比较简单但必不可少且实用的知识,可以当手册查询,适合像我一样的新手看。
PHP常用库函数介绍
一、PHP字符串操作常用函数
1.确定字符串长度
int strlen(string str)
2.比较两个字符串
a. strcmp函数对两个字符串进行二进制安全的比较,并区分大小写
int strcmp(string str1,string str2)
b. 以不区分大小写的方式比较两个字符串
int strcasecmp(string str1,string str2)
3.求两个字符串相同部分
int strspn(string str1,string str2)
4.求两个字符串的不同部分
5.int strcspn(string str1,string str2)
6.处理字符串大小写
a. 将字符串全部转换为小写
string strtolower(string str)
b. 将字符串全部转化为大写
string strtoupper(string str)
c. 将字符串第一个字符大写
string ucfirst(string str)
d. 把字符串中每个单词的首字符转换为大写
string ucwords(string str)
7.字符串与HTML相互转换
a. 将换行符转换为HTML终止标记
string bl2br(string str)
b. 将特殊字符转换wieldHTML等价形式(不解析格式)
string htmlentities(string str[,int quote_style[,int charset]])
string htmlspecialchars(string str[,int quote_style[,string charset]])
c. 将HTML转换为纯文本,移除所有的php和html标签
string strip_tags(string str[,string allowable_tags])
d. 将文本转换为HTML等价形式
array get_html_translaction_table(int table[,int quote_style])
e. 创建一个自定义的转换清单
string strtr(string str,array replacements)
8.正则表达式函数的替代函数
a. strtok函数根据预定义的字符串列表来解析字符串
string strtok(string str,string tokens):返回直到遇到tokens之前的所有内容
b. 根据预定义的定界符分析字符串
array explode(string separator,string str[,int limit]):分割字符串
c. 将数组转换为字符串
string implode(string delimiter, array array)
d. 找到字符串的第一次出现
int strpos(string str,string substr[,int offset])
e. 找到字符串的最后一次出现
int strrpos(string str,char substr[,offset])
f. 用另外一个字符串替代字符串的所有实例
mixed str_replace(string occurrence,mixed replacement,mixed str[,int count])
g. 获取字符串的一部分strstr返回字符串中预定义字符串第一次出现开始的剩余部分
string strstr(string str,string occurrence)
h. 根据预定义的偏移返回字符串一部分
string substr(string str,int start[,ing length]):start可为负数,表示倒数第几开始
i. 确定字符串出现的频率
int substr_count(string str,string substring)
j. 用另一个字符串替换一个字符串的一部分
string substr_replace(string str,string replacement,int start[,int length])
9.填充和剔除字符串
a. 从字符串开始出裁剪字符
string ltrim(string str[,string charliset])
b. 从字符串结尾裁剪字符
string rtrim(string str[,string charliset])
c. 从字符串两端裁剪字符
string trim(string str[,string charliset])
d. 填充字符串
string str_pad(string str,int length[,string pad_string[,int pad_type]])
10.字符和单词计数
a. 字符串中字符计数
mixed count_chars(string str[,mode])
b. 字符串中单词总数计数
mixed str_word_count(string str[,int format])
二、PHP Web开发中常用的三个表单验证函数
(1)isset();——适合于检测是否存在这个参数。用来避免引用不存在的变量
定义和作用范围:用于测试一个变量是否具有值(包括0,FALSE,或者一个空字串都返回true,但不能是NULL),即:“http://localhost/?fo=”也是可以通过检测,因此不适用。但如果是“http://localhost/”参数中并不含fo参数,就可以用isset来检测,此时isset($_GET['fo'])返回false
不适用于:该函数不适合于验证html表单中的文本的有效方式。要检查用户输入文本是否有效,可以用empty();
(2)empty();——最好用的一个函数,用于检查变量是否具有空值
定义和作用范围:用于检查变量是否具有空值:包括:空字串,0,null 或false,这些都返回false,即:“http://localhost/?fo=”或“http://localhost/?fo=0”时,empty检测出来的结果都是ture
不适用范围:不适用于检测可为0的参数
(3)is_numeric();——检查变量是否为数字
定义和作用范围:检查变量是否为数字,只适用于检测数字
不适用范围:但假如参数名不存在,会出错,因此不适合于第一层检测
Another useful verification function is checkdate(month, day, $year), which is used to confirm whether a certain date exists or existed in the past
Comprehensive example:
This is the form:
< ;title>Form validation example
Pass a valid value Pass an empty value Pass 0 Value
Gender: Male Gender: Female
Clear
[code]
This is verification
[code] ini_set("display_errors",1);
//ini_set("error_reporting",E_ALL); print_r
error_reporting(E_ALL);
$a=NULL;
if(isset($a))echo 'isset of variable $a is true';
echo '
isset situation:
';if( isset($_GET['fo'])){
echo 'The isset of variable 'fo' is true, the variable is available';
}else{
echo 'The isset of variable 'fo' is false, No variable setting';
}
echo '
empty situation:
';if(empty($_GET['fo'])){
echo 'The empty value of the variable 'fo' is true, that is, a null value or an invalid value';
}else{
echo 'The empty value of the variable 'fo' is false, and it has a value';
}
echo '
is_numeric case:
';if(is_numeric($_GET['fo'])){ //When there is no fo parameter in the parameter, then Something went wrong.
echo 'is_numeric of variable 'fo' is true and is a number';
}else{
echo 'is_numeric of variable 'fo' is false and is not a number';
}
echo "
$_GET['fo']='' situation:
";if($_GET['fo']==''){ //In the parameters If there is no fo parameter, an error occurs.
echo 'fo has no value, an empty string';
}elseif($_GET['fo']!=''){
echo 'fo has a value, not ''.';
}
echo "
$_GET['sex']='m' case:
";if($_GET['sex']= ='m'){ //An error occurs when there is no sex variable in the parameter.
echo 'male';
}elseif($_GET['sex']=='f'){
echo 'female';
}
?>
3. Other commonly used library functions
(1) ini_set ini_get - List of operable configuration parameters
In order to make our programs have better compatibility on different platforms, many times we have to obtain the current Php operating environment parameters.
For example, what we often use:
Get the magic_quotes_gpc status to decide whether we escape (addslashes) the data when the form is submitted;
Set max_execution_time to extend the execution time of the program;
Set error_reporting Make your project switch between the development and operation phases;
Set memory_limit to increase memory, etc...
(2) ini_set(string varname, string newvalue): //Set the parameters of the environment configuration
ini_get (string varname) : //Get the parameters of the environment configuration
The PHP ini_set function is to set the value in the option. It takes effect after the function is executed. When the script ends, this setting also becomes invalid. Not all options can be set by the function. For specific values

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

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.

Dreamweaver Mac version
Visual web development tools

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

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

WebStorm Mac version
Useful JavaScript development tools