bugsie101 Posted February 16, 2009 Share Posted February 16, 2009 Hello, I am new to php so I hope you will forgive me if what I ask seems obvious to you. Here is a cut down version of what is causing me problems… $team1n = "Plymouth"; $team2n = "Blackpool"; $team3n = "Norwich"; function match($team_no) { global $team1n, $team2n, $team3n; $hometeam = '$team'."$team_no".'n'; print $hometeam; }; match(1); prints '$team1n' but I want it to print 'Plymouth' which is the value held in $team1n. This is only part of the function I have a number of variables to go in the function that would benefit from me being able to identify them this way. Any help greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/145423-solved-how-can-i-display-variable-value/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 16, 2009 Share Posted February 16, 2009 When you have a set of data, use an array and don't use global, always pass data into a function as a parameter - <?php function match($teams,$team_no) { $hometeam = $teams[$team_no]; print $hometeam; } $teams = array(); $teams[1] = "Plymouth"; $teams[2] = "Blackpool"; $teams[3] = "Norwich"; match($teams,1); ?> Link to comment https://forums.phpfreaks.com/topic/145423-solved-how-can-i-display-variable-value/#findComment-763428 Share on other sites More sharing options...
Brian W Posted February 16, 2009 Share Posted February 16, 2009 btw, I know your new so you are forgiven the first time... but please use [ code ][ /code ] (without the spaces inside the tags) tags next time around your code this: [ code ]echo "this is my code";[ /code ] without the spaces in the tags will be: echo "this is my code"; This makes reading the code much easier and many of the members of the forum practically refuse to even read code that isn't in the code tags. Link to comment https://forums.phpfreaks.com/topic/145423-solved-how-can-i-display-variable-value/#findComment-763434 Share on other sites More sharing options...
bugsie101 Posted February 16, 2009 Author Share Posted February 16, 2009 Thanks a lot PFMaBiSmAd, really appreciate you pointing me in the right direction. Brian W... I consider myself told, I will use in the future. Link to comment https://forums.phpfreaks.com/topic/145423-solved-how-can-i-display-variable-value/#findComment-763549 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.