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

  • Home
  • SEARCH
  • 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 1005903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:22:16+00:00 2026-05-16T08:22:16+00:00

again. Right now I’m having issues with some pretty basic functionality in PHP. I

  • 0

again. Right now I’m having issues with some pretty basic functionality in PHP. I have a web page which includes the following code:

  function laserOn()
  {
    $_SESSION['laser'] = TRUE;

    $num_victims = rand(2,125);
    $vic = rand(0, count($victims) - 1);

    print $num_victims." ".$victims[$vic]." have been vaporized!<br />";
  }

and a separate file, victims.php which i have require_once‘d, that contains

$victims = array(
    1 => "chickens",
    2 => "horses",
    3 => "werewolves",
    4 => "zombies",
    5 => "vampires",
    6 => "cows"
);

The page, though, only displays the number and the string, not the array value. WHat am I doing wrong here?

  • 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-16T08:22:17+00:00Added an answer on May 16, 2026 at 8:22 am

    $victims does not appear to be within the scope of your function. In order to use a global variable within function scope, you need to declare it using the global keyword (see variable scope in the php.net docs).

    function laserOn()
    {
      global $victims;
    
      // ... rest of your function
    }
    

    Alternately, you could require the file that contains the array within the function, but this might not be desired (especially if the file defines functions of its own!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I have a JSP page that allows to sort some items, when
Right now, I have code which creates the database (just a few CREATE queries
I'm having trouble right now with the creation of a Master-Detail page in an
I'm debugging some open source email client in android right now but I have
Well, I'm having this newbie problem right now. I have 3 JFrames. JFrame1 opens
Right now, I have a simple web application that displays the entries of a
I am using php to build a change classifieds page right now. I use
I'm back. Again. :3 Right now, I'm working on my RPG project (just for
right now i have this <script type='text/javascript'> $(#beau).click(function(){ $(#beau).animate({margin-Top: 738px}, fast); }); </script> and
I'm taking a Computer Architecture course right now, and we're going over basic R-type

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.