简体   繁体   中英

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. ?> closes that sequence and says that I am not longer using php code.

In your case the php opens up and starts a for loop. Inside of the for loop a table is made but it is done using html not php. Then in each table piece, php is being used to echo (write something to the screen) some content into the table. Then finally at the end the php for loop must be finished with a closed bracket. I hope that makes sense.

The meaning of that syntax, is essentially telling the server to stop processing PHP. What follows in the page, is HTML code. That is <?php tells the server process this as PHP script and the ?> says stop processing PHP script .

Normally, you'll not see HTML outputted in this manner, but instead using PHP's echo to write the 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. 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. If text is not between php tags, it will be output rather than executed.

<?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.

If you don't enclose your PHP code between <?php and ?> , it won't be processed, and will be output as 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. Second part (l.5 to 9) is HTML code, which is include in the loop. 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. 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 ""; } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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