Blogger Information
Blog 29
fans 0
comment 0
visits 18382
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php入门,变量声明,注意事项,变量,数组
CC
Original
538 people have browsed it

php入门

1.网页中当遇到php的格式文件,php解释器h会立即介入处理
2.变量声明$
3.数组可以按下标索引取值
4.遍历foreach
5. 一个建议,当大量的html与php进行混编时,尽可能不要出现echo 和 大括号 “{ … }”,模板语法

  • php文件一般格式
  1. <?php
  2. echo'<h1>大家晚上好</h1>';
  3. ?>
  • php跟html混编
    写法1
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title><?php echo'天蓬影视'; ?></title>
  7. </head>
  8. <body>
  9. <h2><?php echo'国产好剧'; ?></h2>
  10. <ol>
  11. <li><a href=""><?php echo'我的'; ?></a></li>
  12. <li><a href="">我就是这般男子</a></li>
  13. <li><a href="">这个世界看眼</a></li>
  14. </ol>
  15. </body>
  16. </html>

写法2(php文件与html分开)

  1. <?php
  2. $title = '天蓬影视';
  3. $headline='国产好剧';
  4. ?>
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="UTF-8">
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10. <title><?php echo $title; ?></title>
  11. </head>
  12. <body>
  13. <h2><?php echo $headline; ?></h2>
  14. <ol>
  15. <li><a href=""><?php echo $headline ; ?></a></li>
  16. <li><a href="">我就是这般男子</a></li>
  17. <li><a href="">这个世界看眼</a></li>
  18. </ol>
  19. </body>
  20. </html>

写法3:如果是只打印一个输出变量,可以使用短标签简写,下标所以取值

  1. <?php
  2. $title = '天蓬影视';
  3. $headline='国产好剧';
  4. // 声明php数组
  5. $move=['我的小幸运','我爱我的祖国','我爱我爱的家乡'];
  6. ?>
  7. <!DOCTYPE html>
  8. <html lang="en">
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  12. <title><?= $title; ?></title>
  13. </head>
  14. <body>
  15. <!-- <h2><?= $headline; ?></h2> -->
  16. <h2><?= $headline; ?></h2>
  17. <ol>
  18. <li><a href=""><?echo $move[1] ; ?></a></li>
  19. <li><a href=""><?echo $move[2] ; ?></a></li>
  20. <li><a href=""><?echo $move[2] ; ?></a></li>
  21. </ol>
  22. </body>
  23. </html>
  • 遍历

    foreach (数组 as 循环变量) {
    显示循环变量
    }
    .(点)是连接符号,不是方法

  1. <?php
  2. $title = '天蓬影视';
  3. $headline='国产好剧';
  4. // 声明php数组
  5. $moves=['我的小幸运','我爱我的祖国','我爱我爱的家乡'];
  6. ?>
  7. <!DOCTYPE html>
  8. <html lang="en">
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  12. <title><?= $title; ?></title>
  13. </head>
  14. <body>
  15. <!-- <h2><?= $headline; ?></h2> -->
  16. <h2><?= $headline; ?></h2>
  17. <ol>
  18. <?php
  19. foreach($moves as $move){
  20. // .连接符号
  21. echo '<li><a href="">' . $move .'</a></li>';
  22. }
  23. ?>
  24. </ol>
  25. </body>
  26. </html>

模板语法

  1. <?php
  2. $title = '天蓬影视';
  3. $headline='国产好剧';
  4. // 声明php数组
  5. $moves=['我的小幸运','我爱我的祖国','我爱我爱的家乡'];
  6. ?>
  7. <!DOCTYPE html>
  8. <html lang="en">
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  12. <title><?= $title; ?></title>
  13. </head>
  14. <body>
  15. <!-- <h2><?= $headline; ?></h2> -->
  16. <h2><?= $headline; ?></h2>
  17. <ol>
  18. <!-- 使用模板替代语法将“大括号” 和 echo 干掉 -->
  19. <?php foreach ($moves as $move) : ?>
  20. <li><a href=""><?=$move?></a></li>
  21. <?php endforeach ?>
  22. </ol>
  23. </body>
  24. </html>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post