<?php
header("Content-type:text/html;charset=utf-8");
ini_set("error_reporting","E_ALL & ~E_NOTICE");//关闭错误提示
echo "<p>php问卷</p>";
echo "<hr>";
/**
* 第一题
*/
echo "<b>第一题</b> 创建2维数组,进行数组赋值,循环输出数组内容。(任选一题)<br>";
$arrays = array(
array("3月","4月","5月"),
array("6月","7月","8月"),
array("9月","10月","11月"),
array("12月","1月","2月")
);
for($i=0;$i<5;$i++){
switch ($i){
case 0:
echo "春季";
break;
case 1:
echo "夏季";
break;
case 2:
echo "秋季";
break;
case 3:
echo "冬季";
break;
}
echo "<br>";
for($j=0;$j<4;$j++){
echo $arrays[$i][$j];
}
echo "<br>";
}
echo "<hr>";
/**
* 第二题
*/
echo "<b>第二题</b> 用PHP打印出今天、昨天、明天,下一周、上一个月的时间。格式是2006-5-10 22:21:21<br>";
echo "今天是 ".date("Y-m-d h:i:s")."<br>";
$yesterday=strtotime("yesterday");
$tomorrowTime = strtotime("tomorrow");
$lastWeek =strtotime("last week");
$nextWeek=strtotime("+1 week");
$lastMonth=strtotime("-1 Months");
$nextMonth=strtotime("+1 Months");
echo "昨天是".date("Y-m-d h:i:s",$yesterday)."<br>";
echo "明天是".date("Y-m-d h:i:s",$tomorrowTime)."<br>";
echo "上一周是".date("Y-m-d h:i:s",$lastWeek)."<br>";
echo "下一周是".date("Y-m-d h:i:s",$nextWeek)."<br>";
echo "上一个月是".date("Y-m-d h:i:s",$lastMonth)."<br>";
echo "下一个月是".date("Y-m-d h:i:s",$nextMonth)."<br>";
echo "<hr>";
/**
* 第三题
*/
echo "<b>第三题</b> 用例子说明echo(),print(),print_r()的区别<br>";
echo "echo 输出整数和整数数组:<br>";
$zhengshu=105;
$shuzu = array(105,106,3,4,5);
echo $zhengshu."<br>";
echo $shuzu."<br>";
echo "数组里的第一个数 $shuzu[0]","第二个数$shuzu[3]<br>";
echo "print 输出整数和整数数组:<br>";
print $zhengshu."<br>";
print $shuzu."<br>";
echo "print_r 输出整数和整数数组<br>";
print_r($zhengshu."<br>");
print_r($shuzu);
echo "<hr>";
/**
* 第四题
*/
echo "<b>第四题</b> 判断一个变量是否为null、未声明、变量类型。<br>";
$myobj=5;
echo "判断变量myobj是否为null:".empty($myobj)."<br>";
echo "变量myobj类型".getType($myobj)."<br>";
echo "检测变量是否设置".isset($myobj)."<br>";
echo "<hr>";
/**
* 第五题
*/
echo "<b>第五题</b> 定义一个类Add,实现Add方法,传入2个整型参数x,y,输出x+y的结果。JSON输出<br>";
class Add{
function add($x,$y){
$sum=$x+$y;
return $sum;
}
}
$number = new Add($x, $y);
echo json_encode($number->add(1,2));
echo "<hr>";
/**
* 第六题
*/
echo "<b>第六题</b> 1)写出PHP5权限控制修饰符 2)PHP注释方式";
echo "public(公共),private(私用),protected(继承)";
echo "// 这是单行注释
# 这也是单行注释
/*
这是多行注释块
它横跨了
多行
*/";
echo "<hr>";
/**
* 第七题
*/
echo "<b>第七题</b> 写正则表达式实现邮件格式的校验";
/**
$email = test_input($_POST["email"]);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$emailErr = "无效的 email 格式!";
}
*/
echo "<hr>";
/**
* 第八题
*/
echo "<b>第八题</b> PHP连接mysql数据库<br>";
$conn=mysql_connect('localhost','root','root');
if (!$conn) {
die("Could not Connect:".mysql_error());
}
echo '----------------------------Connect------------>SUCCESS !';
mysql_select_db('mysql',$conn);
$query ="select * from user limit 2";
$result = mysql_query($query);
echo "<table>
<tr>
<th>Host</th>
<th>User</th>
</tr>";
while ($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['Host']."</td>";
echo "<td>".$row['User']."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($conn);
echo "<hr>";
/**
* 第九题
*/
echo "<b>第九题</b> 使用curl方式实现get请求<br>";
$init = curl_init();//初始化curl
//设置参数
curl_setopt($init, CURLOPT_URL, "www.baidu.com");
curl_setopt($init, CURLOPT_RETURNTRANSFER, 1);//设置返回数据
curl_setopt($init, CURLOPT_HEADER, 0);//设置头部不执行
$output = curl_exec($init);//执行curl 获取文档内容
curl_close($init);//释放curl句柄
print_r($output);
echo "<hr>";
/**
* 第十题
*/
echo "<b>第十题</b>PHP读取ini文件<br>";
//读取
$myfile = fopen("G:/initfile.ini","r") or die("Unable to open file!");
echo fread($myfile,filesize("G:/initfile.ini"));
fclose($myfile);
?>
初学php做过的10道题
最新推荐文章于 2024-06-16 18:29:03 发布