Blogger Information
Blog 38
fans 0
comment 0
visits 22714
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
变量作用域及web1项目优化
一个好人
Original
455 people have browsed it

变量作用域

变量有作用域,外部与函数内互不可见,除非成为全局变量,两种方式,可以通过global 声明为全局变量,或变为$GLOBALS超全局变量的参数

  1. $one = 200;
  2. $two = 300;
  3. const NATION ='中国';
  4. function test()
  5. {
  6. // 1.
  7. // global $one, $two;
  8. // 2. $GLOBALS超全局变量
  9. echo NATION;
  10. echo "运算的结果是".($GLOBALS['one'] + $GLOBALS['two']);
  11. }

web1优化:

  1. include __DIR__.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'common.php';
  2. include TEMP_PATH_PUBLIC . DS . 'header.php';
  3. $news = require DATA_PATH. DS . 'news.php';
  4. $items = require DATA_PATH . DS . 'item.php';
  5. ?>
  6. <!-- 主体 -->
  7. <main>
  8. <!-- 新闻列表 -->
  9. <div class="news">
  10. <h3>新闻列表</h3>
  11. <div class="list">
  12. <?php for($i=0; $i<10; $i++): ?>
  13. <a href="<?= $news[$i]['url'] ?>">
  14. <?=
  15. strlen($news[$i]['title'])>48? mb_substr($news[$i]['title'], 0,16).'...':$news[$i]['title'];
  16. ?>
  17. </a>
  18. <?php endfor ?>
  19. </div>
  20. </div>
  21. <!-- 产品列表 -->
  22. <div class="items">
  23. <h3>产品列表</h3>
  24. <div class="list">
  25. <?php foreach($items as $k=>$v): extract($v)?>
  26. <div class="item" id="item">
  27. <img src="<?=$img?>" alt="<?= $title?>" />
  28. <a href="<?=$url?>"><?= strlen($title)>45? mb_substr($title, 0,15).'...':$title; ?></a>
  29. </div>
  30. <?php endforeach ?>
  31. </div>
  32. </div>
  33. </main>
  34. <!-- 页脚 -->
  35. <?php
  36. include TEMP_PATH_PUBLIC . DS . 'footer.php';
  37. ?>
  1. <!-- 新闻列表 -->
  2. <div class="news">
  3. <h3>新闻列表</h3>
  4. <div class="list">
  5. <?php foreach($news as $k=>$v): extract($v)?>
  6. <a href="<?= $news[$i]['url'] ?>"><?=
  7. strlen($title)>48? mb_substr($title, 0,16).'...':$title;
  8. ?></a>
  9. <?php endforeach ?>
  10. </div>
  11. </div>

优化包括以下内容:
1、定义常量,简化代码;
2、超过16个汉字的标题取前16个字并加点;
3、首页采用for循环取前10条记录,新闻页用foreach循环显示左右记录。

总结:

我觉得DS常量可以加到路径里面;网页中首页词条始终是红的,其他词条激活了也不能变色,这些都是需要后续完善的。

Correcting teacher:PHPzPHPz

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