Home Backend Development PHP Tutorial Summarize the scattered knowledge points in PHP

Summarize the scattered knowledge points in PHP

Aug 13, 2017 pm 02:36 PM
php Knowledge points

Today I will share with you the trivial knowledge points of PHP. It is very good and has reference value. Friends who need it can refer to it

PHP will not check single quotes'' Variable interpolation in strings Or (almost) any escape sequence, so defining a string using single quotes is fairly simple and fast. However, this is not the case with double quotes "". PHP will check the variables or escape sequences in the string and output the values ​​of the variables and escape sequences.


$a = "123";
print '$a\t';
print '$a';
Copy after login

Output:


##

$a\t$a
Copy after login

If it is a double quote "" :



$a = "123";
print "$a\t";
print "$a";
Copy after login

Output:

Note:

Single quotes'' can be interpreted '\ and \\ are the two escape characters, that's all!

can use single quotes as much as possible for single quote characters. Single quotes are more efficient than double quotes (because double quotes must be traversed first to determine whether there are variables in them before operating, while single quotes are not requires judgment).

Single quotes '' and double quotes "" can be used

The difference between echo and print:

This Both are statements, not functions; the function of these two statements is to output strings. However:

echo can pass in multiple parameters. There is only one print:



echo "123", "123";//输出123123
print "123", "123";//报错,只可以写一个参数 print "123";
Copy after login

echo has no return value, while the print return value is always 1;

Note :

The eight data types of PHP, except arrays and objects that do not implement the __toString magic function, can be output by echo or print, and boolean types can be output by echo or print. Output will only display 1 or not.



echo true; //输出1
echo false; //什么都不输出
Copy after login

Add numbers and strings:

PHP will automatically complete the string and digital conversion, which sometimes brings benefits, sometimes it is very distressing.



echo 1 + "2";//输出3
echo 1 + "a";//输出1
Copy after login

$a = 1 + "A"; What is the data type of variable $a?



if(is_numeric($a)){
  echo "是整型";
}
else{
  echo "是其他类型";
}
//最后输出:是整型
Copy after login

The difference between the random number generation function rand() and mt_rand(): ## The usage of #rand() and mt_rand() are exactly the same. They have two usages respectively:


//第一种用法:
rand();//产生的随机数为0到getrandmax()之间
mt_rand();//产生的随机数为0到mt_getrandmax()之间
//第二种用法:
rand($min, $max);//产生从$min到$max之间的随机数
mt_rand($min, $max);//产生从$min到$max之间的随机数
Copy after login

Difference: mt_rand() is a better random number Generator, because it sows a better random number seed than rand(); and the performance is 4 times faster than rand(), and the range of values ​​represented by mt_getrandmax() is also larger

The difference between the BCMath library and the GMP library: The BCMath library is easy to use. Pass numbers into the function as strings and it will return the sum (or difference, product, etc.) of the numbers as a string. However, when using BCMath, the operations that can be performed on numbers are limited to basic arithmetic operations.


$sum = bcadd("12345678", "87654321");//$sum = "99999999"
Copy after login

GMP functions can accept integers or strings as parameters, but they prefer to pass numbers as resources, which actually point to the internal representation of the number. pointer. So unlike the BCMath function, which returns a string, GMP only returns the resource. This resource can be passed as a number to any GMP function.


$four = gmp_add(2, 2);//可以传入整数
$eight = gmp_add('4', '4');//或字符串
$twelve = gmp_add($four, $eight);//或GMP资源
Copy after login

The only disadvantage of GMP is that when you want to view or use resources with non-GMP functions, you need to use gmp_strval() or gmp_intval() to explicitly convert .

Note BCMath is bundled with PHP. If GMP is not bundled with PHP, you need to download and install it separately. Another option for accomplishing high-precision math operations is to use PECL's big_int library.

The difference between include and require: include() and require() statements include and run the specified file. The two structures are exactly the same in the include files. The only difference is the error handling:


•require() statement will stop when it encounters that the included file does not exist or an error occurs. , and report an error.

•include() When encountering an included file that does not exist, it only generates a warning and the script continues.


In other words, if you want to stop processing the page when a file is missing, don't hesitate to use require(). This is not the case with include() and the script will continue to run.

include_once and require_once
•include_once() is the same as require_once() and should be used on the same file during script execution When it is possible to be included more than once, you want to ensure that it is only included once to avoid problems such as function redefinition and variable reassignment. This is the main difference between include_once() and require_once() and include() and require().

•require_once() and include_once() are less efficient than require() and include() because the first two need to determine whether the file to be imported already exists. `


The difference between PHP merging array + and array_merge()

同为数组合并,但是还是有差别的:

•键名为数字时,array_merge()不会覆盖掉原来的值,但+合并数组则会把最先出现的值作为最终结果返回,而把后面的数组拥有相同键名的那些值“抛弃”掉(不是覆盖)


$a = array('a','b'); 
$b = array('c', 'd'); 
$c = $a + $b; 
var_dump($c);
//输出:
// array (size=2)
// 0 => string 'a' (length=1)
// 1 => string 'b' (length=1) 
var_dump(array_merge($a, $b));
//输出:
//array (size=4)
// 0 => string 'a' (length=1)
// 1 => string 'b' (length=1)
// 2 => string 'c' (length=1)
// 3 => string 'd' (length=1)
Copy after login

•键名为字符时,+仍然把最先出现的键名的值作为最终结果返回,而把后面的数组拥有相同键名的那些值“抛弃”掉,但array_merge()此时会覆盖掉前面相同键名的值


$a = array('a' => 'a' ,'b' => 'b');
$b = array('a' => 'A', 'b' => 'B');
$c = $a + $b;
var_dump($c);
//输出:
//array (size=2)
//'a' => string 'a' (length=1)
//'b' => string 'b' (length=1)
var_dump(array_merge($a, $b));
//输出:
//array (size=2)
//'a' => string 'A' (length=1)
//'b' => string 'B' (length=1)
Copy after login

字符串常用函数

PHP提供了很多方便的字符串函数,常用的有:

•strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 。返回 haystack 字符串从 needle 第一次出现的位置开始到 haystack 结尾的字符串。若为before_needle为 TRUE,strstr() 将返回 needle 在 haystack 中的位置之前的部分。
•substr( string $string , int $start [, int $length ] ) 。返回字符串 string 由 start 和 length 参数指定的子字符串。
•substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] ) 。substr_replace() 在字符串 string 的副本中将由 start 和可选的 length 参数限定的子字符串使用 replacement 进行替换。
•strrev ( string $string ) 。返回 string 反转后的字符串。
•str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) 。该函数返回一个字符串或者数组。该字符串或数组是将 subject 中全部的 search 都被 replace 替换之后的结果。subject为执行替换的数组或者字符串。也就是 haystack。如果 subject 是一个数组,替换操作将遍历整个 subject,返回值也将是一个数组。如果count被指定,它的值将被设置为替换发生的次数。
•strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) 。返回 needle 在 haystack 中首次出现的数字位置;如果提供了offset参数,搜索会从字符串该字符数的起始位置开始统计。 如果是负数,搜索会从字符串结尾指定字符数开始。
•ltrim() 、 rtrim() 、 trim() 。这仨都是删除字符串中的空白符。 ltrim() 删除字符串开头的空白字符; rtrim() 删除字符串末端的空白字符; trim() 去除字符串首尾处的空白字符。 

The above is the detailed content of Summarize the scattered knowledge points in PHP. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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

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.

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.

See all articles