-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;

?>
Machine Guy
  • 1
  • 1
  • 7
  • send link to question it is dup of, and send link of solved question. – Machine Guy Sep 23 '17 at 06: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 – mplungjan Sep 23 '17 at 06:54
  • Your edit shows a completely different construction than the first. You need to use the "." concatenation in your example without the echo – mplungjan Sep 23 '17 at 06:59

3 Answers3

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

?>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Ahmed khaled
  • 51
  • 1
  • 7
0

use the following

<?php
  $a="hi";
  $prints =" <html>
               <h1> $a</h1>
             </html>";
 echo"$prints";
?>
Codecraker
  • 327
  • 4
  • 19
-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 :)

vi_ral
  • 369
  • 4
  • 19