php中的<?= ?>替换<?php echo ?>

首先修改PHP.ini文件.
如下:

1. 将short_open_tag = Off 改成On

开启以后可以使用PHP的短标签:<? ?>

<?= $test ?>来代替 <?php echo $test ?>

同时,只有开启这个才可以使用 <?= 以代替 <? echo
 
2. 将 asp_tags = Off 改成On

同样可以在php中

<%= $test  %> 来替代<?php echo $test ?>


但是短标签不推荐使用

=============================

<? ?>是短标签
<?php ?>是长标签
在php的配置文件(php.ini)中有一个short_open_tag的值,开启以后可以使用PHP的短标签:<? ?>
同时,只有开启这个才可以使用 <?= 以代替 <? echo 。在CodeIgniter的视频教程中就是用的这种方式。
但是这个短标签是不推荐的,使用<?php ?>才是规范的方法。只是因为这种短标签使用的时间比较长,这种特性才被保存了下来。

==========================================

 

 

 

中间这种写法,不管short_open_tag 是 Off还是on都可以正常执行,不管PHP5.6还是PHP5.3,还是php7.1一样,short_open_tag不生效;
但asp_tags是可以生效的,在php7.0弃用这属性。

 


 

<?php // 连接数据库 $conn = new mysqli("localhost", "root", "123456", "wyya"); // 检查连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 查询五个歌单的表 $tables = array(); $result = mysqli_query($conn, "SHOW TABLES LIKE '%_list'"); if ($result->num_rows > 0) { while ($row = mysqli_fetch_array($result)) { $tables[] = $row[0]; } } // 将歌单包含的语种代码替换成对应的语种名称 function getLanguageName($languageCode) { switch($languageCode) { case "one": return "华语"; case "two": return "欧美"; case "three": return "日语"; case "four": return "韩语"; case "five": return "粤语"; default: return ""; } } // 获取选的歌单表 $tableName = isset($_GET["table"]) ? $_GET["table"] : ""; $data = array(); if (!empty($tableName)) { $result = mysqli_query($conn, "SELECT * FROM $tableName"); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $row["language"] = getLanguageName($row["language"]); $data[] = $row; } } } ?> <!DOCTYPE html> <html> <head> <title>网易云音乐歌单</title> <style> table { border-collapse: collapse; width: 100%; } th, td { text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #f2f2f2; } th { background-color: #4CAF50; color: white; } .dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; z-index: 1; } .dropdown:hover .dropdown-content { display: block; } </style> </head> <body> <h2>网易云音乐歌单</h2> <div class="dropdown"> <a href="#">分类</a> <div class="dropdown-content"> <?php foreach ($tables as $table) { ?> <a href="?table=<?php echo $table; ?>"> <?php echo getLanguageName(str_replace("_list", "", $table)); ?> </a> <?php } ?> </div> </div> <?php if (!empty($tableName)) { ?> <table> <tbody> <tr> <?php $columns = mysqli_query($conn, "SHOW COLUMNS FROM $tableName"); if ($columns->num_rows > 0) { while ($column = mysqli_fetch_array($columns)) { ?> <th><?php echo $column["Field"]; ?></th> <?php } } ?> </tr> <?php foreach ($data as $row) { ?> <tr> <?php foreach ($row as $value) { ?> <td><?php echo $value; ?></td> <?php } ?> </tr> <?php } ?> </tbody> </table> <?php } ?> <?php // 关闭连接 $conn->close(); ?> </body> </html>修改代码,给分类菜单和其的选项优化成按钮样式
06-06
<?php // 连接数据库 $conn = new mysqli("localhost", "root", "123456", "wyya"); // 检查连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 查询五个歌单的表 $tables = array(); $result = mysqli_query($conn, "SHOW TABLES LIKE '%_list'"); if ($result->num_rows > 0) { while ($row = mysqli_fetch_array($result)) { $tables[] = $row[0]; } } // 将歌单包含的语种代码替换成对应的语种名称 function getLanguageName($languageCode) { switch($languageCode) { case "one": return "华语"; case "two": return "欧美"; case "three": return "日语"; case "four": return "韩语"; case "five": return "粤语"; default: return ""; } } // 获取选的歌单表 $tableName = isset($_GET["table"]) ? $_GET["table"] : ""; $data = array(); if (!empty($tableName)) { $result = mysqli_query($conn, "SELECT * FROM $tableName"); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $row["language"] = getLanguageName($row["language"]); $data[] = $row; } } } ?> <!DOCTYPE html> <html> <head> <title>网易云音乐歌单</title> <style> table { border-collapse: collapse; width: 100%; } th, td { text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #f2f2f2; } th { background-color: #4CAF50; color: white; } .dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; z-index: 1; } .dropdown:hover .dropdown-content { display: block; } </style> </head> <body> <h2>网易云音乐歌单</h2> <div> <h3>分类</h3> <?php foreach ($tables as $table) { ?> <form method="get" action=""> <input type="hidden" name="table" value="<?php echo $table; ?>"> <button type="submit"> <?php echo getLanguageName(str_replace("_list", "", $table)); ?> </button> </form> <?php } ?> </div> <?php if (!empty($tableName)) { ?> <table> <tbody> <tr> <?php $columns = mysqli_query($conn, "SHOW COLUMNS FROM $tableName"); if ($columns->num_rows > 0) { while ($column = mysqli_fetch_array($columns)) { ?> <th><?php echo $column["Field"]; ?></th> <?php } } ?> </tr> <?php foreach ($data as $row) { ?> <tr> <?php foreach ($row as $value) { ?> <td><?php echo $value; ?></td> <?php } ?> </tr> <?php } ?> </tbody> </table> <?php } ?> <?php // 关闭连接 $conn->close(); ?> </body> </html>修改代码,将分类的五个按钮横向排列,每个按钮间有一定间隔
06-06
<?php // 连接数据库 $conn = new mysqli("localhost", "root", "123456", "wyya"); // 检查连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 查询五个歌单的表 $tables = array(); $result = mysqli_query($conn, "SHOW TABLES LIKE '%_list'"); if ($result->num_rows > 0) { while ($row = mysqli_fetch_array($result)) { $tables[] = $row[0]; } } // 将歌单包含的语种代码替换成对应的语种名称 function getLanguageName($languageCode) { switch($languageCode) { case "one": return "华语"; case "two": return "欧美"; case "three": return "日语"; case "four": return "韩语"; case "five": return "粤语"; default: return ""; } } // 获取选的歌单表 $tableName = isset($_GET["table"]) ? $_GET["table"] : ""; $data = array(); if (!empty($tableName)) { $result = mysqli_query($conn, "SELECT * FROM $tableName"); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $row["language"] = getLanguageName($row["language"]); $data[] = $row; } } } ?> <!DOCTYPE html> <html> <head> <title>网易云音乐歌单</title> <style> table { border-collapse: collapse; width: 100%; } th, td { text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #f2f2f2; } th { background-color: #4CAF50; color: white; } .dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; z-index: 1; } .dropdown:hover .dropdown-content { display: block; } </style> </head> <body> <h2>网易云音乐歌单</h2> <div> <div style="display:flex; justify-content:space-between;"> <?php foreach ($tables as $table) { ?> <form method="get" action=""> <input type="hidden" name="table" value="<?php echo $table; ?>"> <button type="submit" style="margin-right:10px;"> <?php echo getLanguageName(str_replace("_list", "", $table)); ?> </button> </form> <?php } ?> </div> </div> <?php if (!empty($tableName)) { ?> <table> <tbody> <tr> <?php $columns = mysqli_query($conn, "SHOW COLUMNS FROM $tableName"); if ($columns->num_rows > 0) { while ($column = mysqli_fetch_array($columns)) { ?> <th><?php echo $column["Field"]; ?></th> <?php } } ?> </tr> <?php foreach ($data as $row) { ?> <tr> <?php foreach ($row as $value) { ?> <td><?php echo $value; ?></td> <?php } ?> </tr> <?php } ?> </tbody> </table> <?php } ?> <?php // 关闭连接 $conn->close(); ?> </body> </html>修改代码,使五个按钮靠在一起,并适当增大每个按钮,将华语按钮填充为红色,将欧美按钮填充为蓝色,将日语按钮填充为黄色,将韩语按钮填充为粉色,将粤语按钮填充为紫色
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lxw1844912514

你的打赏就是对我最大的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值