tags:

views:

336

answers:

10

I have this function which declares variables:

function imageSize($name, $nr, $category){
    $path = 'ad_images/'.$category.'/'.$name.'.jpg';
    $path_thumb = 'ad_images/'.$category.'/thumbs/'.$name.'.jpg';
    list($width, $height) = getimagesize($path);
    list($thumb_width, $thumb_height) = getimagesize($path_thumb);
     ${'thumb_image_' . $nr . '_width'} = $thumb_width;
     ${'thumb_image_' . $nr . '_height'} = $thumb_height;
     ${'image_' . $nr . '_width'} = $width;
     ${'image_' . $nr . '_height'} = $height;
}

When I echo this:

   echo $image_1_width

It works fine, but if I do it OUTSIDE the function it wont recognize the variable, how can I make them 'global' somehow?

Thanks

+3  A: 

You will have to define them outside of the function. And inside the function use the global keyword before you use them:

$someVar = null;

function SomeFunc () {
    global $someVar;
    // change $someVar
}

// somewhere later
SomeFunc ();
echo $someVar;

be advised though, that this is a really bad design choice!

Jan Hančič
Agreed. I'd recommend having the function return those values as an array rather than using globals.
Mark Biek
why is it bad? give me some arguments as well...
Camran
If you are using globals you never know when and who will modify them and it breaks the flow of the code.Look at some of the suggestions in other answers for clues on how to rewrite your function (hint: return the stuff you need)
Jan Hančič
A: 

Use global?

PHP: Variable scope - Manual

Chris
A: 
$var = 4;

function bla ()
{
  global $var;
  return $var;
}

It will return 4, because the variable is global. Hope this is what you want.

LuRsT
return is vastly different from echo.
davethegr8
+11  A: 

I would strongly advise NOT using global.

What will probably be best is for you to return from the fucntion:

function imageSize($name, $nr, $category){
    $path = 'ad_images/'.$category.'/'.$name.'.jpg';
    $path_thumb = 'ad_images/'.$category.'/thumbs/'.$name.'.jpg';
    list($width, $height) = getimagesize($path);
    list($thumb_width, $thumb_height) = getimagesize($path_thumb);
        ${'thumb_image_' . $nr . '_width'} = $thumb_width;
        ${'thumb_image_' . $nr . '_height'} = $thumb_height;
        ${'image_' . $nr . '_width'} = $width;
        ${'image_' . $nr . '_height'} = $height;

    $myarr = array();
    $myarr['thumb_image_' . $nr . '_width'] = $thumb_width;
    $myarr['thumb_image_' . $nr . '_height'] = $thumb_height;
    $myarr['image_image_' . $nr . '_width'] = $width;
    $myarr['image_image_' . $nr . '_height'] = $height;
    return $myarr;

}

$myImage = imageSize($name, $nr, $category);

then you access each var:

echo $myImage['thumb_image_1_width'];
echo $myImage['thumb_image_1_height'];
echo $myImage['image_1_weight'];
echo $myImage['image_1_height'];

etc

Lizard
A: 

return the value inside the function instead of using globals

uji
+1  A: 

It sounds as though you'd like to implement a class rather than a function. Something like:

class myImage {

   function __construct($name, $nr, $category){
      $path = 'ad_images/'.$category.'/'.$name.'.jpg';
      $path_thumb = 'ad_images/'.$category.'/thumbs/'.$name.'.jpg';
      list($width, $height) = getimagesize($path);
      list($thumb_width, $thumb_height) = getimagesize($path_thumb);
      $this->{'thumb_image_' . $nr . '_width'} = $thumb_width; 
      $this->{'thumb_image_' . $nr . '_height'} = $thumb_height;
      $this->{'image_' . $nr . '_width'} = $width;
      $this->{'image_' . $nr . '_height'} = $height;  
   }
}

$image= new myImage($name, $nr, $category);
echo $image->'image_1_width';

Of course, with this sort of construction, you don't have to glue together variable names. You can just have $image->width.

dnagirl
+2  A: 

You could also create an array or an object then return that - e.g.

$dimensions[$nr] = imageSize($name,$category);
echo "Thumb width " . $dimensions[$nr]['thumb_width'];

Then in the function itself

function imageSize($name, $category)
{
    $path = 'ad_images/'.$category.'/'.$name.'.jpg';
    $path_thumb = 'ad_images/'.$category.'/thumbs/'.$name.'.jpg';
    list($width, $height) = getimagesize($path);
    list($thumb_width, $thumb_height) = getimagesize($path_thumb);

    $rsvp = Array();
    $rsvp['thumb_width']  = $thumb_width;
    $rsvp['thumb_height'] = $thumb_height;
    $rsvp['image_width']  = $width;
    $rsvp['image_height'] = $height;

    return $rsvp;
}
Kev
+1  A: 

I second @Lizard's answer. Also, it sounds like reading up on variable scope a bit wouldn't go amiss. (Note, that link does include explanation of how to use global variables. Like many here have said, that's not the best road to go down.)

keithjgrant
+1  A: 

I'm surprised that no one thought to tell you about extract. It will take the values from an array and turn them into local variables. So, in this case:

function imageSize($name, $nr, $category){
 $path = 'ad_images/'.$category.'/'.$name.'.jpg';
 $path_thumb = 'ad_images/'.$category.'/thumbs/'.$name.'.jpg';

 $myarr = array();
 $myarr['thumb_image_' . $nr . '_width'] = $thumb_width;
 $myarr['thumb_image_' . $nr . '_height'] = $thumb_height;
 $myarr['image_image_' . $nr . '_width'] = $width;
 $myarr['image_image_' . $nr . '_height'] = $height;
 return $myarr;

}

$myImage = imageSize('myName', 'foo', $category);
extract( $myImage );

You'll now have the variables,

$thumb_image_foo_width;
$thumb_image_foo_height;
$image_image_foo_width;
$image_image_foo_height;

in local scope.

Christopher W. Allen-Poole
A: 

If you declare the variable as part of the $GLOBALS array, you will be adding it to the global scope.

$GLOBALS['variable name'] = 'value';

As others have mentioned, globals should be initialized in the global scope, for example to NULL.

Brent Baisley