Make variables accessible outside functions in PHP?

I have this function that 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 repeat this:

  echo $image_1_width 

It works fine, but if I do it outside of this function, it will not recognize the variable, how can I make them "global" somehow?

thanks

+6
php
source share
10 answers

I would strongly advise NOT to use global ones.

Most likely, you better return from the function:

 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 get access to each var:

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

and etc.

+13
source share

You will need to define them outside the function. And inside the function, use the global keyword before using them:

 $someVar = null; function SomeFunc () { global $someVar; // change $someVar } // somewhere later SomeFunc (); echo $someVar; 

however, this is a very poor design choice!

+8
source share

You can also create an array or object and then return it - for example,

 $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; } 
+2
source share

I am surprised that no one thought to tell you about the extraction. It will take values ​​from the 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 ); 

Now you have the variables

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

in the local area.

+2
source share

It sounds like you want to implement a class, not 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 such a design, you do not need to glue the variable names. You can just $image->width .

+1
source share

Second answer @Lizard. In addition, it sounds like reading on the scope of a variable will not go a little awry. (Note that this link includes an explanation of how to use global variables. As many have said here, this is not the best way to go down.)

+1
source share
0
source share

returns the value inside the function instead of using global variables

0
source share

If you declare a variable as part of the $ GLOBALS array, you add it to the global scope.

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

As already mentioned, global global extensions must be initialized globally, for example, in NULL.

0
source share
 $var = 4; function bla () { global $var; return $var; } 

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

-one
source share

All Articles