PHP common functions and common troubleshooting questions_PHP tutorial
WBOY
Release: 2016-07-13 10:37:08
Original
1088 people have browsed it
首先介绍下比较简单但必不可少且实用的知识,可以当手册查询,适合像我一样的新手看。
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开发中常用的三个表单验证函数
[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 that can be set, you can check the list in the manual In fact, it is very good if you combine the PHP ini_set function with ini_get. For example, if you want to add your own include file path to the configuration file, but do you have permission to change php.ini, then you can combine two functions: ini_set ( 'include_path' , ini_get ( 'include_path' ). ': /your_include_dir:' ); (3)chdir(dirname(__FILE__)); //Switch to the directory where global.php is located (4)ob_start('ui_handler');//Set the output buffer handle to ui_handler, that is, the content defined for the ui_handler function on the system home page (5) int intval (mixed var, int [base]); This function can convert variables into integer types. The omitted parameter base is the base of the conversion, with a default value of 10. The converted variable var can be any type variable except an array or class. (6) error_reporting(report_level) function - Set the error reporting level of PHP and return the current level The possible values of report_level are 0, 1, 2, 4, 8, 16, 32,..., 4096, 8191 Example: Any number of the above options can be "or" connected (using OR or |), so that all required error levels can be reported. For example, the following code turns off user-defined errors and warnings, performs certain operations, and then returns to the original error level:
Copy code Code As follows:
//Disable error reporting
error_reporting(0);
//Report runtime errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
//Report all errors
error_reporting(E_ALL);
?>
Frequently Asked Questions 1. The difference between rand(min,max) and mt_rand(min,max). If the optional parameters min and max are not provided, mt_rand() returns a pseudo-random value between 0 and RAND_MAX. number. For example, if you want a random number between 5 and 15 (inclusive), use mt_rand(5, 15). Many old libc random number generators have some uncertain and unknown characteristics and are slow. PHP's rand() function uses the libc random number generator by default. The mt_rand() function is informally used to replace it. This function uses the known features of Mersenne Twister as a random number generator, which can generate random values on average four times faster than rand() provided by libc. 2. The difference and use of PHP echo, print, print_r, printf, sprintf and var_dump functions 1) echo echo() is not actually a function, it is a php statement, so you do not need to use it brackets. However, if you wish to pass more than one argument to echo(), a parsing error will occur using parentheses. Moreover, echo returns void and does not return a value, so it cannot be used to assign values. Example:
Copy code The code is as follows:
echo "55nav"; // 55nav echo ("55nav"); // 55nav echo ("55nav","com"); //Error occurred, cannot be used if there are parentheses Pass multiple parameters echo "55nav"," com"," is", " web"; // When brackets are not used, you can use commas to separate multiple values, and 55nav com is web will be output echo " 55nav is good web."; // Regardless of line breaks, the final display will be one line. 55nav is good web. echo "$fistname com"; // If $firstname = "55nav", 55nav com will be output. echo '$firstname com'; // Due to the use of single quotes, the value of $firstname will not be output, but $firstname com12 ?>
2) print The usage of print() and echo() is the same, but the echo speed is a little faster than print. It's actually not a function either, so you don't need to use parentheses on it. However, if you wish to pass more than one argument to print(), a parsing error will occur using parentheses. Note that print always returns 1, which is different from echo, that is, you can use print to assign values, but it has no practical significance. Example:
Copy code The code is as follows:
echo $a; // The value of $a is 14 ?>;
3) print_r function - prints the value of a predefined variable The print_r function prints easy-to-understand information about the variable. Syntax: mixed print_r (mixed $expression [, bool return]) If the variable is string, integer or float, its value will be output directly. If the variable is an array, a formatted value will be output. Arrays are easy to read, that is, the format corresponding to keys and values. The same is true for object objects. print_r has two parameters, the first is a variable, and the second can be set to true. If set to true, a string will be returned, otherwise a Boolean value TRUE will be returned. Example:
Copy code The code is as follows:
$c = print_r($a); echo $c; // The value of $c is TRUE $c = print_r($a, ture); echo $c; // $c The value is the string 55nav ?>
4) printf function The printf function returns a formatted string. Syntax: printf(format,arg1,arg2,arg++) Parameter format is the format of conversion, starting with the percent sign ("%") and ending with the conversion character. The following are possible format values: * %% – Returns the percent sign * %b – Binary number * %c – Character according to the ASCII value * %d – Signed decimal number * %e - Continuous notation (such as 1.5e+3) * %u - Unsigned decimal number * %f - Floating point number (local settings aware) * %F - Float Points (not local settings aware) * %o – Octal number * %s – String * %x – Hexadecimal number (lowercase letters) * %X – Sixteen Arguments such as base numbers (capital letters) arg1, arg2, arg++ will be inserted into the main string at the percent sign (%) symbol. The function is executed step by step, at the first % sign, arg1 is inserted, at the second % sign, arg2 is inserted, and so on. If there are more % symbols than arg arguments, you must use placeholders. The placeholder is inserted after the % sign and consists of a number followed by "$". You can use numbers to specify the displayed parameters. See the example for details. Example:
Copy code The code is as follows:
printf("My name is %2$s %1$s","55nav", "com"); // before s Add 1$ or 2$... to indicate the position where the following parameters are displayed. This line displays My name is com 55nav ?>
5) sprintf function How to use this function Like printf, the only difference is that this function writes the formatted string into a variable instead of outputting it. Example:
Copy codeThe code is as follows:
$out = sprintf("My name is %1$s %2$s","55nav", " com"); echo $out; //Output My name is 55nav com ?>
6) var_dump function Function: Output the content, type or string of the variable Content, type, length. Often used for debugging. Example:
http://www.bkjia.com/PHPjc/736795.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/736795.htmlTechArticleFirst of all, I will introduce the relatively simple but essential and practical knowledge. It can be used as a manual for query, suitable for people like me. Watch for newbies. Introduction to common PHP library functions 1. Common functions for PHP string operations...
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