调用函数

 

<?php
require_once("function.php");
//$arr = array(1,2,3,4,5);
//$str = "1982-4-2";
//test1($str,$arr);

//函数的参数默认值
//test2("test");

//vsprintf使用实例
//$arr = [1,2,3];
//echo vsprintf("%d-%d-%02d",$arr);

//调用自己的时间格式化方法
/*$time = formatDateTime("99-8-8");
echo $time;*/

//获取函数全部参数
//test4("select * from users where userName=?","pangpang");
//"select * from users where userName='pangpang'

//vsprintf("select * from users where userName = '%s'","pangpang");

mysql_ping("select * from users where userName=? and password=?","pangpang","qqq");

?>

主页面

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<?php
include("function2.php");
$r = mysql_bind("select * from users where userName=? and password=?","pangpang","qqq");

//$r = mysql_bind("insert into users(id,userName,password,nickName,userImg,status)values(?,?,?,?,?,?)","","3","3","3","3","2");
//$r = mysql_bind("delete from users where userName=?","1");
//$r = mysql_bind("update users set nickName=? where id=?","罗大胖","1");

foreach($r as $row){
echo "用户:".$row['nickName']."<br/>";
echo "密码:".$row['password']."<br/>";
}

?>

 

函数

<?php
function test1($str1="",$str2=array()){
//把数组分割成字符串 implode
//下个这个方法的意思是:$str2应该是个数组
//然后把这个数组按照逗号来分割组成一个新的字符串
$s1 = implode(",",$str2);
// echo $s1;

//把字符串分割成数组 explode
//这个函数的意思就是:首先$str1是一个字符串,这个字符串是按照一个规格组装出来的
//这个规格就是必须符合前面第一个参数的样式
$s2 = explode("-",$str1);

print_r($s2);
}

//函数的默认值
function test2($db="bbs"){
$conn = mysql_connect(HOST,USER,PWD) or die(mysql_error());
mysql_select_db($db,$conn);
mysql_query("set names 'utf8'");
}

function test3($str="hello world"){
echo $str;
}

function formatDateTime($date){
$arr = explode("-",$date);
$str = vsprintf("%04d-%02d-%02d",$arr);
return $str;
}


//获取函数全部参数
//获取传过来的所有参数
function test4(){
//获取传过来参数的数量
$num = func_num_args();
//获取所有传入的参数,返回的是一个数组
$arr = func_get_args();

var_dump($arr);
}

function mysql_ping(){
//获取传入的所有参数的数组
$arr = func_get_args();
//获取第一个参数,在我们这个列子里面,第一个参数其实就是sql语句
$sql = $arr[0];
//传入的sql语句,其实开始是用?替代的变量的位置
//这里需要将变量转化为可以替换格式化字符串的'%s'这样的符号
$sql = str_replace("?","'%s'",$sql);

//array_shift,是将数组最开始的元素移出。返回移出的值,然后数组剩下其余的部分
$values = array_shift($arr);

$sql = vsprintf($sql,$arr);

echo $sql;
}

?>