批改状态:合格
老师批语:
<?php// *变量赋值// *1.默认值传递:$usernam='关云长';// *2.传给另一变量:$myName=$usernam;// *3.打印:printf('$username=%s,$myName=%s<br>',$usernam,$myName);// *4.引用传递$yourName=&$username;$yourName='关羽';// *5.打印printf('$yourName=%s,$username=%s',$yourName,$username);

<?php// *从类中创建一个对象(实例)$obj=new class('关羽'){public string $usernam;public function __construct(string $usernam){$this->usernam=$usernam;}};echo gettype($obj).'<br>';echo $obj->usernam.'<br>';

<?php// *自定义函数function sanguo1(string $username):string{return '三国演义,'.$username;}echo sanguo1('关公').'<br>';// *call_user_func(函数,参数列表)echo call_user_func('sanguo1','诸葛亮').'<br>';//*---------------------------------------------------function sanguo2(string $username2,$feature):string{return '五虎上将:'.$username2.',特点是:'.$feature;}$params=['关云长','万夫不挡之勇'];echo call_user_func_array('sanguo2',$params).'<br>';

<?php// *多分支// *deposit:存款$deposit=100000;if($deposit>=1 && $deposit<100000){echo"{$deposit}元,为一般会员。<br>";}elseif($deposit>=100000 && $deposit<500000){echo"{$deposit}元,为铜卡会员。<br>";}elseif($deposit>=500000 && $deposit<1000000){echo"{$deposit}元,为银卡会员。<br>";}elseif($deposit>=1000000){echo"{$deposit}元,为金卡会员。<br>";}else{echo"数据错误!";}

<?php// *多分支的语法糖$deposit=600000;switch(true){case $deposit>=1 && $deposit<100000:echo"{$deposit}元,为一般会员。<br>";break;case $deposit>=100000 && $deposit<500000:echo"{$deposit}元,为铜卡会员。<br>";break;case $deposit>=500000 && $deposit<1000000:echo"{$deposit}元,为银卡会员。<br>";break;case $deposit>=1000000 :echo"{$deposit}元,为金卡会员。<br>";break;default:echo"数据错误!";}

<?php$sanguo=['魏国','蜀国','吴国'];// *人性化打印print_r($sanguo);// *格式化打印printf('<pre>%s</pre>',print_r($sanguo,true));// *索引数组$lgz=['0'=>'刘备','1'=>'关羽','2'=>'张飞'];printf('<pre>%s</pre>',print_r($lgz,true));// *这样写代替索引数组$lgz=['刘备','关羽','张飞'];printf('<pre>%s</pre>',print_r($lgz,true));// *关联数组$liubei=[0=>1,1=>'刘备',2=>'剑'];$liubei=['id'=>1,'name'=>'刘备','weapon'=>'剑'];printf('<pre>%s</pre>',print_r($liubei,true));// *访问关联数组某个元素echo $liubei['name'].'的武器是:'.$liubei['weapon'].'<br>';

<?php// *循环的入口判断$sanguo=['魏国','蜀国','吴国'];$i=0;echo '数组长度:',count($sanguo),'<br>';$list='<ul>';$i=0;while($i<count($sanguo)){$list.="<li>{$sanguo[$i]}</li>";$i++;}$list.='</ui>';echo $list;echo '<hr>';// *循环的出口判断$list1='<ul>';$i1=0;do{$list1.="<li>{$sanguo[$i1]}</li>";$i1++;}while($i1<count($sanguo));$list1.='</ui>';echo $list1;echo '<hr>';// *for循环$list2='<ul>';for($i2=0;$i2<count($sanguo);$i2++){$list2.="<li>{$sanguo[$i2]}</li>";}$list2.='</ui>';echo $list2;

<?php$lgz=['刘备','关羽','张飞'];$liubei=['id'=>1,'name'=>'刘备','weapon'=>'剑'];// *foreach中键与值的打印foreach($lgz as $key =>$value ){printf('[%s]=>%s<br>',$key,$value);}echo '<hr>';foreach($liubei as $key =>$value ){printf('[%s]=>%s<br>',$key,$value);}

<?php// *二维数组及其打印$lgz=[0=>['id'=>1,'name'=>'刘备','weapon'=>'剑'],1=>['id'=>2,'name'=>'关羽','weapon'=>'关刀'],2=>['id'=>3,'name'=>'张飞','weapon'=>'长茅'],];printf('<pre>%s</pre>',print_r($lgz,true));

<?php// *二维数组在页面上的渲染// *二维数组$sanguo = [0=>['id'=>1,'name'=>'曹操', 'gender'=>1, 'nationality'=>'魏国'],1=>['id'=>2,'name'=>'刘备', 'gender'=>1,'nationality'=>'蜀国'],2=>['id'=>3,'name'=>'孙权','gender'=>1, 'nationality'=>'吴国'],3=>['id'=>4,'name'=>'蔡文姬','gender'=>0, 'nationality'=>'魏国'],];// *表格的设置$table = '<table border="1" width="400" cellspacing="0" cellpadding="3" align="center">';$table .= '<caption>三国人物信息表</caption>';$table .= '<thead bgcolor="#ccc"><tr><th>ID</th><th>姓名</th><th>性别</th><th>国籍</th></tr></thead>';$table .= '<tbody align="center">';// *遍历表格中二维数组的数据foreach ($sanguo as $user) {// print_r($user);$table .= '<tr>';$table .= "<td>{$user['id']}</td>";$table .= '<td>'.$user['name'].'</td>';$table .= '<td>'.($user['gender'] ? '男' : '女').'</td>';$table .= '<td>'.$user['nationality'].'</td>';$table .= '</tr>';}// *拼接表格的尾部$table .= '</tbody>';$table .= '</table>';//* 打印表格到页面中echo $table;

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号