Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6930591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:28:01+00:00 2026-05-27T11:28:01+00:00

I have a website. On this website, I am trying to generate random images.

  • 0

I have a website. On this website, I am trying to generate random images. The code for this looked messy when I had it mixed in with the rest of the HTML on the main page, so I decided to use an ‘include’ call to call another page with the php functions I wanted on it.
The problem with calling functions to run the specific blocks of code is, that when it runs, it doesn’t recognise that I have tried to put some global variables at the top, therefore when it outputs a number, there isn’t one because it never had one.

Can anybody tell me how I can fix this, so that I can use the variables so that they have the initial values I would like?

<?php
$total1 = "4";
$start = "1";
$random1 = mt_rand($start, $total1);
$total2 = "3";
$random2 = mt_rand($start, $total2);

function randomImage1()
{
  $file_type = ".jpg";
  $image_folder = "Images/Picturebar";
  $image_name = $random1 . $file_type;
  echo ("<img src=\"$image_folder/$image_name\" alt=\"$image_name\" width=\"204px\"     height=\"100px\" />");
  echo("$random1");
}

function randomImage2()
{
  $file_type = ".jpg";
  $image_folder = "Images/Picturebar";
  if ($random2 == $random1) {
      $random2 = $random2 + 1;
  } 
  $image_name = $random2 . $file_type;
  echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" width=\"204px\"         height=\"100px\" />"; 

  echo("$random2");
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:28:02+00:00Added an answer on May 27, 2026 at 11:28 am

    You need to reference the variables globally inside the functions, or better yet, pass them as parameters:

    // pass $random1 as a parameter
    function randomImage1($random1)
    {
      $file_type = ".jpg";
      $image_folder = "Images/Picturebar";
      $image_name = $random1 . $file_type;
      echo ("<img src=\"$image_folder/$image_name\" alt=\"$image_name\" width=\"204px\"     height=\"100px\" />");
      echo("$random1");
    }
    
    // Pass $random2 as a parameter
    function randomImage2($random2)
    {
      $file_type = ".jpg";
      $image_folder = "Images/Picturebar";
      if ($random2 == $random1) {
          $random2 = $random2 + 1;
      } 
      $image_name = $random2 . $file_type;
      echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" width=\"204px\"         height=\"100px\" />"; 
    
      echo("$random2");
    }
    

    This could also be done with the global keyword inside the function, but passing parameters as above is preferred:

    function randomImage1($random1)
    {
      // Identify $random1 as the existing global
      global $random1;
      // Or use $GLOBALS['random1']
    
      $file_type = ".jpg";
      $image_folder = "Images/Picturebar";
      $image_name = $random1 . $file_type;
      echo ("<img src=\"$image_folder/$image_name\" alt=\"$image_name\" width=\"204px\"     height=\"100px\" />");
      echo("$random1");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to days now to get this website completed ... however,
I'm trying to generate QR codes on my website. All they have to do
I have this website iloveforwards.com created using drupal. I would like to have a
I have this website I am designing and I want to make it to
So I have this website that I'm building and they are wanting to be
I have a website (ASP.NET MVC3) that runs on IIS 7. From this website,
I'm building this website and I have basically two questions about this page: http://sites.publishyours.com.br/silviamecozzi/pt/obra/deserto_das_palmas.php
I have an website where people can place a vote like this: http://mysite.com/vote/25 This
I have sessions that for the website and this is how i use them:
I have 1 website on IIS (myWebsite) and another inside this one (secondWebsite) as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.