1 PHP代码格式
//index.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content ="text/html; charset=utf-8">
<title>标题</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>标题</title>
</head>
<body>
<p>
<?php
echo 'hello world!';
?>
</p>
</body>
</html>
3 PHP数据类型
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content ="text/html; charset=utf-8">
<title>标题</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 />";
//数组
$arrayValue = array(1,3,1,4);
print_r($arrayValue);
echo "<br />";
//空
$nullValue = null;
echo $nullValue;
?>
</p>
</body>
</html>
4 用var_dump查看数据类型
//index.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content ="text/html; charset=utf-8">
<title>标题</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 数据提交
//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>欢迎学习php!</title>
</head>
<body>
<?php
$username = $_POST["username"];
echo $username.",你好!";
?>
</body>
</html>
7 php连接数据库
/**
*PHP连接数据库要经过三个步骤:
*(1)设置初始变量;
*(2)连接数据库;
*(3)判断连接情况。
*/
<?php
/*步骤一:设置初始变量*/
$host = "localhost";
$user = "root";
$password = "123456";
/*步骤二:连接MySQL服务器*/
$conn = mysql_connect($host,$user,$password);
/*步骤三:判断连接结果*/
if(!$conn) {
die("连接数据库失败。".mysql_error());
}
else {
echo "MySQL服务器:$host <br />用户名:$user <br />";
echo "成功连接数据库。";
}
mysql_close();//关闭数据库
?>
网友评论