-4

i want to print a php variable in the html h1 tag. Important thing to know is that html is defined within the php as follow:

<?php


$test=" <html>

<h1><?php echo (rand(10,1000000)); ?></h1>

</html> ";
echo $test;

?>
3
  • send link to question it is dup of, and send link of solved question. Commented Sep 23, 2017 at 6:43
  • Just read the complete duplicate - the link is above your question. There are a few ways and you have others here too. It is REALLY basic stuff you can read on the first page of any PHP tutorial Commented Sep 23, 2017 at 6:54
  • Your edit shows a completely different construction than the first. You need to use the "." concatenation in your example without the echo Commented Sep 23, 2017 at 6:59

3 Answers 3

2

You can write either 1:

<?php 
$a="hi";
?>
<html>
  <h1><?php echo $a;?> </h1>
</html>

or 2:

<?php 
  $a="hi";
  echo "<html><h1>".$a."</h1></html>";

?>
Sign up to request clarification or add additional context in comments.

2 Comments

I have put not that html is within the php, i have updated the question please recheck
Please take more care in formatting. You had a typo too.
0

use the following

<?php
  $a="hi";
  $prints =" <html>
               <h1> $a</h1>
             </html>";
 echo"$prints";
?>

5 Comments

This will generate syntax error.
<?php $a="hi"; $prints =" <html> <h1> $a</h1> </html>"; echo"$prints"; ?> This will work for sure.I've updated my answer
@mr.tanmay Thanks with some ammendments your suggestion works
Bad formatting, no need to use quotes here echo"$prints"; etc - this is not a good solution
he got his answers, Formatting was not the correct thing he was looking for he was looking for the correct conceptual answer :)
-1

You can directly 'show' php variables by 'echoing' them in the html. For example consider this code.

<?php 
        include_once "./php/db.php";
            $result=$conn->query("SELECT * FROM medtable");
                  while($row=mysqli_fetch_array($result)){
                    echo '<form id="form-'.$row["Product"].'"type="POST">';
                    echo '<table class="table table-hover table-condensed" id="table-'.$row["Product"].'">';
                    echo '<tr>';
                    echo'<td style="width:50%" name="name-'.$row["Product"].'">'.$row["Product"].'</td>';
                    echo '<td style="width:20%"> <input type="number" name="amount" id="num-'.$row["Product"].'" min="0" value="'.$row['Amount'].'"</input></td>';
                    echo '<td style="width:10%">'.$row["Price"]. '</td>';
                    echo '<td id="subtotal-'.$row["Product"].'"style="width:10%" value="'.$row["Subtotal"].'">'.$row["Subtotal"].'</td><input type="hidden" name="hidden-'.$row['Product'].'"id="hidden-'.$row["Product"].'"value="'.$row["Subtotal"].'"</input>';
                    echo '<td style="width:10%"><input type="button"id="'.$row["Product"].'"
                        name="'.$row["Product"].'"class="btn btn-primary" value="Submit">
                         </input></td>';
                    echo '<td><input type="hidden" name="product" value="'.$row["Product"].'"></input></td>';
                    echo "</tr>";
                    echo "</table></form>";
                  }
                ?>

As you can see a query is made from a table and html tags are then echod into the html file. Hope this helps :)

5 Comments

Horrible solution
why? @mplungjan
Completely unreadable. Use heredoc or echo or <?= ... ?> - anything is better than echoing html like you do
@mplungjan maybe be more clear when initially commenting as to why something is 'horrible'. This was also very long ago when I wrote this program.
Yeah - apologies - just the reams of unreadable code made me shiver

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.