PHP trivial knowledge points (sorting) and php knowledge points

Source: Internet
Author: User
Tags gmp rtrim

PHP trivial knowledge points (sorting) and php knowledge points

PHP does not check variable interpolation in the single quotes ''string or (almost) any escape sequence, so using single quotes is quite simple and quick to define the string. Otherwise, php checks the variables or escape sequences in the string and outputs the values of the variables and escape sequences.

$a = "123";print '$a\t';print '$a';

Output:

$a\t$a

Double quotation marks "":

$a = "123";print "$a\t";print "$a";

Output:

Note:

Single quotes ''can be used to interpret the escape characters '\ and!

The single quotation mark character can use single quotation marks whenever possible. The efficiency of single quotation marks is higher than that of double quotation marks (because double quotation marks must be traversed first to determine whether there is any variable in it, and then perform operations, single quotes do not need to be determined ).

Single quotation marks ''and double quotation marks" "can be used

Differences between echo and print:

Both statements are statements, not functions. Both statements are used to output strings. However:

Echo can input multiple parameters. Print only has one:

Echo "123", "123"; // output 123123 print "123", "123"; // an error is returned. Only one parameter can be set to print "123 ";

Echo does not return values, while print returns 1;

Note:

In PHP, The octal data type, except the array and the objects that do not implement the _ toString magic function, can be Output Using echo or print, and the boolean type can be Output Using echo or print, only 1 is displayed or not.

Echo true; // output 1 echo false; // No output

Number and string addition:

PHP automatically converts strings and numbers, which sometimes brings benefits and is sometimes annoying.

Echo 1 + "2"; // output 3 echo 1 + "a"; // output 1

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

If (is_numeric ($ a) {echo "is an integer";} else {echo "is another type";} // The final output is an integer.

The difference between the random number generation function rand () and mt_rand:

The usage of rand () and mt_rand () is exactly the same. They have two usage methods:

// First usage: rand (); // The generated random number is mt_rand () between 0 and getrandmax (); // The generated random number is 0 to mt_getrandmax () // second usage: rand ($ min, $ max); // generates a random number mt_rand ($ min, $ max) from $ min to $ max ); // generate a random number from $ min to $ max

Difference: mt_rand () is a better random number generator, because it broadcast a better random number seed than rand (), and the performance is 4 times faster than rand (), mt_getrandmax () the value range is larger.

Difference between BCMath and GMP:

The BCMath library is easy to use. Input a number as a string to the function. It returns the sum (or difference, product, etc.) of the number as a string. However, when BCMath is used, only basic arithmetic operations can be performed on numbers.

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

GMP functions can take integers or strings as parameters, but they are more willing to pass numbers as resources, which actually points to internal pointers of numbers. Therefore, unlike the BCMath function, the BCMath function returns a string, while the GMP function only returns resources. This resource can be passed as a number to any GMP function.

$ Four = gmp_add (2, 2); // You can input an integer $ eight = gmp_add ('4', '4 '); // or string $ twelve = gmp_add ($ four, $ eight); // or GMP Resource

The only drawback of GMP is that gmp_strval () or gmp_intval () must be used for display conversion when you want to use non-GMP functions to view or use resources.

Note:

BCMath and PHP are bundled for release. If GMP is not bundled with PHP, You need to download and install it separately. Another option for completing high-precision mathematical operations is to use the big_int library of PECL.

Difference between include and require:

Include () and require () statements include and run the specified file. The two structures are exactly the same on the contained files, and the only difference is the handling of errors:

• When the require () Statement encounters an error or the contained file does not exist, it stops and returns an error.
• When the contained file does not exist, only warnings are generated and the script continues.

In other words, if you want to stop processing the page when the file is lost, do not hesitate to use require. This is not the case with include (). The script will continue to run.

Include_once and require_once

• The same as include_once () and require_once () should be used when the same file may be contained more than once during script execution, make sure it is included only once to avoid the problem of function redefinition and variable re-assignment. This is the main difference between include_once () and require_once () and include () and require.
• Require_once () and include_once () run less efficiently than require () and include (), because the first two must determine whether the introduced file already exists. '

PHP merge array + and array_merge ()

The same as array merging, but there is still a difference:

• When the key name is a number, array_merge () will not overwrite the original value, but + merges the array and returns the first value as the final result, the values of the following arrays with the same key names are discarded (not overwritten)

$ A = array ('A', 'B'); $ B = array ('C', 'D'); $ c = $ a + $ B; var_dump ($ c); // output: // array (size = 2) // 0 => string 'A' (length = 1) // 1 => string 'B' (length = 1) var_dump (array_merge ($ a, $ B); // output: // array (size = 4) // 0 => string 'A' (length = 1) // 1 => string 'B' (length = 1) // 2 => string 'C' (length = 1) // 3 => string 'D' (length = 1)

• When the key name is a character, + still returns the value of the first key name as the final result, and "discard" the values with the same key name in the subsequent array, however, array_merge () overwrites the values with the same key name.

$ A = array ('A' => 'A', 'B' => 'B'); $ B = array ('A' => 'A ', 'B' => 'B'); $ c = $ a + $ B; var_dump ($ c); // output: // array (size = 2) // 'A' => string 'A' (length = 1) // 'B' => string 'B' (length = 1) var_dump (array_merge ($, $ B); // output: // array (size = 2) // 'A' => string 'A' (length = 1) // 'B' => string 'B' (length = 1)

String common functions

PHP provides many convenient string functions, commonly used:

• Strstr (string $ haystack, mixed $ needle [, bool $ before_needle = false]). Returns the haystack string from the position where the needle first appeared to the end of the haystack. If before_needle is TRUE, strstr () returns the part before the position of needle in the haystack.
• Substr (string $ string, int $ start [, int $ length]). Returns the substring specified by the start and length parameters.
• Substr_replace (mixed $ string, mixed $ replacement, mixed $ start [, mixed $ length]). Substr_replace () Replace the substring specified by the start and optional length parameters with replacement in the string copy.
• Strrev (string $ string ). Returns the string after string inversion.
• Str_replace (mixed $ search, mixed $ replace, mixed $ subject [, int & $ count]). This function returns a string or array. This string or array is the result after all the searches in the subject are replaced by replace. Subject is an array or string to be replaced. That is, haystack. If the subject is an array, the replacement operation traverses the entire subject, and the return value is also an array. If count is specified, its value is set to the number of times it is replaced.
• Strpos (string $ haystack, mixed $ needle [, int $ offset = 0]). Returns the position of the number that needle first appeared in the haystack. If the offset parameter is provided, the search starts from the start position of the string. If it is a negative number, the search starts with the specified number of characters at the end of the string.
• Ltrim (), rtrim (), and trim (). This Delimiter is used to delete the blank characters in the string. Ltrim () removes white spaces starting with a string, rtrim () removes white spaces at the end of the string, and trim () removes white spaces at the beginning and end of the string.

The above is a collection of trivial PHP knowledge points, I hope to help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.