<?php // 函数(全局成员)完成特定功能的代码块 // echo strtoupper('imzchloe'); // echo strlen('阳光真好我好开心o(* ̄▽ ̄*)ブ'); // echo abs(-4);// function 函数名称 ([参数列表 -形参]) // { // 函数体 // return 返回值 // 1.函数只能返回单个的值 如果想返回多个值,返回一个数组,对象。 // 2.return后面的代码不会被执行 // }function divide($one,$two) {return $one/$two;// echo '你好呀,小可爱' }// echo divide(99,9);function demo() { return sha1('dfrescfre');return md5('dfffdf');return 1===1;return new StdClass;return array('12','45');return 88.88;return 'nihao';return true; } // var_dump(demo());// 多个值以数组的形式返回function test() {return ['status'=>1,'msg'=>'登录成功']; } // print_r(test());// 多个值以对象的形式返回 function demo2() {return new class(){public $name = 'chloe';public $gender = '女';}; }// $obj = demo2(); // var_dump($obj); // echo '<hr>'; // echo $obj->name; // echo $obj->gender;// 多个值以json字符串的1方式返回 function demo3() {// json_encode()将数组转为json字符串return json_encode(['status'=>1,'msg'=>'验证成功']); } var_dump(demo3()); echo '<hr>'; $res = demo3(); // json_decode()第二个设为true拿到数组 var_dump( json_decode( demo3($res,true)));?>
<?php // 多个参数可以用,号隔开 // 1.参数为函数的调用者一个接口去改变函数体的执行行为 // 2.没有参数function demo($name) {echo"你好$name<br>"; }demo('蓝波'); demo("喵喵");//返回一个3行2列的表格 $table = "<table border ='1' cellspacing='0'>"; for ($i=0; $i < 9; $i++){$table.="<tr>";for ($j=0; $j < 5; $j++){$table.="<td>习</td>";} $table.="</tr>";$table.="</tr>"; } $table.='</table>';// echo $table;function createTable(int $rows,int $cols,string $content, string $bgColor):string {$table = "<table border ='1' cellspacing='0' bgColor='$bgColor'>";for ($i=0; $i < $rows; $i++){$table.="<tr>";for ($j=0; $j < $cols; $j++){$table.="<td>$content</td>";} $table.="</tr>";}$table.='</table>'; return $table; }// echo createTable(3,10,'小','red');function createTable1(int $rows=7 ,int $cols=7 ,string $content='大', string $bgColor='green'):string {$table = "<table border ='1' cellspacing='0' bgColor='$bgColor'>";for ($i=0; $i < $rows; $i++){$table.="<tr>";for ($j=0; $j < $cols; $j++){$table.="<td>$content</td>";} $table.="</tr>";}$table.='</table>'; return $table; }// echo createTable1(5,5,'饿','orange');// echo createTable1('饿','orange');// 1. 参数列表是从左往右传的值// 2.不传参数或者少传参数,都会使用默认的参数,如果用户传了参数,会发生覆盖echo createTable1(8,8,);?>