1

I have an html page with few <?php ?> code snippets in it. In the first snippet I am connecting to a database and fetching data. Now I want to display that data later in some div tags on my HTML page.

So, is the following the easiest way to do it? When I fetch the data from my database, I store them in an array which I define globally - I mean to be visible in other PHP code snippets on page, and then in those div tags I just print whatever elements of that array I want, like this: <div class="info"> <?php print "{$info[2]} ?> </div>. Is this the correct and safe way to do this and how it's done, I mean how should I define this global array $info?

halfer
  • 19,824
  • 17
  • 99
  • 186
Vladimir
  • 1,243
  • 5
  • 19
  • 23
  • 1
    That's the way it is generally done when using PHP as a templating language in the context of a single HTML page, yeah. You could make it an associative array so you know further down in the code which element is which – Pekka Aug 27 '13 at 12:08
  • 1
    [PHP variable scope between code blocks](http://stackoverflow.com/a/5126417/285587) – Your Common Sense Aug 27 '13 at 12:09

1 Answers1

3

use global keword: global $variable_name;

ram
  • 2,275
  • 3
  • 27
  • 38