Common PHP functions, php_PHP tutorial

Source: Internet
Author: User
Common PHP functions: php. Common functions of PHP: variables declared by php1.PHP string or (single quotation marks are generally used because it is easier to write) $ strHelloPHP; echo $ str; common functions of strpos PHP, php
1. PHP string
  • String declaration variable = ''or" "(single quotation marks are generally used, because it is easier to write)
$str = 'Hello PHP';echo $str;
  • Strpos calculates the position of a character in a string (starting from 0)
$ Str = 'Hello php'; echo strpos ($ str, 'o'); // calculate the position of the character in the string echo'
'; Echo strpos ($ str, 'Ph ');
  • Substr truncated string
$ Str = 'Hello php'; // capture string $ str1 = substr ($ str, 2, 3); // start from 2, truncate a string of 3 echo $ str1;
If the length parameter is not input, it is intercepted from the specified position until the end of the string.
  • Str_split: splits a string with a fixed length (the default length is 1)
$ Str = 'Hello php'; // The split string $ result = str_split ($ str); // Save the result to an array print_r ($ result ); // input an array echo using print_r'
'; $ Result1 = str_split ($ str, 2); print_r ($ result1 );
  • Explode (delimiter, string to be split) is separated by space
$str = 'Hello PHP Java C# C++';$result = explode(' ',$str);print_r($result);
  • String connection
$ Str = 'Hello PHP Java C # C ++ '; // string join $ num = 100; $ str1 = $ str .'
Objective-C '. $ num; echo $ str1; echo'
'; $ Str2 = "$ str
Objective-C $ num "; // A simple echo $ str2 statement;

2. PHP array
  • Array declaration PHP array is very dynamic and can store all types of data
// Array declaration method $ arr = array (); $ arr [0] = 'hello'; $ arr [1] = 'world '; $ arr [2] = 2; $ arr [3] = 3.14; print_r ($ arr );
  • Array_push () // add an element
  • Array_pop () // delete the last element
$arr = array();for($i=0;$i<100;$i++){    array_push($arr,'Item'.$i);}print_r($arr);
  • Store data using Key-Value pairs
$arr = array();$arr['H'] = 'Hello';$arr['W'] = 'World';print_r($arr);echo '
';echo $arr['H'];
Output: Array ([H] => Hello [W] => World) Hello
  • Not only can PHP Array be used as an Array, but also as a Map
  • Array initialization
$ Arr = array ('H' => 'hello', 'W' => 'world'); // initialize print_r ($ arr) for the PHP array );
  • Array icons and key-value pairs can exist at the same time.
$arr = array(0=>'Make',1=>'Tom','H'=>'Hello','W'=>'World');print_r($arr);echo '
';echo $arr[0];echo '
';echo $arr['W'];
Output: Array ([0] => Make [1] => Tom [H] => Hello [W] => World) Make World

Lifecycle 1. PHP string declaration variable = ''or" "(single quotation marks are generally used because it is easier to write) $ str = 'Hello php'; echo $ str; strpos...

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.