PHP function operations

Source: Internet
Author: User

PHP function operations

<? Php

// Simple functions
Function show (){
Echo "hello ";
}
Show ();

// Functions with Parameters
Function show ($ ){
Echo "$ ";
}
Show ("world ");

// Functions with returned values
Function show (){
Return "Small V, hello! ";
}
Echo show ();


Function show ($ a, $ B ){
Return $ a + $ B;
}
Echo show (10, 5 );

// Variable parameter functions (special usage)
Function sum (){
$ Arr = func_get_args ();
$ Sum = 0;
For ($ I = 0; $ I <count ($ arr); $ I ++ ){
$ Sum + = $ arr [$ I];
}
Echo $ sum;
}
Sum (1, 2, 5 );

// Common functions
Echo rand (); // obtain a random number
Echo time (); // obtain the current Timestamp
Echo date ("Y-m-d H: I: s", time (); // format the display time
Echo strtotime ("10:00:00") // convert the time to the timestamp


// String functions
$ S = "Small V, hello! ";
$ S1 = "hello world ";
Echo strlen ($ s1 );
Echo strtolower ($ s1 );
Echo strtoupper ($ s1 );

// Split the string
$ Str = "hello | mike | nice | 159 ";
Var_dump (explode ("|", $ str ));


// Merge strings
$ Arr = [2, 5, 9, 6, 3];
Echo implode ($ arr );


// Replace the string
Echo substr_replace ($ str, "###", 16,3 );
Echo str_replace ("|", "@", $ str );


// Truncate the string
Var_dump (substr ($ str, 10, 4 ));

// Regular Expression
Separator:/start/end
^ Match start with $ match end
* The previous expression can appear n times + the previous expression appears at least once.
? The preceding expression can appear for times.
{N} matches n times {n,} At least appears n times at most {n, m} At Least n times at most m times
X | y or [abc] matches any one of them [a-z] matches any one between a and z.
\ D any number \ S any visible character \ w any word character including underline


$ S = a1b2c3d4e5;
Echo preg_replace ("/\ d/", "*", $ s); // replace
Var_dump (preg_split ("/\ d/", $ s); // split

// Match the first regular type
$ S = file_get_contets (); // get the content
$ Arr = array ();
Preg_match ("", $ s, $ arr );
?>

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.