PHP--笔试题

1、比较大小

var_dump(10 == "10a1"); //10 ==10 true
var_dump("10" == "10a1"); //false
var_dump("10" == "1e1"); //10 ==10 true
var_dump(null == 0); //0 == 0 true
var_dump(true == 0); //false
var_dump(true == 1); //true
运算数1类型 运算数2类型 结果
string或null 任何其他类型 将null转换为”“,进行数字或词汇比较
bool或null 任何其他类型 转换为bool, false
string,resource或number string,resource,或number 将字符串和资源转换为数字,按数字比较
array array 具有较少成员的数组较小,如果count相同,并且运算数1中的键不存在与运算数2中则数组无法比较,否则按个比较
object 任何其他类型 object总是最大
array 任何其他类型 array总是最大

2、$str="hello world test",用一行代码分割字符串,并分别赋给三个变量,两种方法

list($a,$b,$c) = explode(' ', $str)
list($a, $b, $c) = preg_split("/\s+/", $str)

3、php连接mysql及查询操作

/*原始mysql扩展在php5.5之后不支持使用,mysql扩展是针对mysql4.1.3设计的,mysql4.1.3之后版本推荐使用mysqli*/
//连接到数据库
//方法1
$mysqli = mysqli_connenct($server_name,$username,$password, $database, $port);
if (mysqli_connect_errno($mysqli)) {
   echo "Failed to connect MySQL:" . mysqli_connect_error();
}
//方法2:面向对象方法
$msyqli = new mysqli("localhost", "user", "password", "database", 3306);
if($mysqli->connect_errno) {
   echo "Failed to connect MySQL:" . $mysqli->connnect_errno . $mysqli->connect_error;
}
//从user取出数据
$res = $mysqli->query("SELECT * FROM user order by id desc limit 2");
$data = $res->fatch_all(MYSQLI_ASSOC);
//stmt查询
$sql1 = "select * from user where id;
$stmt1 = $mysqli->prepare($sql1);
$stmt1->bind_param('i', $i);
$i = 5;
$stmt1->execute();
$result = $stmt1->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);
var_dump($data);
$stmt1->close();
//遍历user的id
$res = $mysqli->query("SELECT * FROM user");
//移动记录指针
$res = data_seek(0);
while ($row = $res->fetch_assoc()) {
      echo "uid = " . $row['uid] . "\n";
}

你可能感兴趣的:(PHP,PHP,php,笔试题)