简体   繁体   English

这种语法的含义是什么?

[英]What's the meaning of this syntax ?> <?php

<?php
for($i=0;$i<=100;$i++)
{
?> // why this
 <tr>
  <td> <?php echo $i; ?></td>
  <td><?php echo "tên sách $i"; ?></td>
  <td><?php echo "noi dung sach $i";?></td>
 </tr>
<?php
}
?>

So that is the scenario I'm looking to understand. 这就是我想要了解的场景。 Thanks 谢谢

The <?php opens a php script sequence so it is saying that inside this is php code. <?php将打开一个php脚本序列,因此可以说其中是php代码。 ?> closes that sequence and says that I am not longer using php code. ?>关闭该序列,并说我不再使用php代码。

In your case the php opens up and starts a for loop. 在您的情况下,php打开并开始for循环。 Inside of the for loop a table is made but it is done using html not php. 在for循环中,创建了一个表,但它是使用html而不是php来完成的。 Then in each table piece, php is being used to echo (write something to the screen) some content into the table. 然后在每个表中,使用php将某些内容回显(在屏幕上写一些东西)到表中。 Then finally at the end the php for loop must be finished with a closed bracket. 然后最后在末尾,php for循环必须以一个封闭的括号结束。 I hope that makes sense. 我希望这是有道理的。

The meaning of that syntax, is essentially telling the server to stop processing PHP. 该语法的含义实质上是告诉服务器停止处理PHP。 What follows in the page, is HTML code. 页面中紧随其后的是HTML代码。 That is <?php tells the server process this as PHP script and the ?> says stop processing PHP script . <?php告诉服务器将其作为PHP脚本处理 ,而?>指示停止处理PHP脚本

Normally, you'll not see HTML outputted in this manner, but instead using PHP's echo to write the HTML 通常,您不会看到以这种方式输出的HTML,而是使用PHP的echo来编写HTML

<?php
 for($i=0;$i<=100;$i++)
 {
 echo "
 <tr>
  <td>{$i}</td>
  <td>\"tên sách {$i}\"</td>
  <td>\"noi dung sach {$i}\"</td>
 </tr>
 ";
}
?>

?>是PHP的结束标记,就像</td>是表单元格的结束标记一样。

The PHP parser lets you enable and disable parsing by including the PHP start and end tags <?php and ?> in your document. PHP解析器允许您通过在文档中包含PHP开始和结束标记<?php?>来启用和禁用解析。 These two samples have the same output: 这两个样本具有相同的输出:

One
-------------------
echo '<td>' . $foo . '</td>';

Two
-------------------
?>
<td><?php echo $foo; ?></td>
<?php

As far as which is easier to read, well, that's a matter of preference I suppose. 至于哪一个更容易阅读,我想这是一个偏好问题。

See also: the shortcut tag <?= for echoing a value. 另请参见: 快捷方式标签 <?=用于回显值。

You can come in and out of php execution any time you like. 您可以根据需要随时进出PHP执行。 If text is not between php tags, it will be output rather than executed. 如果文本不在php标签之间,则将输出而不是执行文本。

<?php // start executing
if($test){ ?> <!-- start a php if statement -->
<p>This will only be output if the php test is true</p>
<?php } ?> <!-- don't forget to close the if statement -->
<p>This will always be output</p>

PHP allows you to min PHP code and HTML markup in the same file, so it needs a way to tell them apart. PHP允许您最小化同一文件中的PHP代码和HTML标记,因此需要一种区分它们的方法。

If you don't enclose your PHP code between <?php and ?> , it won't be processed, and will be output as HTML. 如果您不将PHP代码放在<?php?> ,则将不会对其进行处理,并将其输出为HTML。

You should read a minimum of documentation before start a project (and ask basic questions). 在开始一个项目之前,您应该阅读最少的文档(并提出基本问题)。

First part (l.1 to 4) is PHP code, here you start a for loop. 第一部分(l.1至4)是PHP代码,在这里启动for循环。 Second part (l.5 to 9) is HTML code, which is include in the loop. 第二部分(l.5至9)是HTML代码,该代码包含在循环中。 Third and last part is here to close the loop. 第三部分也是最后一部分在这里关闭了循环。

You can do the same with : 您可以使用:

<?php
   for($i=0;$i<=100;$i++)
   {
      echo "<tr>";
      echo "<td>".$i."</td>";
      echo "<td>tên sách".$i."</td>";
      echo "<td>noi dung sach".$i."</td>";
      echo "</tr>";
   }
?>

This is a way to embed the HTML code without having to use the php print or echo function. 这是一种嵌入HTML代码的方法,而不必使用php print或echo函数。 using ?> 使用?>

Another way to get at the same result would be this: "; echo " $i "; echo " tên sách $i "; echo " noi dung sach $i "; echo ""; } ?> 获得相同结果的另一种方式是:“; echo” $ i“; echo”tênsách$ i“; echo” noi dung sach $ i“; echo”“;}?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM