1 PHP代碼格式
//index.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content ="text/html; charset=utf-8">
<title>標(biāo)題</title>
</head>
<body>
<p>
<?php
//php代碼
?>
</p>
</body>
</html>
<a id=>
2 第一個PHP程序
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content ="text/html; charset=utf-8">
<title>標(biāo)題</title>
</head>
<body>
<p>
<?php
echo 'hello world!';
?>
</p>
</body>
</html>
3 PHP數(shù)據(jù)類型
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content ="text/html; charset=utf-8">
<title>標(biāo)題</title>
</head>
<body>
<p>
<?php
//整形
$intValue = 1314;
echo $intValue;
echo "<br />";
//浮點型
$floatValue = 13.14;
echo $floatValue;
echo "<br />";
//布爾型
$booleanValue = TRUE;
echo $booleanValue;
echo "<br />";
$booleanValue = FALSE;
echo $booleanValue;
echo "<br />";
//字符串
$stringValue = "Sam123";
echo $stringValue;
echo "<br />";
//數(shù)組
$arrayValue = array(1,3,1,4);
print_r($arrayValue);
echo "<br />";
//空
$nullValue = null;
echo $nullValue;
?>
</p>
</body>
</html>
4 用var_dump查看數(shù)據(jù)類型
//index.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content ="text/html; charset=utf-8">
<title>標(biāo)題</title>
</head>
<body>
<p>
<?php
$a = 10;
$b = 'abcd';
var_dump($a);
var_dump($b);
?>
</p>
</body>
</html>
5 字符串
<?php
$sam="sam";
$s1 = "hello,$sam";
$s2 = 'hello,$sam';
echo $s1;
echo "<br />";
echo $s2;
?>
6 數(shù)據(jù)提交
//index.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content ="text/html; charset=utf-8">
<title>sam</title>
</head>
<body>
<form action="data.php" method="post">
<label for="username">請輸入姓名</label>
<input type="text" name="username" value="請輸入姓名">
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>
\\data.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>歡迎學(xué)習(xí)php!</title>
</head>
<body>
<?php
$username = $_POST["username"];
echo $username.",你好!";
?>
</body>
</html>
7 php連接數(shù)據(jù)庫
/**
*PHP連接數(shù)據(jù)庫要經(jīng)過三個步驟:
*(1)設(shè)置初始變量票渠;
*(2)連接數(shù)據(jù)庫;
*(3)判斷連接情況嘉涌。
*/
<?php
/*步驟一:設(shè)置初始變量*/
$host = "localhost";
$user = "root";
$password = "123456";
/*步驟二:連接MySQL服務(wù)器*/
$conn = mysql_connect($host,$user,$password);
/*步驟三:判斷連接結(jié)果*/
if(!$conn) {
die("連接數(shù)據(jù)庫失敗。".mysql_error());
}
else {
echo "MySQL服務(wù)器:$host <br />用戶名:$user <br />";
echo "成功連接數(shù)據(jù)庫。";
}
mysql_close();//關(guān)閉數(shù)據(jù)庫
?>