PHP 文档学习 - 1 - 基本语法

来源:互联网 发布:java泛型使用场景 编辑:程序博客网 时间:2024/05/22 01:58

1. 关于结束标签   

纯php文件中结束不建议写结束标签 ?> 


2. 作用域

php 没有块级作用域, 即在 for / if / while 等块内定义的变量外部也能访问

考虑到安全性、内存消耗, 必要时可使用 unset() 销毁变量


3. 夹在条件语句间的HTML代码输出

PHP 标签只是用来分离PHP代码和HTML代码, 不会带来其他影响

// 1. 替代语法<?php if ($num === true): ?>    print this text if $num === true<?php else: ?>    print this text if $num !== true<?php endif; ?>// 2. 正常语法<?php if ($num === true) { ?>    print this text if $num === true<?php } else { ?>    print this text if $num !== true<?php } ?>// 3. 另一例子<?php function test() { ?>hello world<?php } ?>// 4. 3的常用写法<?php function test() {    echo 'hello world';} ?>

4. NULL

变量当成NULL仅当

    (1) 被赋值为NULL

    (2) 未被赋值

    (3) 被unset()

对变量使用强制转换 (unset)  得到NULL


5. 变量

变量名使用中文合法

变量默认使用传值赋值, 引用赋值需使用 &


6. global

$var = 1;    function test_for_global() {    global $var; // 将外部变量 $var 引入函数    echo $var;}test_for_global();

7. static

静态变量在全局中声明无意义

将引用赋予静态变量将可能导致引用NULL(生命期不同)


8. 常量

define('CONST_VAR', 100); // 全局范围中定义常量const CONST_VAR = 100; // 类中定义常量// 读取: self, static(表现多态)


0 0
原创粉丝点击