编写位置|注释
文件.php后缀 须通过服务器解析后由浏览器打开,直接通过浏览器打开为纯文本 写在<?php?>之外的代码不会被解析,所以可以用来写其他HTML,css,js等代码
<?php
?>
设置编码格式|变量|输出
header ( 'content-type:text/html;charset=utf-8' ) ;
echo 'hello world!<br>' ;
echo '你好,世界! <br>' ;
$name = 'Hello PHP!' ;
echo $name ;
echo '<br>' ;
$PI = 3.1415926 ;
$male = true ;
字符串拼接|条件选择语句|循环语句
if ( $male == false ) {
echo '女' ;
} else {
echo '男' ;
}
echo '<br>' ;
$day = '星期日' ;
switch ( $day ) {
case '星期一' :
case '星期二' :
case '星期三' :
case '星期四' :
case '星期五' :
echo 'good good study, day day up!' ;
break ;
case '星期六' :
case '星期日' :
echo 'zzz' ;
break ;
default :
echo '???' ;
}
echo '<br>' ;
for ( $i = 0 ; $i < 5 ; $i ++ ) {
echo 'haha+' . $i . '<br>' ;
}
$i = 0 ;
while ( $i < 5 ) {
echo 'peipei+' . $i . '<br>' ;
$i ++ ;
} ;
do {
echo 'dodo+' . $i . '<br>' ;
$i -- ;
} while ( $i != 0 ) ;
数组相关
$foodArr = array ( '土豆' , '番茄' , '鸡蛋' ) ;
echo $foodArr [ 2 ] ;
echo '<br>' ;
print_r ( $foodArr ) ;
echo '<br>' ;
for ( $i = 0 ; $i < count ( $foodArr ) ; $i ++ ) {
echo $foodArr [ $i ] . '<br>' ;
}
$me = array ( 'name' = > 'kk' , 'age' = > 19 , 'male' = > true ) ;
echo $me [ 'name' ] ;
echo '<br>' ;
print_r ( $me ) ;
echo '<br>' ;
foreach ( $me as $key = > $value ) {
echo $key . '----' . $value ;
echo '<br>' ;
}
$peopleArr = array (
array ( 'name' = > 'aa' , 'age' = > 19 , 'male' = > true ) ,
array ( 'name' = > 'bb' , 'age' = > 18 , 'male' = > false ) ,
array ( 'name' = > 'vv' , 'age' = > 17 , 'male' = > true )
) ;
引入数据
data_fruit_list.php
<?php
$arr = array ( ) ;
$arr [ 0 ] = array ( 'href' = > 'detail/detail1.php?flag=bilibili' , 'path' = > 'img/bilibili.png' , 'name' = > '哔哩哔哩' ) ;
$arr [ 1 ] = array ( 'href' = > 'detail/detail1.php?flag=csdn' , 'path' = > 'img/csdn.png' , 'name' = > 'CSDN' ) ;
$arr [ 2 ] = array ( 'href' = > 'detail/detail1.php?flag=gitee' , 'path' = > 'img/gitee.png' , 'name' = > 'GitEE' ) ;
$arr [ 3 ] = array ( 'href' = > 'detail/detail1.php?flag=github' , 'path' = > 'img/github.png' , 'name' = > 'GitHub' ) ;
$arr [ 4 ] = array ( 'href' = > 'detail/detail1.php?flag=Google' , 'path' = > 'img/Google.png' , 'name' = > '谷歌' ) ;
$arr [ 5 ] = array ( 'href' = > 'detail/detail1.php?flag=Idea' , 'path' = > 'img/Idea.png' , 'name' = > 'Idea' ) ;
$arr [ 6 ] = array ( 'href' = > 'detail/detail1.php?flag=kugou' , 'path' = > 'img/kugou.png' , 'name' = > '酷狗' ) ;
$arr [ 7 ] = array ( 'href' = > 'detail/detail1.php?flag=Pycharm' , 'path' = > 'img/Pycharm.png' , 'name' = > 'Pycharm' ) ;
$arr [ 8 ] = array ( 'href' = > 'detail/detail1.php?flag=qq' , 'path' = > 'img/qq.png' , 'name' = > 'QQ' ) ;
$arr [ 9 ] = array ( 'href' = > 'detail/detail1.php?flag=weixin' , 'path' = > 'img/weixin.png' , 'name' = > '微信' ) ;
?>
引入
< ! DOCTYPE html>
< html lang= "zh-CN" >
< head>
< meta charset= "UTF-8" >
< meta name= "viewport" content= "width=device-width, initial-scale=1.0" >
< title> 数据生成页面< / title>
< style>
li{
width: 200 px;
height: 150 px;
float: left;
list- style: none;
}
img{
width: 50 px;
height: 50 px;
}
a, span{
display: block;
width: 80 px;
height: 50 px;
line- height: 50 px;
text- align: center;
}
a, span, img{
float: left;
}
< / style>
< / head>
< body>
< ! -- 使用php生成页面 -- >
< ! -- 引入其他的php页面 -- >
< ! --
1. 数据存储在数据库中,此处使用数组作为假数据
2. 用户访问页面时- 读取数据
3.
-- >
<?php
include './data_fruit_list.php' ;
echo '<ul>' ;
for ( $i = 0 ; $i < count ( $arr ) ; $i ++ ) {
echo '<li>' ;
echo '<a href="' . $arr [ $i ] [ 'href' ] . '">' . $arr [ $i ] [ 'name' ] . '</a>' ;
echo '<img src="' . $arr [ $i ] [ 'path' ] . '" alt="' . $arr [ $i ] [ 'name' ] . '">' ;
echo '<span>' . $arr [ $i ] [ 'name' ] . '</span>' ;
echo '</li>' ;
}
echo '</ul>' ;
?>
< / body>
< / html>
获取form表单提交的数据
form
< form action = " ./02getData.php" method = " get" >
< input type = " text" placeholder = " 请输入" name = " submit" >
< input type = " submit" >
</ form>
getData.php
<?php
header ( 'content-type:text/html;charset=utf-8' ) ;
echo '刚刚你的输入为:' . $_GET [ 'submit' ] ;
?>