日期:2014-05-17  浏览次数:20380 次

PHP基础开发代码示例~
最近打算重拾PHP来开发一些小型应用,很久没用PHP了,有些语法都生疏了,今天上午写了三个例子,基本上把之前的PHP复习了一下。

基础语法操作:
<?php
//输出的方式测试
echo '###################输出测试################<br>';
echo '测试输出1(单引号)<br>';
echo "测试塑出2(双引号)<br>";
?>
<?='测试输出3(=表达式)<br>'?>
<? echo '测试输出4(单个表达式)<br>'; ?>

<?php 
//类型
echo '###################类型测试################<br>';
$testInt = 1;
$testStr = "字符串";
$testFloat = 1.23;
$testBoolean = false; //false/true:如果转换成字符串则为空/"1",转换成整型则为0/1
$testArray = array("first"=>1,2,3,4,5);
$testStr2 = $testStr; //testStr修改,testStr2不会修改
$testStr3 = &$testStr; //testStr修改,testStr3也会修改

echo '整型:'.$testInt.'<br>';
echo '字符串型:'.$testStr.'<br>';
$testStr = '字符串修改了';
echo '字符串型修改后:'.$testStr.'<br>';
echo '字符串型修改后(2):'.$testStr2.'<br>';
echo '字符串型修改后(3):'.$testStr3.'<br>';

echo '浮点型:'.$testFloat.'<br>';
echo '布尔型:'.(int)$testBoolean.'<br>';
echo '数组测试(1):'.$testArray[0].'<br>';
echo '数组测试(2):'.$testArray['first'].'<br>';
print_r($testArray);
echo '<br>';
foreach ($testArray as $i => $value) {
    echo '数组迭代结果:'.$i."->".$value."<br>";
}

//IO操作
echo '##################IO操作####################<br>';
echo '读取该文件的内容:<br>';
$theFileName = "test_base.php"; //文件名称或者全路径
$handle = fopen ($theFileName, "rb");
$contents = fread ($handle, filesize ($theFileName));
fclose ($handle); 
echo '<div style="border:1px solid #aaa;color:blue;"><pre>'.htmlspecialchars($contents).'</pre></div>';

?>



数据库的处理:
<?php

//四种引用方式
//require 'test_base.php'; //无条件的引用,报错会终止应用,引用的文件只处理一次
//require_once 'test_base.php'; //无条件的引用,报错会终止应用,引用的文件只处理一次并只显示一次
//include 'test_base.php'; //可有条件的引用,报错不会终止应用,引用的文件处理多次
//include_once 'test_base.php'; //可有条件的引用,报错不会终止应用,引用的文件处理多次但只显示一次

//数据库的测试
$hostname = '192.168.1.6'; /*数据库服务器访问地址*/
$username = 'root'; /*数据库用户帐号*/
$password = 'root'; /*数据库密码*/
$database = 'phptest'; /*数据库名称*/
$databaseCharset = 'GBK'; /*数据库编码,防止插入中文乱码和报错*/

//获取请求信息
$actionSubmit = $_REQUEST['submit'];
$reqTheType = $_REQUEST['theType'];
if($reqTheType == null || $reqTheType == '') {
	$reqTheType = '1';
}
echo '请求信息:'.$actionSubmit."|".$reqTheType.'<br>';

if($actionSubmit != null && $actionSubmit != '') {
	if($reqTheType == '1') {
		testSearch();
	}
	if($reqTheType == '2') {
		testInsert();
		testSearch();
	}
	if($reqTheType == '3') {
		testUpdate();
		testSearch();
	}
}

/**
 * 数据库查询
 * Enter description here ...
 */
function testSearch() {
	echo '查询数据<br>';
	global $hostname,$username,$password,$database,$databaseCharset;
	
	$currentConn = null;
	$currentConn = mysql_connect ( $hostname, $username, $password );
	mysql_select_db ( $database );
	mysql_query("set names charset ".$databaseCharset);
	mysql_query("set names ".$databaseCharset);
	$result = mysql_query ( "select * from e_user" ); //查询动作返回的是result结果集
	while ( $row = mysql_fetch_object ( $result ) ) {
		echo $row->uri . "\t" . ($row->username) . "<br>";
	}
	mysql_free_result ( $result );
	mysql_close ( $currentConn );
}

/**
 * 数据库数据添加
 * Enter description here ...
 */
function testInsert() {
	global $hostname,$username,$password,$database,$databaseCharset;
	$insertSql = "insert into e_user(uri,username,password) values";
	$insertSql .= "(";
	$insertSql .= "'".generateId()."','测试用户','123456'";
	$insertSql .= ")";
	$currentConn = null;
	$currentConn = mysql_connect ( $hostname, $username, $password );
	mysql_select_db ( $database );
	mysql_query("set names charset ".$databaseCharset);
	mysql_query("set names ".$databaseCharset);
	echo '添加数据'.$insertSql.'<br>';
	$result = mysql_query($insertSql); //插入动作返回的是boolean
	if(!$result) {
		die('Error: ' . mysql_error());
	}
	mysql_close ( $currentConn );
}

/**
 * 数据库修改
 * Enter description here ...
 */
function testUpdate() {
	global $hostname,$username,$pas