《PHP与MySQL程序设计》第三章 PHP基础

WBOY
Release: 2016-06-23 14:29:28
Original
954 people have browsed it


1. PHP标识

Web服务器只向PHP解析器传递带有PHP标识的行,来提高效率。

四种:, ?>, ,

2. 注释

// 单行, /* */ 多行, # 单行

3. 向浏览器输出

print: 1表示输出成功。echo没有返回值所以略快。

     $season ="summertime";

     print "

I love the $season

";

printf格式化输出:

printf("%d bottles cost %.2f", 100, 43.20);

sprintf生成字符串:

$cost = sprint("%.2f", 43.2);

4. 数据类型

布尔:$alive = false, 0, 整数:43, 041, 0x1F

浮点:4.2, 字符串:"test"

数组:

     $array[0]=1, $array[1]..., 

     $state["LA"]=90, $state...

对象:

class Appliance {

     private $_power;

     function setPower($status) {

          $this->_power = $status;

     }

5. 变量

赋值:

$value1 = "hello";

$value2 =& $value1;

作用域:

局部变量:

     $x = 4;

     function assignx() {

          $x = 0;

          print($x); // 0

     }

     print($x); // 4

函数参数:函数执行结束后就被撤销

全局变量:

     $x = 4;

     function assignx() {

          GLOBAL $x;

          $x++;      // 5

     }

静态变量:递归调用时很有用

     function keep_track() {

          STATIC $count = 0;

          $count++;

     }

     keep_track();

     keep_track();

     keep_track();     // $count is 3

超级全局变量:获得当前用户会话、环境变量等信息

     foreach($_SERVER as $var => $value) {

          echo "$var => $value
";

     }

6. 常量

常量是全局的,并且引用前面不需要美元符。

define("PI", 3.141592);

7. 字符串插入

双引号:解释转义符和变量

单引号:按原样输出

heredoc语法:不用引号,而用两个相同标识符来界定字符串。

     echo

     

...

     EXCERPT;

8. 控制结构

foreach两种结构

普通数组:foreach($array as $var) ...

键和值数组:foreach($array as $key => value) ...

文件包含语句:

include("path/to/filename")

require:不管require语句在哪,指定文件都会载入。即使require放入false的判断中。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!