PHP知识点补漏

【前言】

    本文记录下漏掉的PHP相关知识点

 

【主体】

(1)foreach循环

$name = array('one','two','three');
foreach ($name as $key => $value) {
	 echo "$key:$value<br>";
}

    这里有两种写法,还有一种不带键值对的。没什么区别,第一种在遍历的时候会将每个值赋给$value,第二种除了赋值给$value,还会将键名赋值给$name

$name = array('one','two','three');
foreach ($name as $value) {
		echo "$key:$value<br>";
}

例如,遍历关联数组,这里会输出

$name = array('one'=>1,'two'=>2,'three'=>3);
foreach ($name as $key => $value) {
	echo "$key:$value,";
}

one:1,two:2,three:3

 

(2)函数参数默认值

设置默认值后,调用参数不传参时会使用默认值

扫描二维码关注公众号,回复: 212435 查看本文章
function add($a=1,$b=2){
	return $a + $b;
};
echo add();
echo add(3,3);

 

(3)面向对象

 

(4)修饰词

 

(5)命名空间

 

(6)模板引擎典型的如smarty引擎

 

(7)配置文件分3类:

系统配置文件,分组配置文件,应用配置文件

①系统配置文件ThinkPHP/Conf/convention.php;

②分组 / 模块 /平台配置文件Home/Conf/config.php;(所以有了mca或pca模式)

③应用配置文件Common/Conf/config.php;

 

(8)多维数组

多维数组的各个元素间用,分隔开,否则会报错

$array = array(
	array('one','two','three'),array('four','five','six')
);

 

(9)创建类之前先定义命名空间

创建类,然后实例化类来创建对象(命名空间的写法取决于文件存放的位置)

注意:对于多个人开发项目,函数名很容易重复。用了类之后,类之间的方法名被类分开,重名也没关系。

但是当项目更大时,类名也有可能重复。此时就要用到命名空间,来避免重名。所以创建类之前必须定义命名空间

 

(10)给类的属性赋值  / 对象添加元素

namespace Admin\Controller;
class Student{}
//实例化Student对象
$stu = new Student();
//给类的属性赋值
$stu -> id='1';
$stu -> name='Tony';
$stu -> age='23';
dump($stu);

浏览器输出:

object(Admin\Controller\Student)#6 (3) {
  ["id"] => string(1) "1"
  ["name"] => string(4) "Tony"
  ["age"] => string(2) "23"
}

 

(11)设置编码字符集

TestController.class.php:
<?php
    namespace Admin\Controller;
    use Think\Controller;
    class TestController extends Controller{
        public function test1(){
            //实例化Student对象
            $stu = new Student();
            dump($stu);    
        }
    }
?>
<meta charset="utf-8">//这里为了方便,直接去入口文件添加header头,设置编码字符集,
                      //我在下面拓展里做介绍

 拓展:设置编码字符集:

index.php入口文件:
//给入口文件添加header头声明字符集
header('Content-Type:text/html;charset=utf-8')

 

(12)PHP输出对象属性

$obj=>arrt;或者$obj::attr;,测试后发现第二种没有用?

所以这里推荐使用第一种

 

(13)创建数据表

create table sp_dept(
    id int not null auto_increment,
    name varchar(50) not null,
    pid int not null default 0,//部门分上下级,pid只下级部门id
    sort int not null default 50,//排序
    remark varchar(255),//备注说明
    primary key(id)
)engine=myisam default charset=utf8;//引擎myisam,Mysql的默认存储引擎

 注意:auto_increament为自增;

 

(14)验证时终止脚本向后执行

函数方法中验证某个数据,输出打印后,die()方法终止脚本向后执行;

案例:ThinkPHP的where方法

public function test(){
            //实例化模型
            $model = M('dept');
            //where查询
            $model -> where('id>20');
            
            $data = $model -> select();
            dump($data);
}

 

(15)错误提示完毕后,终止脚本die / exit

$this -> error($model->getError());
exit;
//虽然理论上回跳回上一页,但PHP底层代码会继续执行。所以必须加exit

 

(16)TP里的if判断,注意加空格

<if condition="$vol.id == $data.id ">selected="selected"</if>

 不仅等号两端加,最后也要加

 

(17)说明:有单个删除,也有批量删除。而编辑只能单个编辑,不能批量编辑。

 

(18)静态资源目录下,后期使用的插件都放到plugin(插件)目录下



 

(19)TP添加成功与失败

 

//判断保存结果
if($result){
    $this->success('添加成功',U('showList'),3);
}else{
    $this-error('添加失败');
}

 添加成功后跳页,添加失败的话可以跳页,不过这里一般是默认直接继续展示页,所以也就不传跳页连接了

 

(20)缩略图的等比缩放原则,缩略图thumb方法,必传参数为宽高(遵守等比缩放原则,所以会有下面这种情况

        
 

 

 

 

 

.

猜你喜欢

转载自570109268.iteye.com/blog/2414473