PHP学习之一:PHP脚本、类型和变量
来源:互联网 发布:域名top 编辑:程序博客网 时间:2024/06/03 12:54
PHP的文档内容主要分为三部分:基本语言介绍、库函数说明、特性说明。其中基本语言介绍是基础部分,介绍了PHP语言的基本语法、类型、变量、运算符、流程控制、函数、类、命名空间等。库函数说明部分主要介绍了相关的PHP扩展函数库的内容。特性部分介绍了相关的web开发的内容如请求、会话、数据库连接等内容。其中第一部分是本系列文章的主要内容。
整体上来说,PHP在语法和语义方面和C或JAVA有许多类似的地方,只有少数地方有所区别。
本篇先记录一下PHP的基本使用、变量和类型。
1 如何运行一个PHP脚本
<?phpecho "Hello world"; //输出hello world?>
PHP解释器需要查找<?php ?>标记来解释相应的PHP脚本的开始和结束,对于不在标记中的内容PHP解释器会直接进行输出,一直到遇到下一个开始标记之前。如果整个文件都属于单个的开始和结束标记,则文件尾部的结束标记可以省略。
上面这个脚本可以命令行上执行,root@localhost# php hello.php。我们即可以看到输出。
PHP中的注释和C是一样的,支持单行注释 // 和多行注释 /* */ 。此外还支持unix shell风格的注释,#注释。
2 PHP支持的类型
PHP支持8种内置类型:bool int float string array object resource callable NULL
此外还有三种伪类型:mixed number callback
前4种内置类型不需要做额外说明,array支持线性和hash方式的存储与使用,数组使用时可以直接按位置来存储和访问。
object类型是通过new生成的类的实例。resource为PHP存储的外部资源的句柄,可以通过get_resource_type来查看相应的资源类型。callable可以理解为函数。
三种伪类型只用于文档中对函数对数进行说明,mixed表示类型不固守、number表示int或float、callback表示回调函数类型。
对于不同的类型PHP定义了is_int,is_string之类的类型判断符,get_type()以及set_type()等用于获取变量的类型。
不同的类型可以进行类型转换,转换有两种方式,一种为操作符,一种为函数。类型$a = (int) 132.3或$a = intval(132.3)
- (int), (integer) - cast to integer
- (bool), (boolean) - cast to boolean
- (float), (double), (real) - cast to float
- (string) - cast to string
- (array) - cast to array
- (object) - cast to object
- (unset) - cast to NULL (PHP 5)
3.1 变量使用
在定义前不需要声明,也不需要指定类型,变量由$符号开头。变量命名规则与C类似。变量名为大小写敏感的。
3.2 变量作用型类型
PHP的作用域有4种,全局、局部、静态、形参。
全局作用域的变量不在任务函数体中声明,非静态的变量。
局部变量声明于函数体内。形参变量声明于函数声明中。
若变量声明于include或require语句之前,则变量在被包含文件中可见。
值得注意的是PHP的全局作用域与C的作用域规则不同,PHP的全局作用域并不能作用于局部作用域中,若在局部需要引用全局变量,必须在局部作用域对全局变量进行声明。如下所示,对于全局变量$a并不能在函数test()中被访问到。若想使用则采用global $a声明。
<?php$a = 1; /* global scope */ function test(){ echo $a; /* reference to local scope variable */ } test();?>
3.3 外部变量
对于从HTML里提交的变量可以通过超级全局变量进行访问。
3.4 超级全局变量
PHP定义了若干个超级全局变量,用于方便web开发。如下所示:
- $GLOBALS
- $_SERVER : 描述web服务器以及客户端等的配置信息
- $_GET : 客户端的GET请求数据
- $_POST : 客户端的POST请求数据
- $_FILES : 客户端上传文件数据
- $_COOKIE : 客户端COOKIE数据
- $_SESSION : 客户端Session数据
- $_REQUEST : GET/POST的别名
- $_ENV :环境信息
4 常量
4.1 全局常量
define("PI", 3.1415926);
echo PI;
常量是全局可见的。
4.2 类常量
class Test{ const MATH_PI = 3.1415926; //...}
4.3 魔法常量
Name Description
__LINE__
The current line number of the file.__FILE__
The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2,__FILE__
always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.__DIR__
The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent todirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.)__FUNCTION__
The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.__CLASS__
The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. The class name includes the namespace it was declared in (e.g.Foo\Bar). Note that as of PHP 5.4 __CLASS__ works also in traits. When used in a trait method, __CLASS__ is the name of the class the trait is used in.__TRAIT__
The trait name. (Added in PHP 5.4.0) As of PHP 5.4 this constant returns the trait as it was declared (case-sensitive). The trait name includes the namespace it was declared in (e.g.Foo\Bar).__METHOD__
The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).__NAMESPACE__
The name of the current namespace (case-sensitive). This constant is defined in compile-time (Added in PHP 5.3.0).- PHP学习之一:PHP脚本、类型和变量
- php中的类型和变量
- PHP学习笔记(十):HTML如何显示PHP中的变量和脚本中的变量
- PHP学习(三)--变量的类型
- php (三) PHP变量类型
- php变量所有类型和转换
- PHP 测试和设置变量类型
- 006.php变量和常量类型
- PHP伪类型和伪变量
- PHP变量及其类型和范围
- PHP学习笔记——php变量类型;
- PHP 的 变量类型
- php判断变量类型
- 004.php变量类型
- php判断变量类型
- php变量类型
- php变量类型转换
- 【PHP】判断变量类型
- Gentoo使用WPA2-PSK
- 状态栏SystemUI的启动过程
- C#调用C/C++ 动态链接库DLL(二)
- MyEclipse内存溢出问题
- Vim配置
- PHP学习之一:PHP脚本、类型和变量
- UVa10026 - Shoemaker's Problem
- VC++图形平移功能的实现
- REST架构之Apache Wink介绍
- 随机函数
- PHP提取emule下载ed2k链接
- 地图符号化-柱状图
- 黑马程序员——Java的基本构成元素
- Perl 引用变量传入函数中的行为