Jump to content

simple array retrieval


lit108

Recommended Posts

I am just starting php and attempting a simple program that will retrieve some data from an array

 

I have 2 files one with the function containing the array, another with the code that should select a value from the array. I have done what I thought would work however it does nothing. Any help would be greatly appreciated

 

function.php

<?PHP
function getMonthName(){
    $a=array("one","two","three","four","five");
    print_r($a);
}
?>

 

retrieve.php

<?PHP
require_once('functions.php');
$number=3;

if ($number == $a) {
    echo "$a";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/214941-simple-array-retrieval/
Share on other sites

First, the function isn't returning anything. Second, you're not invoking the function.

 

Here's what I think you want to do:

functions.php

<?php
function getMonthName($n) {
   $a=array("one","two","three","four","five");
   return($a[$n]);
}

 

main code

<?php
require_once('functions.php');
$number = 3;
echo getMonthName($number);
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.