首页 > php教程 > php手册 > 正文

PHP常用功能,php

WBOY
发布: 2016-06-13 08:51:18
原创
1375 人浏览过

PHP常用功能,php

1.PHP字符串
  • 字符串声明 变量=''或者""(一般情况会使用单引号,因为写起来会比较方便)
<span>$str</span> = 'Hello PHP'<span>;
</span><span>echo</span> <span>$str</span>;
登录后复制
  • strpos 计算字符在字符串中的位置(从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 截取字符串
<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>;
登录后复制
不传入长度参数的话,会从指定位置一直截取到字符串的末尾
  • str_split 分割字符串 固定长度的分割(默认长度为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(分割字符,待分割的字符串) 按照空格进行分割
<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>);
登录后复制
  • 字符串的连接
<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数组
  • 数组的声明 PHP的数组动态性非常好,可以存所有类型的数据
<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()//添加一个元素
  • array_pop()//删除最后一个元素
<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>);
登录后复制
  • 通过键值对(Key-Value)方式存储数据
<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'];
登录后复制
  输出:   Array ( [H] => Hello [W] => World )   Hello
  • 不仅可以把PHP数组当成一个Array用,还可以当成一个Map用
  • 数组初始化
<span>$arr</span> = <span>array</span>('H'=>'Hello','W'=>'World');    <span>//</span><span>PHP数组初始化</span>
<span>print_r</span>(<span>$arr</span>);
登录后复制
  • 可以同时存在数组小标,以及键值对
<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'];
登录后复制
     输出:     Array ( [0] => Make [1] => Tom [H] => Hello [W] => World )    Make   World
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板