-1

I have wasted several hours just to make a textbook example of defining the php variables and trying to echo the values using my both XAMPP Sever and the actual hosting company. My goal is to display the results of a sql query into a html table. Unfortunately, I cannot even echo the 4 basic php variables with actual values?

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test HTML Table With mysql variables</title>
</head>
<body>
<?php

$email1 = "av104";
$email2 = "av1040";
$address1 = "Thousand Oaks";
$address2 = "Los Angeles";
echo $email1."<br/>";

echo $email2."<br/>";

echo $address1."<br/>";

echo $address2.."<br/>";
<?
<table>
<tr>
<td>$email1</td>
<td>$address1</td></tr>
<tr>
<td>$email2</td>
<td>$address2</td>
<tr>
</table>
*/
</body>
</html>

When I execute this script using either XAMPP or my hosting company's server, I get this output:

"; echo $email2."
"; echo $address1."
"; echo $address2.."
"; $email1 $address1 $email2 $address2

I will greatly appreciate if someone can guide me what is wrong with this html/php script? I will be much obliged. Thanks!

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
av104
  • 1

3 Answers3

2

just use below code, there are multiple typos in your code:

php:

<?php

  $email1   = "av104";
  $email2   = "av1040";
  $address1 = "Thousand Oaks";
  $address2 = "Los Angeles";

  echo $email1."<br/>";
  echo $email2."<br/>";
  echo $address1."<br/>";
  echo $address2."<br/>";
?>

html:

<table>
  <tr>
   <td><?php echo $email1; ?></td>
   <td><?php echo $address1; ?></td>
  </tr>
  <tr>
   <td><?php echo $email2; ?></td>
   <td><?php echo $address2; ?></td>
  </tr> 
</table>

Vural
  • 8,666
  • 11
  • 40
  • 57
  • the HTML is wrong. That will literally output exactly what you have. You need to echo the PHP variables... – Native Coder Oct 07 '16 at 20:33
  • I made all the changes that were suggested. Here is the revised code but output has still not changed? What did I do wrong? Thanks! "; echo $address1."
    "; ?>
    echo echo
    – av104 Oct 07 '16 at 21:51
  • You changed the both PHP and HTML codes? Because you have in both codes typos and bugs. – Vural Oct 07 '16 at 21:54
  • Viral, I did change both php and HTML as suggested by others. It still does not echo php variables? Thanks – av104 Oct 08 '16 at 02:47
0

It's because your closing PHP tag is backwards. php should end with ?>. Also note, that ANYTHING that's not between the php tags will be echod out literally, as is. so these

<td>$email1</td>
<td>$address1</td></tr>

should look like this

<td><?php echo $email1; ?></td>
<td><?php echo $address1; ?></td></tr>

and your code will work

Native Coder
  • 1,792
  • 3
  • 16
  • 34
0

There are two different errors in your code:

First, the PHP closing tag is ?> you wrote the opposite!

Second, you mixed the HTML and the PHP. This is NOT okay:

<td>$address1</td>

If you want to inject a PHP into HTML tags, you have to use the PHP opening and closing tags like this:

<td><?php $address1 ?></td>

This way, you will reach the PHP variable inside the HTML tags.