Jump to content

[SOLVED] How can I display variable value?


bugsie101

Recommended Posts

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

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);
?>

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.

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.