//已经定义以下php类
class People{
static $version = "1.02beta";
private $age;
public function m($key,$value=NULL){
if($value == NULL){
return $this->key;
}else{
$this->key = $value;
}
}
}
//1、用echo输出People的版本号
echo People::$version;//输出 1.02beta
echo "<br>";
//2、实例化一个People,叫$jack,并给他的age赋值为50
$jack = new People();
$jack->m("age",50);
echo $jack->m("age");//输出 50
/**
* 继承People的子类Child,Child在实例化时,会自动echo父类的版本号。
*/
/**People.class.php文件**/
class People{
static $version = "1.02beta";
private $age;
public function m($key,$value=NULL){
if($value == NULL){
return $this->key;
}else{
$this->key = $value;
}
}
}
/**Child.class.php文件**/
require "People.class.php";
class Child extends People{
public function __construct(){
echo parent::$version;//输出父类版本号
}
}
//输出父类版本号
new Child();//输出 1.02beta
echo (1 == true) ? "Yes" : "No";//输出 Yes
echo "<hr/>";
echo count("abc");//输出 1
echo "<hr/>";
echo ("abc" == true) ? "Yes" : "No";//输出 Yes
echo "<hr/>";
echo ("" == false) ? "Yes" : "No";//输出 Yes
echo "<hr/>";
echo (0 == false) ? "Yes" : "No";//输出 Yes
echo "<hr/>";
$arr = array();
echo $arr ? "Yes" : "No";//输出 No
echo "<hr/>";
$num = 0;
echo empty($num) ? "Yes" : "No";//输出 Yes
/**
* 设计function check($str)验证$str是否仅由26个英文字母(含大小写)组成,是返回true,否则返回false.
*/
function check($str){
if(preg_match("/^[a-zA-Z]*$/", $str)){
return true;
}else{
return false;
}
}
$str = "abcABC";
echo check($str);//输出 true
<?php
/**
* 设计function arr_loop(),使得输入形如array("a","b","c")的数组,返回array("b","c","a")
* 即第一个元素变成最后一个元素。
*/
function arr_loop($arr){
$first = array_shift($arr);
array_push($arr,$first);
return $arr;
}
$arr = array("a","b","c");
print_r(arr_loop($arr));//输出 Array ( [0] => b [1] => c [2] => a )
/**
* 已知网址格式的字符串$url,设计function get_param($url),返回所有
* get参数的数组.
* 例如:$url = "http://wwww.baidu.com/order.php?year=2012&type=done"
* 返回:array("year"=>"2012","type"=>"done");
*/
function get_param($url){
$list = array();
$urls = parse_url($url);
$arr = explode("&",$urls['query']);
foreach ($arr as $value) {
$temp = explode("=", $value);
$list[$temp[0]] = $temp[1];
}
return $list;
}
$url = "http://wwww.baidu.com/order.php?year=2012&type=done";
print_r(get_param($url));//输出 Array ( [year] => 2012 [type] => done )
<?php
/**
* 设计function find_max($a,$b,$c),用最少代码,返回三个浮点数中的最大值。
*/
function find_max($a,$b,$c){
return (($a > $b ? $a : $b) > ($a > $c ? $a : $c)) ? ($a > $b ? $a : $b) : ($a > $c ? $a : $c);
}
$a = 1.1;
$b = 1.2;
$c = 1.3;
echo find_max($a,$b,$c);//输出 1.3
/**
* 已知数据表如下,求其sql查询。
* user表 order表 item表
* -------------------- * ------------------- * -----------------------
* | uid | name | age | * | oid | uid | iid | * | iid | name | price |
* -------------------- * ------------------- * -----------------------
* | 1 | jack | 25 | * | 17 | 1 | 201 | * | 201 | apple | 4 |
* -------------------- * ------------------- * -----------------------
* | 2 | mike | 40 | * | 18 | 1 | 202 | * | 202 | banana| 7 |
* -------------------- * ------------------- * -----------------------
* * | 19 | 2 | 202 | *
* * ------------------- *
*/
//1、请写出sql查询语句:获得30岁以上的顾客的所有订单的消费总额。
SELECT
SUM(`item`.price)
FROM
`user`
JOIN `order` ON `user`.uid = `order`.uid
JOIN `item` ON `item`.iid = `order`.iid
WHERE
`user`.age > 30;
//2、解析三种索引index、primary、unique之不同
primary主键,是唯一的索引且不能为空。
index是普通索引。
unique是唯一索引,不允许有重复。
//测试sql
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for item
-- ----------------------------
DROP TABLE IF EXISTS `item`;
CREATE TABLE `item` (
`iid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`price` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`iid`)
) ENGINE=InnoDB AUTO_INCREMENT=203 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of item
-- ----------------------------
INSERT INTO `item` VALUES ('201', 'apple', '4.00');
INSERT INTO `item` VALUES ('202', 'banana', '7.00');
-- ----------------------------
-- Table structure for order
-- ----------------------------
DROP TABLE IF EXISTS `order`;
CREATE TABLE `order` (
`oid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`uid` int(11) DEFAULT NULL,
`iid` int(11) DEFAULT NULL,
PRIMARY KEY (`oid`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of order
-- ----------------------------
INSERT INTO `order` VALUES ('17', '1', '201');
INSERT INTO `order` VALUES ('18', '1', '202');
INSERT INTO `order` VALUES ('19', '2', '202');
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`uid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'jack', '24');
INSERT INTO `user` VALUES ('2', 'mike', '40');