php函数 函数名,参数列表,函数体 php时弱类型语言返回类型可以没有
function 函数名()
{
}

1.简单函数
四要素:返回类型,函数名,参数列表,函数体

function Show()
{
echo "hello";
}

Show();

2.有返回值的函数
function Show()
{
return "hello";
}

echo Show();

3.有参数的函数

function Show($a)
{
echo $a;
}

Show("你好");

4.可变参数的函数
function Sum()
{
$attr = func_get_args();
$n = func_num_args();

$sum = 0;
for($i=0;$i<$n;$i++)
{
$sum += $attr[$i];
}
echo $sum;
}
Sum(1,2,3,4);
数组
$attr = array(1,2);
$attr1 = [1,2,3,4,5];
$attr2[0] = "hello";
$attr2[1] = "world";
var_dump($attr2);

1.索引数组
$attr = array(1,2,3);
var_dump($attr);

2.关联数组
$attr1 = array("one"=>1,"two"=>2,"3"=>3);
var_dump($attr1);
echo $attr1[3];

特点:
1.数组里面可以存储任意类型数据
2.数组并不是在内存里面开辟一块连续的区域存储

遍历数组
1.for循环遍历,只能遍历索引数组

for($i=0;$i<count($attr);$i++)
{
echo $attr[$i]."<br>";
}

2.foreach遍历,索引关联都可以遍历
foreach($attr as $v)
{
echo $v."<br>";
}

foreach($attr1 as $k=>$v)
{
echo "{$k}--{$v}<br>";
}

3.each()和list()配合着来遍历数组var_dump(each($attr1)); 取数组里面当前指针指向的元素
var_dump(each($attr1));
var_dump(each($attr1));

list($a,$b,$c,$d)=$attr; 将右侧数组里面的元素赋值给参数列表里面的变量

while(list($k,$v) = each($attr1))
{
echo "{$k}--{$v}<br>";
}

4.使用指针的方式来遍历数组
echo current($attr1); //取指针指向的当前元素的value值
echo key($attr1); //取指针指向的当前元素的key
next($attr1); 将指针向下调一个
next($attr1);
prev($attr1); 将指针向上调一个
echo key($attr1);
end($attr1); 将指针调向最后一个元素
reset($attr1); 将指针复位

for($i=0;$i<count($attr1);$i++)
{
echo key($attr1);
next($attr1);
}

do
{
echo key($attr1);
}
while(next($attr1))*/
$attr = array(1,2,3);
var_dump($attr);
常用函数
1.随机数和时间
echo rand(); 随机数生成器
echo rand(0,10); 生成某个范围内的随机数

echo time(); 取当前时间戳
echo date("Y-m-d H:i:s",1381253766); 格式化显示时间
echo strtotime("2013-10-09 01:36:06"); 将字符串转换为时间戳

2.字符串函数
$str = "Hello|World|ni|hao";
$attr = array("aa","bb","cc","dd");

echo strlen($str); 取字符串的长度
var_dump(strcmp($str,"hello world")); 比较两个字符串
echo strtolower($str); 转小写
echo strtoupper($str); 转大写

var_dump(explode("|",$str)); 拆分字符串,返回数组
echo implode("--",$attr);将数组元素拼接成一个字符串

echo substr_replace($str,"***",0,5); 替换指定位置的字符串
echo str_replace("|","***",$str); 查找替换
echo substr($str,0,5); 截取字符串

3.正则表达式
$str =<<<A

A;

echo preg_replace("/\d/","#",$str); 替换
var_dump(preg_split("/\d/",$str)); 拆分
preg_match("/\d/",$str,$arr); 匹配第一个满足正则的字符串
preg_match_all("/\d/",$str,$arr); //匹配所有满足正则的字符串
var_dump($arr);

preg_match_all("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/",$str,$arr)邮箱正则;
var_dump($arr);

4.数组方法
$attr = array(1,2,3,4,5,1);
var_dump(in_array(6,$attr)); 判断某个值是否在数组里面
var_dump(array_reverse($attr)); 翻转数组
echo count($attr); 取数组长度
var_dump(array_unique($attr)); 去重
unset($attr[1]); 删除数组的元素
var_dump(array_values($attr)); 重新索引
var_dump(array_merge($attr,array(5,6))); 合并数组
array_push($attr,"hello"); 向数组里面添加一个元素,返回索引。

例子
<?php
$attr = array(
array("n001","汉族"),
array("n002","回族"),
array("n003","维吾尔族")
);
echo"<select>";
foreach($attr as $v)
{
echo"<option>$v[1]</option>";
}
echo"</select>";
?>

效果显示为:

2016年12-09php函数的更多相关文章

  1. 2016年12月31日 星期六 --出埃及记 Exodus 21:26

    2016年12月31日 星期六 --出埃及记 Exodus 21:26 "If a man hits a manservant or maidservant in the eye and d ...

  2. 2016年12月30日 星期五 --出埃及记 Exodus 21:25

    2016年12月30日 星期五 --出埃及记 Exodus 21:25 burn for burn, wound for wound, bruise for bruise.以烙还烙,以伤还伤,以打还打 ...

  3. 2016年12月29日 星期四 --出埃及记 Exodus 21:24

    2016年12月29日 星期四 --出埃及记 Exodus 21:24 eye for eye, tooth for tooth, hand for hand, foot for foot,以眼还眼, ...

  4. 2016年12月28日 星期三 --出埃及记 Exodus 21:23

    2016年12月28日 星期三 --出埃及记 Exodus 21:23 But if there is serious injury, you are to take life for life,若有 ...

  5. 2016年12月27日 星期二 --出埃及记 Exodus 21:22

    2016年12月27日 星期二 --出埃及记 Exodus 21:22 "If men who are fighting hit a pregnant woman and she gives ...

  6. c++中变量声明和变量定义的区别。2016年12月6日

    整个流程: 1.程序告诉cpu,程序将要使用一个变量.(暂时不一定用到,先说一下.) 2.程序告诉CPU,程序现在就要使用一个变量.(现在就用) 3.cpu按照这个变量的类型,把内存划分出几个单位(b ...

  7. 2016年12月26日 星期一 --出埃及记 Exodus 21:21

    2016年12月26日 星期一 --出埃及记 Exodus 21:21 but he is not to be punished if the slave gets up after a day or ...

  8. 2016年12月25日 星期日 --出埃及记 Exodus 21:20

    2016年12月25日 星期日 --出埃及记 Exodus 21:20 "If a man beats his male or female slave with a rod and the ...

  9. 2016年12月24日 星期六 --出埃及记 Exodus 21:19

    2016年12月24日 星期六 --出埃及记 Exodus 21:19 the one who struck the blow will not be held responsible if the ...

  10. 2016年12月23日 星期五 --出埃及记 Exodus 21:18

    2016年12月23日 星期五 --出埃及记 Exodus 21:18 "If men quarrel and one hits the other with a stone or with ...

随机推荐

  1. html5 svg动画

    http://www.zhangxinxu.com/sp/svg/ 以上是svg的一个线上编辑器,也可以adobe Illustrator制作生成. 我们通过以上编辑器可以获得以下代码. 例: < ...

  2. NavBarControl

    https://documentation.devexpress.com/#WindowsForms/clsDevExpressXtraNavBarNavBarControltopic Views h ...

  3. 从零开始HTML(三 2016/9/20)

    1.HTML表单 HTML 表单用于搜集不同类型的用户输入.<form> 元素,HTML 表单用于收集用户输入.<form> 元素 ①<input> 元素,< ...

  4. Oracle_下载地址

    1.http://www.oracle.com/technetwork/cn/database/enterprise-edition/downloads/index.html 1.1.现在(20161 ...

  5. for_each使用方法详解[转]

    for_each使用方法详解[转] Abstract之前在(原創) 如何使用for_each() algorithm? (C/C++) (STL)曾經討論過for_each(),不過當時功力尚淺,只談 ...

  6. iOS开发 弹簧效果

    #import "DDJelloView.h" #define SYS_DEVICE_WIDTH    ([[UIScreen mainScreen] bounds].size.w ...

  7. git reset soft,hard,mixed之区别深解

    GIT reset命令,似乎让人很迷惑,以至于误解,误用.但是事实上不应该如此难以理解,只要你理解到这个命令究竟在干什么. 首先我们来看几个术语 HEAD 这是当前分支版本顶端的别名,也就是在当前分支 ...

  8. CSS Hack大全-教你如何区分出IE6-IE10、FireFox、Chrome、Opera

    CSS Hack大全-教你如何区分出IE6-IE10.FireFox.Chrome.Opera 转载自:http://www.jb51.net/article/50116.htm 现在的浏览器IE6- ...

  9. 关于电脑安装多个版本JDK后使用时的切换

    描述:刚到新公司,自己安装了jdk1.7和开发工具myeclipse10,但是由于公司项目的需要(具体原因不详细描述了),需要使用myeclipse6.5和jdk1.6.于是在切换jdk1.7和jdk ...

  10. D:Balanced Lineup

    总时间限制: 5000ms 内存限制: 65536kB描述For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always lin ...