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?

2
  • 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 Commented Aug 27, 2013 at 12:08
  • 1
    PHP variable scope between code blocks Commented Aug 27, 2013 at 12:09

1 Answer 1

3

use global keword: global $variable_name;

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.