最近因项目需要,开始使用PHP技术,本文是按照一个java程序员的角度学习PHP的一些经验和感受,后期会根据学习使用的状况,进行增加相应的内容,本文主要是基础知识的总结。本文基于的PHP都是PHP5的版本。
1 arry 的类型
这是一个数值类型,和java语言有部分相似的地方。
1.1 array的定义
定义有三种形式:
形式一
$myArray = new array{"test","shanghai","China"};
形式二:
$myArray[0]="test";
$myArray[1]="shanghai";
$myArray[2]="China";
形式三:
$myArray = new array{
"myFirst"=>"test",
"mySecond"=>"shanghai",
"myThr"=>"China"};
形式四:
$myArray["myFirst"]="test";
$myArray["mySecond"]="shanghai";
$myArray["myThr"]="China";
2 循环
2.1 PHP中的循环语句包括while, for, swith,foreach, 其中前面三中和java的语法基本相同。只有foreach我们java中没有,
但是这个功能和JDK1.5以后新的for特性相似。
2.2 foreach的使用:
2.2.1 简单使用,仅仅对array定义的形式一和形式二支持
foreach ( $myArray as $value){
print "$value";
}//end f
对形式三和四的支持
foreach ($myArray as $field => $value) {
print "$fild, $value";
}
2.2.2 foreach 对PHP传递参数的应用:
一般的PHP的配置信息 register_globals 都会关闭,这样,我们传递的参数都需要手动获取,下面是手动获取的方式。
foreach ($_REQUEST as $field => $value){
print <<<HERE
<tr>
<td>$field</td>
<td>$value</td>
</tr>
HERE;
} // end foreach
说明:获取传递参数的数据还可以从
$HTTP_POST_VARS(所有post的参数),$HTTP_GET_VARS(所有get的参数)
2.3 定义数组
$theFiles[] = $currentFile;
使用该数组的时候,使用theFiles就可以了
3 String的处理:
3.1 split()
不一个字符串解析到一个数据中,
接受两个参数 分割表示和需要分割的字符串。
比如:
$words = split(“ “, $inputString);
3.2 rtrim()
自动删除字符末尾的 spaces, tabs, newlines and other whitespace.
返回字符串,参数:需要处理的字符串。
相关函数:ltrim()
3.3 substr( )
获取第一个字串。
参数: 需要处理的字符串,从什么位置开始,获取多少个字符串
$firstLetter = substr($theWord, 0, 1);
3.4 strlen()
获取字符串的长度
参数: 字符串
返回:字符串的长度
3.5 strstr()
判断某个字符串里面是否包含字串
参数:需要配置的字串,需要查找的字符串
返回:boolean类型
if (strstr(“aeiouAEIOU”, $firstLetter)){
3.6 字符串连接 " ."
$newWord = $theWord . “way”;
3.7 ord()
ASCII 转换成字符串
$theNumber = random(65, 90);
$theLetter = ord($theNumber);
3.8 stroupper
把字符串转换成大写
$wordList = strtoupper($wordList);
4 文件操作:
4.1 fopen
参数:文件名,打开类型
返回:一个文件对象
Modifier Type Description
“r” Read-only Program can read from the file
“w” Write Writes to the file, overwriting it if it already exists
“a” Append Writes to the end of the file
“r+” “w+” Read and write Random access. Read or write to a specified part of the file
注意:如果一个文件已经打开了,创建相同的文件,PHP将会创建一个新文件,并且覆盖旧文件
$fp = fopen(“sonnet76.txt”, “w”);
4.2 fputs()
参数:打开的文件变量,需要加入的内容
返回:boolean
fputs($fp, $sonnet76);
4.3 fclose()
参数:打开的文件变量
返回:boolean
fclose($fp);
4.4 fgets()
读文件的一行,游标会向下移动,下一次使用的时候,读下一条
参数:文件句柄
返回:一行的内容
$line = fgets($fp);
4.5 feof()
判断文件是否已经达到底端
参数:文件句柄
返回:boolean:true 表示达到底端
while (!feof($fp)){
4.6 readFile()
读文件
4.7 file()
读文件,把文件保存在数组中
参数:文件名称
返回:按行保存的数组
$sonnet = file($fileName);
4.8 str_replace()
参数:需要替换的字母,替换后的字母,需要替换的字符串
返回:完成替换的字符串
$currentLine = str_replace(“r”, “w”, $currentLine);
4.9 openDir()
参数:文件路径
返回:路径句柄
$dp = opendir($dirName);
说明:函数的具体定义在 php5/standard.php
4.10 readdir()
读文件夹下面的文件,每次返回一个,并且指针向下移动一位
参数:文件夹句柄
返回:文件名称或者false,false表示已经没有文件可以读了。
$currentFile = readDir($dp);
$theFiles[] = $currentFile;
4.12 preg_grep()
该命令有点象linux的grep命令,获取资金感兴趣的内容
参数:感兴趣的字符,需要查找的文件夹数组
返回:需要的内容数组
$imageFiles = preg_grep(“/jpg$|gif$/”, $theFiles);
参数一 的所有类型:P221
4.13 list()
5 XML
5.1 simpleXML API
5.1.1 simplexml_load_file()
load xml进入变量中, $xml = simplexml_load_file(“main.xml”);
5.1.2 implexml_load_string().
5.1.3 asXML()
转换成可以显示的形式
$xmlText = $xml->asXML();
5.1.4 htmlentities
转换字符串格式
$xmlText = htmlentities($xmlText);
print "<pre>$xmlText</pre> /n";
输出的时候注意加上<pre>
5.1.5 获取某一个知道元素的值
print $xml->title;
5.1.6 获取所有元素的名称和值
foreach ($xml->children() as $name => $value){
print “<b>$name:</b> $value<br /> /n”;
} // end foreach
其中的$xml->children() 返回一个数组
6 Mysql 部分
数据库连接的PHP部分:
$conn = mysql_connect(“localhost”, “”, “”);
mysql_select_db(“chapter7”, $conn);
//create a query
$sql = “SELECT * FROM hero”;
$result = mysql_query($sql, $conn);
6.2 获取field
while ($field = mysql_fetch_field($result)){
print “ <th>$field->name</th>/n”;
} //
field 具有的属性
Property Attribute
max_length Field length; especially important in VARCHAR fields
Name The field name
primary_key TRUE if the field is a primary key
Table Name of table this field belongs to
Type This field’s datatype
6.3 mysql_fetch_assoc 获取每一行的值
while ($row = mysql_fetch_assoc($result)){
print “<tr>/n”;
//look at each field
foreach ($row as $col=>$val){
print “ <td>$val</td>/n”;
} // end foreach
print “</tr>/n/n”;
}// end while
如果没有值,fetch_assoc 将返回false
6.4 mysql_fetch_object()
获取一行保存到对象中
6.5 mysql_fetch_array()
获取数组