三日php之路 -- 第一天(php语言参考)

时间:2022-07-22
本文章向大家介绍三日php之路 -- 第一天(php语言参考),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、基本语法

(1)PHP标记

<?php

echo "Hello World!";

// 当文件为纯PHP时,最好在末尾删除PHP结束标记
//?>

(2)从HTML中分离

// 在一对开始和结束之外的内容,都会被PHP解释器忽略。也就是html标签和PHP代码混合的那种,跟jsp,asp一样...
<p>This is going to be ignored by PHP and displayed by the browser.</p>
<?php echo 'While this is going to be parsed.'; ?>
<p>This will also be ignored by PHP and displayed by the browser.</p>

// 使用条件,高级分离
<?php if ($expression == true): ?>
    This will show if the expression is true.
<?php else: ?>
    Otherwise this will show.
<?php endif; ?>

(3)指令分隔符,注释

PHP需要在每个语句后面用分隔符结束指令。

注释: // 或 /* ... */ 但是,*/ 会匹配最近的那个,切记!切记!


二、类型

PHP支持8种原始数据类型。

四种标量类型:boolean(布尔型),integer(整型),float(浮点型,double),string(字符串)

两种复合类型:array(数组),object(对象)

两种特殊类型:resource(资源),NULL(无类型)

<?php
$a_bool = TRUE;   // a boolean
$a_str  = "foo";  // a string
$a_str2 = 'foo';  // a string
$an_int = 12;     // an integer

echo gettype($a_bool); // prints out:  boolean
echo gettype($a_str);  // prints out:  string

// If this is an integer, increment it by four
if (is_int($an_int)) {
    $an_int += 4;
}

// If $bool is a string, print it out
// (does not print out anything)
if (is_string($a_bool)) {
    echo "String: $a_bool";
}
?>

(1)Boolean 布尔类型

可以为TRUE或FALSE,不区分大小写。

一般非0,即为TRUE。

(2)Integer 整型

整型可以使用十进制,十六进制,八进制或二进制表示。八进制前面必须加0(零),十六进制加0x,二进制加0b。

如果给定的一个数超出了interger的范围,将会被解释为float。同样运算结果超出integer范围,同样如此。

php没有整除运算符,1/2 将产生出 float 0.5。可以强制转换为integer 或使用round() 更好的四舍五入。

echo (int)2.9; // 输出 2
echo round(2.555, 2) // 输出 2.56
// 决不要将未知的分数强制转换为 integer,这样有时会导致不可预料的结果。
<?php
echo (int) ( (0.1+0.7) * 10 ); // 显示 7!
?>

(3)Float 浮点型(double)

浮点型,也叫浮点数float,双精度double, 实数real。

<?php
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?>

(4)String 字符转

一个字符串string,就是由一系列的字符组成,其中每个字符等同于一个字节。这就意味着php只能支持256个字符集,因此不支持Unicode。

string最大可以达到2GB。

<?php
$a = 123;
echo '$a'; // 输出 $a
echo "$a"; // 输出 123, 转义字符 ''

$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;

?>

(5)Array 数组

php中的数组,实际上是一个有序序列。映射是把values关联到keys的类型。

由于数组元素的值也可以说是另外的数组,树形结构和多维数组也是允许的。

<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

// 自PHP 5.4 起
$array = [
    "foo" => "bar",
    "bar" => "foo",
]
// key 可以是 integer 或 string 类型
// key 值为可选项, 如果未指定,则使用之前用过最大的integer键名加上1作为新键名
?>

// 要修改某个值,通过其键名给该单元赋一个新值。
// 要删除某个键值对,对其调用 unset() 函数。

使用 unset() 需要注意,此时数组不会重建索引。需要重建索引,可以使用 array_values() 函数。

数组计算总数: 使用 count() 函数

(6)Object 对象

<?php
class foo{
    function do_foo(){
        echo "Doing foo.";
    }
}
// 用 new 实例化一个类
$f = new foo;
$f->do_foo;

(7)Resource 资源类型

资源 resource 是一种特殊的变量,保存了到外部资源的一个引用。资源是通过专门的函数来建立和使用的。

(8)NULL

特殊的NULL 表示一个变量没有值。NULL类型唯一可能的值就是NULL。

可被认定为NULL的变量:①被赋值为NULL ②尚未被赋值 ③被unset

(9)Callback 回调类型

自PHP5.4 起,可以使用 callable 类型 指定回调类型 callback。


三、变量

php中变量用一个美元符号 $ 后面跟变量名来表示的。区分大小写。

变量默认总是传值赋值。

<?php

$a = 1;
// 值传递赋值 
$b = $a
// 引用赋值
$c = &$a

// global 关键字
global ; $GLOBALS