lit108 Posted October 1, 2010 Share Posted October 1, 2010 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 More sharing options...
nikolajpop Posted October 1, 2010 Share Posted October 1, 2010 You are using variable $number as a whole number, try assigning $number variable as array, like $number[3]; and then in retrieve.php assign to $number[2] = "three"; and then compare arrays with IF. Link to comment https://forums.phpfreaks.com/topic/214941-simple-array-retrieval/#findComment-1118127 Share on other sites More sharing options...
kenrbnsn Posted October 1, 2010 Share Posted October 1, 2010 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 Link to comment https://forums.phpfreaks.com/topic/214941-simple-array-retrieval/#findComment-1118156 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.