"黄色");foreach遍历数组//数组的遍历$arr=array("苹"/> "黄色");foreach遍历数组//数组的遍历$arr=array("苹">

首頁 >後端開發 >php教程 >用2十秒记住几个PHP基础知识点

用2十秒记住几个PHP基础知识点

WBOY
WBOY原創
2016-06-13 12:21:46915瀏覽

用二十秒记住几个PHP基础知识点

数组:
索引数组:数组的键是整数的数组,从0开始。
关联数组:数组的键是字符串的数组

<code class=" hljs php"><span class="hljs-comment">//索引数组</span><span class="hljs-variable">$arr</span>=<span class="hljs-keyword">array</span>(<span class="hljs-string">'I'</span>,<span class="hljs-string">'love'</span>,<span class="hljs-string">'you'</span>);<span class="hljs-comment">//关联数组</span><span class="hljs-variable">$arr0</span>=<span class="hljs-keyword">array</span>(<span class="hljs-string">'apple'</span>=><span class="hljs-string">"苹果"</span>,<span class="hljs-string">'yellow'</span>=><span class="hljs-string">"黄色"</span>);</code>

foreach遍历数组

<code class=" hljs php"><span class="hljs-comment">//数组的遍历</span><span class="hljs-variable">$arr</span>=<span class="hljs-keyword">array</span>(<span class="hljs-string">"苹果"</span>,<span class="hljs-string">"黄色"</span>);<span class="hljs-keyword">foreach</span>(<span class="hljs-variable">$arr</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$key</span>=><span class="hljs-variable">$value</span>){    <span class="hljs-keyword">echo</span> <span class="hljs-string">'<br>键是:'</span>.<span class="hljs-variable">$key</span>.<span class="hljs-string">'对应的值是:'</span>.<span class="hljs-variable">$value</span>;      }<span class="hljs-variable">$arr0</span>=<span class="hljs-keyword">array</span>(<span class="hljs-string">'apple'</span>=><span class="hljs-string">"苹果"</span>,<span class="hljs-string">'yellow'</span>=><span class="hljs-string">"黄色"</span>);<span class="hljs-keyword">foreach</span>(<span class="hljs-variable">$arr0</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$key</span>=><span class="hljs-variable">$value</span>){    <span class="hljs-keyword">echo</span> <span class="hljs-string">'<br>键是:'</span>.<span class="hljs-variable">$key</span>.<span class="hljs-string">',对应的值是:'</span>.<span class="hljs-variable">$value</span>;}</code>

判断函数是否存在:function_exsits(函数名)

<code class=" hljs php"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHello</span><span class="hljs-params">()</span>{</span>    <span class="hljs-keyword">echo</span> <span class="hljs-string">'hello'</span>;}<span class="hljs-variable">$test</span>=<span class="hljs-string">'sayHello'</span>;<span class="hljs-keyword">if</span>(function_exsits(<span class="hljs-variable">$test</span>)){    sayHello();}<span class="hljs-comment">//输出hello</span></code>

构造函数:对象创建时调用的方法
析构函数:对象销毁时调用的方法

<code class=" hljs cs">class Car(){    <span class="hljs-keyword">public</span> __<span class="hljs-title">construct</span>(){        echo <span class="hljs-string">'对象已经创建'</span>;    }    <span class="hljs-keyword">public</span> __<span class="hljs-title">destruct</span>(){        echo <span class="hljs-string">'对象已经销毁'</span>;    }}$car=<span class="hljs-keyword">new</span> Car();</code>

strpos:查找字符串的方法

<code class=" hljs php"><span class="hljs-variable">$str</span>=<span class="hljs-string">'hello world'</span>;<span class="hljs-keyword">echo</span> strpos(<span class="hljs-variable">$str</span>,<span class="hljs-string">'world'</span>);<span class="hljs-comment">//输出6,从第6位开始</span></code>

implode:返回值:把数组元素组合成一个字符串

<code class=" hljs php"><span class="hljs-variable">$arr</span> = <span class="hljs-keyword">array</span>(<span class="hljs-string">'abc'</span>, <span class="hljs-string">'defg'</span>);<span class="hljs-variable">$arr0</span> = implode(<span class="hljs-string">''</span>, <span class="hljs-variable">$arr</span>);print_r(<span class="hljs-variable">$arr0</span>);<span class="hljs-comment">//结果显示abcdefg</span></code>

explode:返回值:函数返回由字符串组成的数组

<code class=" hljs ruby"><span class="hljs-variable">$str</span>=<span class="hljs-string">"abc,defg"</span>;<span class="hljs-variable">$arr</span>=explode(<span class="hljs-string">','</span>,<span class="hljs-variable">$str</span>);print_r(<span class="hljs-variable">$arr</span>); <span class="hljs-regexp">//</span>结果返回一个数组,里面有两个元素<span class="hljs-symbol">:</span> abc和defg</code>

本文作者:By:罗坚元 http://blog.csdn.net/sunyuan_software

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn