《PHP4核心编程》读书笔记

Gavin [PHP]

2008.11.15

1、PHP的4种编码方式:

<1> 标准记法

1
2
3
4
5
<?php
 
/* code here*/
 
>

适用于XML分析器

<2> 脚本标记法

 查看代码 JAVASCRIPT
1
2
3
<SCRIPT LANGUAGE = "php">
 
</SCRIPT>


<3> ASP风格记法

1
2
3
4
5
<%
 
/* code here*/
 
%>

或者:

1
2
3
4
5
<%=
 
/* code here*/
 
%>

仿效ASP风格的标记符方式

<4>“短标记”法

1
2
3
4
5
<?
 
/* code here*/
 
?>

或:

1
2
3
4
5
<?
 
/* code here*/
 
>

2、空白区域,包括空格、tabs、回车返回等被忽略

3、注释方式:

  <1>多行注释:/*…*/

  <2>单行注释://…

4、PHP对其“内置函数”大小写不敏感,但对变量大小写敏感

5、变量的声明,在变量名前加美元符($),如:$YourName变量的类型不用声明,PHP自动计算

6、PHP程序的基本框架:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<HTML>
 
<HEAD>
 
<TITLE>A Title</TITLE>
 
</HEAD>
 
<BODY>
 
/* Some HTML code */
<?php
 
	/* Some PHP code */
 
?>
 
/* Some HTML code */
</BODY>
 
</HTML>

注:php文件的命名尽量只使用小写字母、数字、破折号”-”和”.”号,如:do-it.php

7. 一些例子
<1>给变量赋值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?
$YourName = "Leon";
$Today = date("l F d, Y");
$CostofLunch = 3.50;
$DaysBuyingLunch = 4;
?>
<HTML>
<HEAD><TITLE>Listing 1-3</TITLE></HEAD>
<BODY>
Today's Date:
<? /* print today's date */
print("");
 
/* print message about lunch cost */
print("$YourName, you will be out");
print("$CostofLunch * $DaysBuyingLunch");
print("dollars this week.<BR>\n");
?>
</BODY>
</HTML>

<2>1-4.php调用1-5.php,1-4.php显示午餐信息的HTML表单,1-5.php从表单计算午餐费用
1-4.php:

1
2
3
4
5
6
7
8
9
10
11
12
<HTML>
<HEAD><TITLE>Listing 1-4</TITLE></HEAD>
<BODY>
<FORM ACTION = "1-5.php" METHOD = "post">
Your Name:
<INPUT TYPE = "text" NAME = "YourName"><BR>
Cost of a Lunch:
<INPUT TYPE = "text" NAME = "DaysBuyingLunch"><BR>
<INPUT TYPE = "submit" NAME = "x"> VALUE = "Compute">
</FORM>
</BODY>
</HTML>

1-5.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?
$Today = date("l F d, Y");
?>
<HTML>
<HEAD><TITLE>Listing 1-5.php</TITLE></HEAD>
<BODY>
Today's Date:
<?
/* print today's date */
print("<H3>$Today</H3>\n");
 
/*print message about lunch cost*/
print("YourName, you will be out");
print("CostofLunch * $DaysBuyingLunch");
print("dollars this week.<BR>\n");
?>
</BODY>
</HTML>

8、PHP的标识符:
<1>由字母、数字或下划线组成,第一个字符只能是字母或下划线
<2>标识符大小写敏感,但内置函数例外

9、数据类型:
<一>基本数据类型
整 形:integer
浮点数:双精度浮点数double
文本串:用双引号”或‘括起来String

<二>集合数据类型
数组 :array
对象 :object
布尔数:boolean
资源 :标识系统资源的整数
注:a.PHP不用声明变量的具体类型
b.使用settype函数、类型转换函数或定型可以改变变量的当前类型

10、一段程序示例:类型转换示例:
2-1.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?
print("<B>Using settype</B>");
$AverageTemperature = "60.5 degree";
print("String: $AverageTemperature <BR>\n");
Settype($AverageTemperature, "double");
print("Double: $AverageTemperature <BR>\n");
 
Settype($AverageTemperature, "integer");
print("Integer: $AverageTemperature <BR>\n");
 
Settype($AverageTemperature, "string");
print("String: $AverageTemperature <BR>\n");
 
print("<B>Using strval, intval,");
print("and doubleval</B><BR>\n");
$AverageTemperature = "60.5 degrees";
 
print("String: ");
print(strval($AverageTemperature));
print("<BR>\n");
 
print("Double: ");
print(doubleval($AverageTemperature));
print("<BR>\n");
 
print("Integer: ");
print(intval($AverageTemperature));
print("<BR>\n");
 
print("Original: ");
print($AverageTemperature);
print("<BR>\n");
 
print("<B>Using casts</B>");
$AverageTemperature = "60.5 degree";
 
print("String: ");
print((string)$AverageTemperature);
print("<BR>\n");
 
print("Double: ");
print((double)$AverageTemperature);
print("<BR>\n");
 
print("Integer: ");
print((integer)$AverageTemperature);
print("<BR>\n");
 
print("Original: ");
print($AverageTemperature);
print("<BR>\n");
?>

2-1.php运行结果
Using settype
String:60.5 degrees
Double:60.5
Integer:60
String:60
Using strval, intval, and doubleval
String:60.5 degrees
Double:60.5
Integer:60
Original:60.5 degrees
Using casts
String:60.5 degrees
Double: 60.5
Integer:60
Original:60.5 degrees

11、在PHP中创建一个函数时,必须明确告诉PHP在该函数中将要出现的全局变量(用global关键字)

12、关键字static可以实现全局变量的功能,而且更好:
2-3.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?
function useColor()
{
/* remember the last color we used */
static $ColorValue;
 
/* choose the next color */
if($ColorValue == "#00FF00")
{
$ColorValue = "#CCFFCC";
}
else {
$ColorValue = "#00FF00";
}
return ($ColorValue);
}
print ("<TABLE WIDTH = \"300\">\n");
for($count = 0; $count < 10; $count++)
{
/* get color for this row */
$RowColor = useColor();
 
/* print out HTML for row set background color */
print("<TR><TD BGCOLOR = \"$RowColor\">")
 
print("Rownumber $count</TD></TR>\n");
}
print("</TABLE>\n");
?>

13、使用<<<符号标识出一个文本区域的开始,紧跟一个字符串标识符,当这个字符串标识符单独地出现在一行时,PHP认为字符串已到末尾,标识符习惯用HERE或EOD(end of data)
如:2-4.php:

1
2
3
4
5
6
7
8
9
10
11
12
<? $text = <<<HERE
"This text an contain both double quotes and Single quotes". It's simple.
 
Note that the line break following the first HERE and the one begore the last HERE are not included int the string. And PHP is smart enough to recognize that the line abouve was not the real end of the string.
 
Your can also emeed variables and backslash codes in this string.
 
The only downside is that any tabs or spaces you use to index the text will pass through,too.
HERE;
 
print("$text\n");
?>

14、运算符:
<1>算术运算符:+ – * / % ++ –
<2>赋值运算符: =
<3>逻辑和关系运算符:
< > <= >= == != AND&& OR|| XOR !
<4>位运算符:
$ | ^ ~ >> <<
<5>复合运算符:
= += -= *= /= %= &= |= ^= .=
<6>其他运算符:
.并置,联接两个字符串
$引用一个变量
&引用变量存储
->引用一个类方法或特性
=>设置缺省参数或分配矩阵元素索引
@抑制函数错误
?三段条件表达式 

//~待续


评论

输入后可按 Ctrl+Enter 提交评论.