"黄色");foreach遍历数组//数组的遍历$arr=array("苹"/> "黄色");foreach遍历数组//数组的遍历$arr=array("苹">
Home > Backend Development > PHP Tutorial > 用2十秒记住几个PHP基础知识点

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

WBOY
Release: 2016-06-13 12:21:46
Original
913 people have browsed it

用二十秒记住几个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>
Copy after login

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>
Copy after login

判断函数是否存在: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>
Copy after login

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

<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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template