"黄色");foreach遍历数组//数组的遍历$arr=array("苹"/> "黄色");foreach遍历数组//数组的遍历$arr=array("苹">
> 백엔드 개발 > PHP 튜토리얼 > 用2十秒记住几个PHP基础知识点

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

WBOY
풀어 주다: 2016-06-13 12:21:46
원래의
913명이 탐색했습니다.

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

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿