What's the meaning of this syntax ?> <?php - 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>
";
}
?>

?> is the end tag of PHP much like </td> is an end tag of a table cell.

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 "";
}
?>

Related

Hyperlinked and creating a HTML Page that is nested in a table

So I have a html table that is automatically generated after passing a query to my database. I want to create a hyperlink within my html table to a page that will pull more detailed information from a Second Table.
I was thinking of using the Tablecell creator that pulls from the First Table, and modifying so that it would encompass the table's contents with hyperlink tags. I was thinking it would look like this.
foreach(new TableRow(new AutoArrayMaker($stmt->fetchAll()) as $rowend => $row){
echo <a href = "the reusable HTML Page">;
echo $row;
echo </a>;
}
Is my idea sound from a coding standpoint?
Firstly, echo's need to be in quotation marks " So that code wouldn't fire.
There are a few ways you can output HTML. The first is using echo's:
echo "Google";
Notice how I put a back-slash before hand? This is what is known as an escape. This puts the character after into a letter depending on what it escapes to. See php docs: http://php.net/manual/en/regexp.reference.escape.php (as my description of it was poor)
The other option would be to run out of php then join back on so to speak:
<?PHP
foreach(new TableRow(new AutoArrayMaker($stmt->fetchAll()) as $rowend => $row){
?>
<a href="abc">
<?PHP echo $row; ?>
</a>
<?PHP
}
However, this is not advised.
Edit:
Also, you can make your own table very simply:
<table>
<?PHP
foreach($stmt as $row){
?>
<tr>
<td>
<a href="abc"><?PHP echo $row[id]; ?>
</td>
</tr>
<?PHP
}
?>
</table>
See https://www.w3schools.com/html/html_tables.asp for more info.

How to echo javascript code which contains php tags inside?

I usually echo script alert by simply
echo '<script type="text/javascript">alert("'.$my_message.'");</script>';
but how to echo the script when the script contains php tags? (see my example code)
Because of my php obfuscator script, I have to use only one <?php ?> tag. so in this case I need to echo the javascript without having <?php tags. What are the possible solutions in this case?
<?php
...some php code...
<script type="text/javascript">
jQuery(document).ready(function($){
$i = 0;
$('.wrapper_vertical_menu .megamenu_menu').append('<div class="more-wrap"><span class="more"><?php echo $this->__("More"); ?></span></div>');
$('.wrapper_vertical_menu .megamenu_menu > li.megamenu_lv1').each(function(){
$i ++;
if($i>13){
$(this).css('display', 'none');
}
});
... more js code ...
JavaScript doesn't "contain PHP tags". All your PHP code needs to do is build the resulting output string (which happens to be JavaScript code, but that doesn't matter to PHP) and echo it.
So where you have something like this:
echo "some javascript code <?php echo some_php_value; ?> more javascript code";
What you really want, quite simply, is this:
echo "some javascript code " . some_php_value . " more javascript code";
Don't try to nest <?php ?> tags within each other. Just concatenate the actual output you want and echo that output.

Output some PHP code into a HTML <code> tag

How can I use the HTML <code> element to output a block of PHP code, without the page running that PHP code? Eg;
<pre><code>
<?php
// Some super duper PHP code
?>
</code></pre>
I'm creating an API docs page, which features snippets of PHP that anyone wishing to use the API can use as examples, but anything wrapped in <?php> tags runs as an actual PHP function
Use <?php and ?>.
The HTML entities will show up as PHP opening and closing tags when the page is rendered, but PHP will obviously not see them. But you have to html-escape your code anyways, otherwise contained HTML-tags will be rendered. So there should be
<?php echo 'Hello, World.<br>'; ?>
Another way would be to have a string specified by a nowdoc and then output html-escaped (demo):
<?php
$code = <<<'EOC'
<?php
echo 'Hello, World.<br>';
// ...your code here...
?>
EOC;
echo htmlentities($code);
?>
Have look for different approaches at How do I display PHP code in HTML?.
Do this via PHP like so:
<?php
$code = '<?php
echo "Hello, World!";
?>';
echo '<code>' . htmlspecialchars($code) . '</code>';
?>
try something like this:
<?php echo '<?php'; ?>
This may help you.........
######################################################################
echo "<h2><br>Source Code of ".basename((string)__FILE__) . "</h2><hr>";
show_source(__FILE__);
echo "<hr>";
echo "<h2>Output of ".basename((string)__FILE__) . "<hr></h2>";
#######################################################################
I had to convert the less-than and greater-than to their HTML name.
<pre><code><?php echo
"<!--
This is the church title to be used in the heading of the web-pages.
Author: John Fischer III of Written For Christ in 2018
Updated:
-->
<?php echo 'Our Little Church:'; ?>" ?>
</code></pre>

Echoing PHP TAG showing blank output HTML

I am testing this on windows 7 xammp 1.8.1 php 5.4.7
I am trying to show dynamic php codes in html as example
my code is
<?php
$output="<?php echo $ti ?>";
echo $output;
?>
but output html is blank! i am not sure if its a bug, can some help me.thanks in advance
When you run this, this DOES produce an output, which is a blank page, because the output is:
<?php echo ?>
To a browser which renders html, it will look like an open tag with nothing in value.
Run your script and view the page source...
You need to use single quotes
i.e.
change
<?php
$output="<?php echo $ti ?>";
echo $output;
?>
to
<?php
$output='<?php echo $ti ?>';
echo $output;
?>
Just change your double quotes to single quotes:
<?php
$output='<?php echo $ti ?>';
echo $output;
?>
Example: http://ideone.com/bUJAxb
There are two things you have to change. First, if you use double quotes PHP will evaluate variables in it, so your output will be <?php echo ?>:
$output='<?php echo $ti ?>';
Now the output will be <?php echo $ti ?>.
Next, the browser will interpret this as HTML and since it is only a single tag it will display nothing. You need to run this through htmlentities():
echo htmlentities($output);
This will output <? echo $ti ?;gt; which will be displayed by the browser in the way you intend it.
You need to escape the characters
This can be done by adding the entire line you want to output in an htmlentities function call, like:
$output = htmlentities("<?php echo \$ti ?>");

sql, php, html table question

I want to grab data from a mysql database by using php. The data looks something like this:
apple 3
orange 2
banana 4
I want to take the data and put it in a html table and use css to make it look pretty, but I dont want to deal with it inside <?php ?>
After I grab the
$result = mysql_query("SELECT * FROM Table");
can I reference the result variable outside the <? php ?> tags?
No. PHP can only be done in <?php ... ?> or <?= ... ?>. Use a template engine such as Smarty if you want substitution in this manner.
in short, no you cant, it is a php variable (technically a resource in this case) so you have to parse it through the php engine, which requires the php tags
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr><td>'.$row['fruit'].'</td><td>'.$row['id'].'</td></tr>';
}
echo '</table>';
Short answer is no. HTML cannot deal with dynamic content.
If you want to cut down the amount of echo statements within your code you can store the html within a given variable and then make reference to it.
I find it better to do the following:
<table>
<?php foreach($result as $row): ?>
<tr>
<td><?php echo $row['fruit']?></td>
<td><?php echo $row['id']?></td>
</tr>
<?php endforeach; ?>
</table>
This provides clarity and minimizes concatenation.

Categories