009——基础加强

作者: 土乒76 | 来源:发表于2017-07-17 23:03 被阅读12次

检测变量

// 获取变量类型
gettype()
// 是否是某种类型
is_int()
is_array()
// 变量是否存在
isset()
// 变量是否为空
empty()

打印变量

echo()// 字符串、数字
print_r()// 数组、对象
var_dump()// 打印变量的类型及其值

类型转换

+// 转number
.// 转字符串
if// 转Boolean

销毁变量

unset()

函数

// 函数就是封装起来的一段代码可以随时调用
如果有默认值参数,应该写在最后
<?php 
    $a = 7;
    function fn(&$a) {
        return $a = $a - 1;
    }
    fn($a);
    echo $a;// 6
?>
<?php 
    $a = 7;
    function fn() {
        global $a;// 告诉去全局找
        return $a;
    }
    echo fn();
?>
<?php 
    $a = 1;
    $b = 2;
    print_r($GLOBALS);// 收集页面中全局变量的全局数组
?>
// 动态调用函数
<?php 
    function good(){
        echo "haha";
    }
    function bad(){
        echo "wuwu";
    }
    $heart = 'good';

    $heart();
?>

时间戳函数

<?php 
    // 格林威治事件1970年1月1日00:00:00到当前的秒数
    echo time()."<br/>";
    echo microtime()."<br/>";// 微秒数和时间戳
    echo microtime(true);// 合一起输出
?>
// 格式化时间
<?php 
    echo date('Y/m/d H:i:s');
?>
<?php 
    // 上一小时的时间
    $time = time() - 60 * 60;
    echo date('Y/m/d H:i:s', $time);
?>

读取文件夹

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <?php
        $path = isset($_GET['dir']) ? $_GET['dir'] : '.';
        $fh = opendir($path);

        // echo $row = readdir($fh)."<br/>";// 返回文件名
        // echo $row = readdir($fh)."<br/>";// 返回文件名
        // echo $row = readdir($fh)."<br/>";// 返回文件名
        
        // closedir();
    ?>
    <h1>读取文件夹</h1>
    <table border="1">
        <tr>
            <td>名称</td>
            <td>操作</td>
        </tr>
        <?php while ( ($row = readdir($fh)) !== false ) {// 文件名为0时 ?>
        <tr>
            <td><?php echo $row; ?></td>
            <td>
                <a href="index.php?dir=<?php echo $path.'/'.$row; ?>">查看</a>
            </td>
        </tr>
        <?php } ?>
        <?php closedir(); ?>
    </table>
</body>
</html>

数组

// 索引数组
// 关联数组
<?php
    $arr = array('name'=>'Aaayang', 'age'=>18);
    echo $arr['name'];
?>
// 遍历数组
<?php
    $arr = array('a', 'b', 'c', 'd');
    for($i = 0; $i < count($arr); $i ++) {
        echo $arr[$i] . "<br/>";
    }
?>
// foreach
<?php
    $arr = array('a', 'b', 'c', 'd');
    foreach ($arr as $key => $value) {
        echo $key . '>>' . $value . "<br/>";
    }
?>
// foreach简写,foreach不能单循环出键,通过array_keys可以
<?php
    $arr = array('a', 'b', 'c', 'd');
    foreach ($arr as $value) {
        echo $value . "<br/>";
    }
?>
// array_keys
<?php
    $arr = array('a', 'b', 'c', 'd');
    print_r(array_keys($arr));// 返回数组中所有的键
?>
// 修改数组
<?php
    $arr = array('a'=>3, 'b'=>4, 'c'=>5);
    foreach ($arr as $key => $value) {
        $arr[$key] = $value * 2;
    }
    print_r($arr);
?>

static

<?php
    function a() {
        $a = 5;
        $a += 1;
        return $a;
    }
    echo a() . "<br/>";// 6
    echo a() . "<br/>";// 6
?>
<?php
    function a() {
        static $a = 5;
        $a += 1;
        return $a;
    }
    echo a() . "<br/>";// 6
    echo a() . "<br/>";// 7
?>
// 应用
<?php
    function openfile($file) {
        $fh = fopen($file, 'r');
        return $fh;
    }
    // 打开了3次
    print_r(openfile('.'));
    print_r(openfile('.'));
    print_r(openfile('.'));

    function openfile($file) {
        static $fh = null;// 记住了上次的$fh
        if($fh === null) {
            $fh = fopen($file, 'r');
        }
        return $fh;
    }
    // 实际打开了1次
    print_r(openfile('.'));
    print_r(openfile('.'));
    print_r(openfile('.'));
?>

<?php 

    class Comment {
        public $username;
        public $content;

        public function setUsername($username) {
            $this->username = $username;
        }
        public function getUsername() {
            return $this->username;
        }
    }

    class CommentList {
        const FilePath = "commentList.txt";

        public function getCommentList() {
            return unserialize(file_get_contents(self::FilePath));// 获取值的姿势
        }

        public function write($commentData) {
            $commentList = $this->getCommentList();
        }
    }

?>
// 访问私有变量的套路
<?php 

    class Comment {
        private $username;
        public $content;

        public function set($name, $value) {
            $this->$name = $value;
        }
        public function get($name) {
            return $this->$name;
        }
    }

    // 访问私有属性的姿势
    $comment = new Comment();
    $comment->set('username','Aaayang');
    echo $comment->get('username');
?>
// 魔术方法
<?php 
    
    // 魔术方法自动调用
    class Comment {
        private $username;
        public $content;

        public function __set($name, $value) {
            $this->$name = $value;
        }
        public function __get($name) {
            return $this->$name;
        }
    }

    // 有魔术方法的情况下可以直接访问私有
    $comment = new Comment();
    $comment->username = "Aaayang";
    echo $comment->username;
?>
// 访问类常量
<?php 
    
   class CommentList {
        const FilePath = "commentList.txt";

        public function getCommentList() {
            return unserialize(file_get_contents(self::FilePath));// 获取值的姿势
        }

        public function write($commentData) {
            $commentList = $this->getCommentList();
        }
    }

    // 访问类常量
    $commentList = new CommentList();
    echo $commentList::FilePath;
?>
// 静态属性和方法不需要实例化可以直接调用
<?php
    
    class Tools {
        public static $titleTemp = 'Aaayang';// 静态属性属于类本身,不需要实例化就能调用

        public static function parseTitle($title) {// 静态方法中不能调用非静态属性和非静态方法
            return $title . '-_-'.self::$titleTemp;// 调用静态属性
        }

        public function test() {
            self::parseTitle('haha');// 非静态方法中调用静态方法
        }
    }

    // echo Tools::$titleTemp;// 静态属性和方法不需要实例化就能调用

    echo Tools::parseTitle("VIP");

?>
<?php
    
    // 构造方法:对象被创建时自动调用的方法,一般做初始化工作时使用
    // 析构函数:对象在内存中被销毁时自动调用,不能带参数
    class Pager {
        public $page;// 当前页
        public $totalPage;
        public $link;

        public function __construct($totalPage, $link, $page=1){
            $this->page = $page;
            $this->totalPage = $totalPage;
            $this->link = $link;
        }
    }

    $pager = new Pager(10, 'http://baidu.com', 2);
    print_r($pager);
?>
// 构造方法
<?php
    
    class BaseClass {
        public $user;
        public function error() {
            echo "404<br/>";
        }
        public function __construct() {
            echo "验证<br/>";
        }
    }

    class SubClass extends BaseClass {
        public function __construct() {
            parent::__construct();// 执行父类的构造方法
            echo "验证2";
        }
        public function test() {
            $this->user;
            $this->error();// 会调用本身的
        }
        public function error() {
            echo "error";
        }
    }

    $subClass = new SubClass();// 子类没有构造函数会直接调用父类的

?>

相关文章

  • 009——基础加强

    检测变量 打印变量 类型转换 销毁变量 函数 时间戳函数 读取文件夹 数组 static 类

  • 玩偶公仔怎么用礼品纸包装送人?

    毛绒公仔的礼物包装方法。 礼盒先生 基础礼物包装009-玩偶包法 1. 基础礼物包装009-玩偶包法↓ 2. 包装...

  • 基础加强

    Junit单元测试: 反射:框架设计的灵魂 注解:

  • php基础加强

    php四种标识符 标准标示符 短标签风格 script风格 asp风格 HTML和PHP的混合模式 php的代码是...

  • js基础加强

    定义类1 调用 定义类2 this指向修改 剩余参数 伪数组转真数组 数组相关 模板字符串定义 set集合去重 O...

  • 加强基础练习

    赵老师新教育知行合一678: 宜阳县董王庄乡赵坡小学教学点 抓住核心素养的训练方法,有效,而且有明显的效果。 但是...

  • 012 《互联网时代如何高效学习》的课程设计要求

    012是对009的迭代 目前的基础是,已经有了第一次cc课堂。这次的任务是要细化。 013也是对009的迭代,主要...

  • Lesson 009 —— python 基础语法

    Lesson 009 —— python 基础语法 编码 默认情况下,Python 3 源码文件以 UTF-8 编...

  • iOS学习

    还有很多基础知识需要加强。

  • 九 基础加强——第一节 基础加强

    1、今日内容 1、Junit 单元测试。测试代码2、反射。框架涉及的灵魂3、注解@Override 2、测试概述 ...

网友评论

    本文标题:009——基础加强

    本文链接:https://www.haomeiwen.com/subject/txvqkxtx.html