5 Replies - 1018 Views - Last Post: 03 March 2014 - 07:46 PM Rate Topic: -----

#1 toasterburn   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 45
  • Joined: 08-March 10

PHP Variable in HTML Tag

Posted 03 March 2014 - 12:50 PM

I am trying to create a bar graph. Specifically, I want to use php to grab certain values, and then use those values to populate a graph I've written using html and jquery. The problem I'm having is I can't figure out how to embed a php variable into an HTML tag.

What I'm trying to do:
<?php
$doc1 = "50";
?>

.....

<td>
<?php 
echo $doc1; 
?>
</td>




Is this possible?

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Variable in HTML Tag

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2730
  • View blog
  • Posts: 10,898
  • Joined: 03-December 12

Re: PHP Variable in HTML Tag

Posted 03 March 2014 - 01:05 PM

Have you tried it?
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3810
  • View blog
  • Posts: 13,836
  • Joined: 08-August 08

Re: PHP Variable in HTML Tag

Posted 03 March 2014 - 01:07 PM

View Posttoasterburn, on 03 March 2014 - 02:50 PM, said:

The problem I'm having is I can't figure out how to embed a php variable into an HTML tag.

Is this possible?

No, it is not.

PHP runs on the server after a request from the browser (client's computer) and before the browser renders what PHP has done. Two different computers at two different times. You'd be better off if you separated your languages to reflect reality.

This post has been edited by CTphpnwb: 03 March 2014 - 01:08 PM

Was This Post Helpful? 0
  • +
  • -

#4 toasterburn   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 45
  • Joined: 08-March 10

Re: PHP Variable in HTML Tag

Posted 03 March 2014 - 02:09 PM

Ah, I see. So I should create a separate php file that grabs the html, replaces the values, and then spits the edited version back out. Something like:


HTML File:
....
<td>doc1</td>
....


PHP File:
<?php
$html = file_get_contents('chart.html');
$editedhtml = str_replace("<td>doc1</td>", "<td>300</td>", $html);
echo $editedhtml;
?>


I ran a quick test of this and it seems to work.
Was This Post Helpful? 1
  • +
  • -

#5 Ambitious   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Re: PHP Variable in HTML Tag

Posted 03 March 2014 - 06:54 PM

That wouldn't be possible because you've ended the PHP code with ?> already and the variable has been destroyed.
A smarter and easier way to do this is to actually put the HTML code inside of PHP code.
<?php
$doc="50";
echo "<td>".$doc."</td>";
?>


Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3810
  • View blog
  • Posts: 13,836
  • Joined: 08-August 08

Re: PHP Variable in HTML Tag

Posted 03 March 2014 - 07:46 PM

Breaking out of PHP does not destroy the variable. It merely tells the PHP interpreter to pass what follows directly to the browser, ignoring it completely until either the end of file is reached or another <?php tag is found, at which point the variable can be accessed as before.

The problem with breaking out of PHP is not the interpreter. It's the human. We can't keep track of many breaks, and we tend to get confused about where things are happening (server or client?) and when. There is a good reason we have .html, .js, .css, and .php files: Those languages don't belong together in the same file! You can put them together, and sometimes that is convenient, but it comes at the cost of readability. That's a huge cost, even more so if you're not an expert.

This post has been edited by CTphpnwb: 03 March 2014 - 07:46 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1