Jump to content

array in a function


poe

Recommended Posts

i have this function

 

function showImg($filename) {

 

if($filename != "") {

$jpg = $filename;

$messageAry['title'] = "woohoo, we have an image";

} else {

$jpg = "default.jpg";

$messageAry['title'] = "no image found";

}

 

return $jpg;

} // end function

 

$thepic = "superstar.jpg";

echo "<img src='".showImg($thepic)."'>";

 

 

 

but when i try to print the $messageAry for later use it doesnt show.

print_r($messageAry);

 

Link to comment
https://forums.phpfreaks.com/topic/42185-array-in-a-function/
Share on other sites

You need to declare the $messageAry global inside your function to access it outside of your function.

 

function showImg($filename) {
global $messageAry;
if($filename != "") {
$jpg = $filename;
$messageAry['title'] = "woohoo, we have an image";
} else {
$jpg = "default.jpg";
$messageAry['title'] = "no image found";
}

return $jpg;
} // end function

 

Jeremy

Link to comment
https://forums.phpfreaks.com/topic/42185-array-in-a-function/#findComment-204664
Share on other sites

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.