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. sql 、linq、lambda 查询语句的区别

    LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量中被查询的值 [group by 条件] Lambda ...

  2. 数据库连接池配置 - Oracle,SQL Server,DB2,MYSQL,SQLLITE3

    ################## 数据库连接配置 ################## #Oracle#hibernate.connection.driverClass=oracle.jdbc.d ...

  3. java初学知识点

    public class EnumTest { public static void main(String[] args) { Size s=Size.SMALL; Size t=Size.LARG ...

  4. Educational Codeforces Round 6 E. New Year Tree dfs+线段树

    题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...

  5. px和em的区别(转)

    在国内网站中,包括三大门户,以及“引领”中国网站设计潮流的蓝色理想,ChinaUI等都是使用了px作为字体单位.只有百度好歹做了个可调的表率.而 在大洋彼岸,几乎所有的主流站点都使用em作为字体单位, ...

  6. LTE Module User Documentation(翻译13)——频率复用算法(Frequency Reuse Algorithms)

    LTE用户文档 (如有不当的地方,欢迎指正!)   19 Frequency Reuse Algorithms(频率复用算法)   本节我们将描述如何在 LTE 仿真中使用频率复用(FR)算法.共有两 ...

  7. linux chomd 学习

    chomd -R 777 directory_name :递归地给directory目录下所有文件和子目录的属主分配读的权限 ------2016-10-31 -- source: Linux chm ...

  8. php_match/preg_match_all 默认有字符串长度限制

    php_match/preg_match_all 默认有字符串长度限制:52500(或许你的服务器环境是更长,或者更短),当字符串长度大于52500,只能匹配到52500数据,超出的部分会被系统自己截 ...

  9. bootstrap笔记-布局

    1.通过文本对齐类,可以简单方便的将文字重新对齐. <p class="text-left">Left aligned text.</p> <p cl ...

  10. cmake gcc等安装备案

    cmake安装,参照 http://www.cnblogs.com/voyagflyer/p/5323748.html cmake2.8以上 安装后的是/usr/local/bin/cmake -ve ...