I have this php function that I created that is basically a switch statement. For each case, the $team_image variable is saved to a different value. It looks a little something like this:
function teamImage($team)
{
switch($team)
{
case "Baltimore Orioles":
$team_image = "orioles";
case "New York Yankees":
$team_image = "yankees";
case "Toronto Blue Jays":
$team_image = "bluejays";
You get the idea. However, when I call on the function and try to use the $team_image variable in other parts of my code, it doesn't work because apparently, the variable is still undefined. any thoughts?
Thanks,
Lance
As you're only setting the $team_image
inside the teamImage
function, it will only exist with that function's "scope". (In general, variables, etc. will always exist in as narrow a scope as possible, which is good in terms of encapsulation. (Encapsulation being a key benefit of object orientated programming, etc. which you may go on to discover as you learn more.)
As such, you should return the $team_image
value from the teamImage
function and set it as follows:
function teamImage($team) {
$team_image = NULL;
switch($team) {
...
}
return $team_image;
}
$team_image = teamImage($team);
An alternative would be to define the $team_image
variable within the teamImage
function as a global by adding the line global $team_image;
at the beginning of the function, but this isn't considered good practice.
Additionally, you should break;
each case code block within your switch statement as otherwise you'll simply end up setting $team_image
with the value assigned in your final case. (i.e.: If you don't break each statement, code flow will continue into the next.) See the switch PHP manual page for full details.
That's because the $team_image variable is scoped to the function. Either declare $team_image as global at the beginning of the function:
function teamImage($team)
{
global $team_image;
...
or better, return $team_image at the end of your function and assign it to another variable where you need it:
function teamImage($team) {
...
return $team_image
}
...
$image = teamImage($team_name);
you need to see the variables scope documentation..variables scope
Few facts:
break;
$team_image
have local scopedefault
?Answer:
You have to use return
in your function, if you haven't used already, otherwise the problem can be in $team_image
scope.
Example:
Things changed:
$team_image
scopeCode:
function teamImage($team)
{
$team_image = '';
switch($team)
{
case "Baltimore Orioles":
$team_image = "orioles";
break;
case "New York Yankees":
$team_image = "yankees";
break;
case "Toronto Blue Jays":
$team_image = "bluejays";
break;
}
return $team_image;
}
Usage:
$team = 'new York Yankees';
$teamImage = teamImage($team); // yankees
This problem is one of variable scope. PHP functions have their own symbol table, and when you assign to the variable $team_image
in your function, you're really assigning to a local variable. That variable goes 'out of scope' at the end of the function, meaning it doesn't exist any more.
The best way to fix this would probably be to return the value from the function and assign to the $team_image
variable using the function call.
function teamImage($team)
{
switch($team)
{
case "Baltimore Orioles":
return "orioles";
case "New York Yankees":
return "yankees";
case "Toronto Blue Jays":
return "bluejays";
}
}
$team_image = teamImage($team);
Now the variable $team_image
is in the scope in which you call the function.
If you want the variable to be visible in all scopes, you can use $GLOBALS['team_image']
instead, but global variables are widely considered bad practice. (You can find many sources online that will explain why.)
However, when I call on the function and try to use the $team_image variable in other parts of my code
you need to return your $team_image
at the end of the function
so it looks like this
function getTeamImage($team)
{
switch($team)
{
case "a":
$team_image = "asdas";
break;
#end so on
}
return $team_image;
}
#And than you can use in your other code:
$team_image = getTeamImage($team);
First off, you need to use break
in your switch to prevent fall-through:
<?php
function teamImage($team)
{
switch($team)
{
case "Baltimore Orioles":
$team_image = "orioles";
break;
case "New York Yankees":
$team_image = "yankees";
break;
case "Toronto Blue Jays":
$team_image = "bluejays";
break;
default:
$team_image = "none";
}
return $team_image;
}
echo teamImage('Baltimore Orioles');
?>
Second, if you want to use a variable that is modified in a global scope, you need to use the global
keyword within the function, or use the $GLOBALS
array:
<?php
$team_image = '';
function teamImage($team)
{
global $team_image;
switch($team)
{
case "Baltimore Orioles":
$team_image = "orioles";
break;
case "New York Yankees":
$team_image = "yankees";
break;
case "Toronto Blue Jays":
$team_image = "bluejays";
break;
default:
$team_image = "none";
}
return $team_image;
}
teamImage('Baltimore Orioles');
echo $team_image;
?>