首页 > 代码库 > PHP-day02

PHP-day02

/*函数的调用;给函数设置默认值*/
function showSelf($name = ‘lemon‘, $age = 18)
{
echo "{$name}今年{$age}了";
}

showSelf(‘小明‘);

/*判断函数是否已经存在*/
if (function_exists("showSelf")) {
showSelf("张三", 22);
} else {
echo "当前函数不存在";
}
echo "<br>";
/*global关键字使用全局变量*/
$age = 10;
function num1()
{
global $age;
echo $age;
$age = 20;
}

function num2()
{
global $age;
echo $age;
}

num1();
num2();

/*静态变量,只初始化一次内存,会改变变量的生命周期,直到文档结束*/
function fn1()
{
static $age = 10;
$age++;
echo $age . "<br>";
}

fn1();
fn1();
fn1();

/*函数的调用*/
function fn2()
{
echo "fn2被调用了";
}

$f = "fn2";
$f();

/*回调函数*/
function showNum($f1)
{
for ($i = 1; $i <= 100; $i++) {

if ($f1($i)) {
echo $i . " ";
}
}
}

function even($num)
{
return $num % 2;
}

echo "<br>";
showNum("even");
function oven($num)
{
return !($num % 2);
}

showNum("oven");
echo "<br>";
function five($num)
{
return $num % 10 == 5;
}

showNum("five");
echo "<br>";
echo "/*************************数组*****************************/";
echo "<br>";
/*************************数组*****************************/
//数组的定义
//1、直接赋值方式;
$array[0] = "1";
$array[1] = "2";
$array[2] = "3";
$array[4] = "5";
$array[‘haha‘] = "hahah";
$array[3] = "4";
print_r($array);
echo "<br>";
//2、关联数组
$array2["name"] = "lemon";
$array2["age"] = "18";
print_r($array2);

echo "<br>";

//2、通过array定义数组
$array3 = array(1, 2, 3, 4, "lemon");
print_r($array3);

echo "<br>";
//按顺序向数组添加数据
$array4 = array();
$array4[] = 0;
$array4[] = 1;
$array4[] = 2;
$array4[] = 3;
print_r($array4);
echo "<br>";

/*遍历数组*/
for ($i = 0; $i < count($array4); $i++) {
echo $array[$i] . " ";
};
echo "<br>";
foreach ($array2 as$item) {
echo $item;
};
echo "<br>";
foreach ($array2 as $key=>$item) {
echo $key."=".$item;
};
echo"<br>";
/*************************get和post请求*************************************/
echo"<a href=http://www.mamicode.com/‘child/judge.php?useName=lemon&passWord=123456‘>点击登录";
echo"<br>";
echo"<form action=‘child/judge.php‘ method=‘post‘>";
echo"<input name=‘userName‘ type=‘text‘>";
echo"<input name=‘passWord‘ type=‘password‘>";
echo"<input type=‘submit‘>";
echo"</form>";

PHP-day02