Home >Backend Development >PHP Tutorial >PHP common functions, php_PHP tutorial

PHP common functions, php_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:05:221050browse

PHP common functions, php

1.PHP string
  • String declaration variable ='' or "" (usually single quotes are used because it is more convenient to write)
<span>$str</span> = 'Hello PHP'<span>;
</span><span>echo</span> <span>$str</span>;
  • strpos calculates the position of the character in the string (starting from 0)
<span>$str</span> = 'Hello PHP'<span>;
</span><span>echo</span> <span>strpos</span>(<span>$str</span>,'o');  <span>//</span><span>计算字符在字符串中的位置</span>
<span>echo</span> '<br/>'<span>;
</span><span>echo</span> <span>strpos</span>(<span>$str</span>,'PH');
  • substr intercepts string
<span>$str</span> = 'Hello PHP'<span>;
</span><span>//</span><span>截取字符串</span>
<span>$str1</span> = <span>substr</span>(<span>$str</span>,2,3); <span>//</span><span>从2位置开始截取,截取长度为3的字符串</span>
<span>echo</span> <span>$str1</span>;
        If the length parameter is not passed in, it will be intercepted from the specified position to the end of the string
  • str_split splits string fixed-length split (default length is 1)
<span>$str</span> = 'Hello PHP'<span>;
</span><span>//</span><span>分割字符串</span>
<span>$result</span> = <span>str_split</span>(<span>$str</span>);   <span>//</span><span>将结果保存到一个数组中</span>
<span>print_r</span>(<span>$result</span>);  <span>//</span><span>使用print_r输入一个数组</span>
<span>echo</span> '<br/>'<span>;
</span><span>$result1</span> = <span>str_split</span>(<span>$str</span>,2<span>);
</span><span>print_r</span>(<span>$result1</span>);
  • explode (split character, string to be split) Split according to spaces
<span>$str</span> = 'Hello PHP Java C# C++'<span>;
</span><span>$result</span> = <span>explode</span>(' ',<span>$str</span><span>);
</span><span>print_r</span>(<span>$result</span>);
  • Concatenation of strings
<span>$str</span> = 'Hello PHP Java C# C++'<span>;
</span><span>//</span><span>字符串的连接</span>
<span>$num</span> = 100<span>;
</span><span>$str1</span> = <span>$str</span>.'<br/>Objective-C '.<span>$num</span><span>;
</span><span>echo</span> <span>$str1</span><span>;
</span><span>echo</span> '<br/>'<span>;
</span><span>$str2</span> = "<span>$str</span><br/>Objective-C <span>$num</span>";    <span>//</span><span>另一中简便的写法</span>
<span>echo</span> <span>$str2</span>;

2.PHP array
  • Array declaration PHP arrays are very dynamic and can store all types of data
<span>//</span><span>数组的声明方式</span>
<span>$arr</span> = <span>array</span><span>();
</span><span>$arr</span>[0] = 'Hello'<span>;
</span><span>$arr</span>[1] = 'World'<span>;
</span><span>$arr</span>[2] = 2<span>;
</span><span>$arr</span>[3] = 3.14<span>;
</span><span>print_r</span>(<span>$arr</span>);
  • array_push()//Add an element
  • array_pop()//Delete the last element
<span>$arr</span> = <span>array</span><span>();
</span><span>for</span>(<span>$i</span>=0;<span>$i</span><100;<span>$i</span>++<span>){
    </span><span>array_push</span>(<span>$arr</span>,'Item'.<span>$i</span><span>);
}
</span><span>print_r</span>(<span>$arr</span>);
  • Storing data through key-value pairs
<span>$arr</span> = <span>array</span><span>();
</span><span>$arr</span>['H'] = 'Hello'<span>;
</span><span>$arr</span>['W'] = 'World'<span>;
</span><span>print_r</span>(<span>$arr</span><span>);
</span><span>echo</span> '<br/>'<span>;
</span><span>echo</span> <span>$arr</span>['H'];
Output: Array ( [H] => Hello [W] => World ) Hello
  • You can not only use a PHP array as an Array, but you can also use it as a Map
  • Array initialization
<span>$arr</span> = <span>array</span>('H'=>'Hello','W'=>'World');    <span>//</span><span>PHP数组初始化</span>
<span>print_r</span>(<span>$arr</span>);
  • Array subscripts and key-value pairs can exist at the same time
<span>$arr</span> = <span>array</span>(0=>'Make',1=>'Tom','H'=>'Hello','W'=>'World'<span>);
</span><span>print_r</span>(<span>$arr</span><span>);
</span><span>echo</span> '<br/>'<span>;
</span><span>echo</span> <span>$arr</span>[0<span>];
</span><span>echo</span> '<br/>'<span>;
</span><span>echo</span> <span>$arr</span>['W'];
Output: Array ( [0] => Make [1] => Tom [H] => Hello [W] => World ) Make World

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1069668.htmlTechArticlePHP common functions, php 1. PHP string string declaration variable = '' or "" (generally it will Use single quotes, because it is more convenient to write) $str = 'Hello PHP' ; echo $str ; strpos count...
Statement:
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