Skip to content
Advertisement

Randomly Show an HTML Snippet From List With PHP

As the title implies, I have a few different lines of HTML code (Example Below), and I would like the server to randomly select one and show it when the page loads.

I have these three HTML snippets, and I want one to randomly show when the page is loaded.

JavaScript
JavaScript
JavaScript

I found this example using the PHP array function, but I don’t know how to put the array in a separate PHP file, or if I can change the image links to full HTML code.

The example I found:

JavaScript

Then, you just get a random element from the array

JavaScript

Then you can go right away displaying the image:

JavaScript

How can I make this so the HTML snippets above are stored in the array, and the array is in a different PHP file (I can use the “require” function for that, right?).

Advertisement

Answer

You can echo any amount of html code just like you would echo the value of a property.

You’re right that you can use require to access an array defined in another file.

JavaScript

images_array.php:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement