Common functions in PHP

Source: Internet
Author: User

This article mainly introduces PHP commonly used functions, interested in the friend's reference, I hope to be helpful to everyone.

Array_intersect ()

Compares the key values of two arrays and returns the intersection:

<?php$a1=array ("A" = "red", "b" = "green", "c" = "Blue", "d" = "yellow"), $a 2=array ("e" = "red", "f" = > "Green", "g" = "blue"), $result =array_intersect ($a 1, $a 2);p Rint_r ($result);? >result:array ([A] = red [b] = green [c] = blue)

Array_keys () function

Returns a new array that contains all the key names in the array.

<?php$a=array ("Volvo" = "XC90", "BMW" and "X5", "Toyota" and "Highlander");p Rint_r (Array_keys ($a)); >result:array ([0] = Volvo [1] = BMW [2] = Toyota)

Array_key_exists () function

Checks whether the specified key name exists in an array, returns true if the key name exists, or False if the key name does not exist.

<?php    $a =array ("Volvo" = "XC90", "BMW" = "X5");        if (array_key_exists ("Volvo", $a))      {      echo "key exists!) ";      }    else      {      echo ' key does not exist! ";      }? >result: Key exists!

Array_merge () function

Combine two arrays into an array:

<?php$a1=array ("Red", "green"), $a 2=array ("Blue", "yellow");p Rint_r (Array_merge ($a 1, $a 2));? >result:array ([0] = + red [1] = green [2] = = Blue [3] = yellow)

Array_reverse ()

Returns an array in the reverse order of elements:

<?php$a=array ("A" = "Volvo", "B" and "BMW", "c" = "Toyota");p Rint_r (Array_reverse ($a));? >result:array ([c] = Toyota [b] = BMW [a] = Volvo)

Array_unshift () function

Used to insert a new element into an array. The value of the new array is inserted at the beginning of the array.

<?php$a=array ("A" = "red", "B" and "green"); Array_unshift ($a, "blue");p rint_r ($a); >result:array ([0] = blue [A] = red [b] = green)

Array_values

Returns an array that contains all the key values in the given array, but does not retain the key name.

<?php$a=array ("Name" = "Bill", "age" = "country" and "USA");p Rint_r (array_values ($a));? >result:array ([0] = Bill [1] [2] = USA)

Hash_equals

String comparisons to prevent timing attacks
Compares two strings, regardless of whether they are equal, the time consumption of this function is constant.
This function can be used in a string comparison scenario where you need to prevent timing attacks, for example, a scenario where you can compare a crypt () password hash value.

BOOL Hash_equals (String $known _string, string $user _string)

Parameters:
Known_string
string of known length, to participate in the comparison
User_string
User-supplied string

return value:
Returns TRUE when two strings are equal, otherwise FALSE.

<?php$expected  = crypt (' 12345 ', ' $2a$07$usesomesillystringforsalt$ '); $correct   = Crypt (' 12345 ', ' $2a$07$ usesomesillystringforsalt$ '); $incorrect = Crypt (' Apple ',  ' $2a$07$usesomesillystringforsalt$ '); Var_dump (Hash_ Equals ($expected, $correct)); Var_dump (Hash_equals ($expected, $incorrect));? >result:bool (TRUE) bool (false)

In_array () function

Searches the array for the presence of the specified value.

<?php$people = Array ("Bill", "Steve", "Mark", "David"), if (In_array ("Mark", $people))  {  echo "match found";  } else  {  echo "match not Found";  }? >result: Match found

sprintf () function

Replace the percent sign (%) with a variable that is passed as a parameter:

<?php    $number = 2;    $STR = "Shanghai";        $txt = sprintf ("There is%u million cars in%s.", $number, $str);        Echo $txt;? >    Result:there is 2 million cars in Shanghai.

Str_ireplace ()

Replace some characters in a string (case insensitive) str_ireplace (Find,replace,string,count)

<?phpecho Str_ireplace ("World", "Shanghai", "Hello world!");? >result:hello shanghai!

Strpos

Finds the position of the first occurrence of a string in another string.

<?phpecho Strpos ("You love php, I love php too!", "PHP"); >result:9

Str_replace ()

Replace some characters in a string with other characters (case sensitive)

<?phpecho Str_replace ("World", "Shanghai", "Hello world!");? >result:hello shanghai!

str_ireplace ()
Replace some characters in a string (case insensitive)
Str_ireplace (Find,replace,string,count)

<?phpecho Str_ireplace ("World", "Shanghai", "Hello world!");? >result:hello shanghai!

Substr

Returns a portion of a string.

<?phpecho substr ("Hello World", 6);? >result:world
Related recommendations:
Ajax functional functions and usage examples in pure JS encapsulation
Promise how to replace the callback function in the code

Promise how to replace the callback function in the code

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.