在PHP中使变量在函数外部可用?

在PHP中使变量在函数外部可用?,php,Php,我有一个声明变量的函数: 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, $th

我有一个声明变量的函数:

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;
}
当我回应这一点时:

   echo $image_1_width
它工作得很好,但是如果我在函数外执行它,它将无法识别变量,我如何使它们以某种方式“全局”呢


谢谢

您必须在函数之外定义它们。在函数内部,请在使用它们之前使用全局关键字:

$someVar = null;

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

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

但请注意,这是一个非常糟糕的设计选择

使用
全局


它将返回4,因为该变量是全局变量。希望这是您想要的。

我强烈建议不要使用global

您最好从函数返回:

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)

然后访问每个变量:

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

等等。

返回函数内部的值,而不是使用全局变量。

听起来好像您想要实现一个类而不是函数。比如:

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';

当然,使用这种结构,您不必将变量名粘在一起。您可以只使用
$image->width

您还可以创建一个数组或对象,然后返回该数组或对象-例如

$dimensions[$nr] = imageSize($name,$category);
echo "Thumb width " . $dimensions[$nr]['thumb_width'];
然后在函数本身

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;
}

我支持“蜥蜴”的回答。而且,听起来读点东西不会出错。(注意,这个链接确实包含了如何使用全局变量的解释。正如这里的许多人所说,这并不是最好的选择。)

我很惊讶没有人想到告诉你有关提取的事情。它将从数组中获取值,并将它们转换为局部变量。因此,在这种情况下:

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 );
现在您将拥有变量

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

在局部作用域中。

如果将变量声明为$GLOBALS数组的一部分,则将其添加到全局作用域中

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

正如其他人所提到的,globals应该在全局范围内初始化,例如为NULL。

同意。我建议让函数以数组的形式返回这些值,而不是使用全局值。如果使用全局值,您永远不知道何时以及谁将修改它们,这会中断代码流。看看其他答案中的一些建议,寻找关于如何重写函数的线索(提示:返回您需要的东西)return与echo有很大的不同。这一点很好!我以前不知道
extract()
。我总是使用:
while(list($key,$val)=each($array)){$$key=$val;}
+1。
$GLOBALS['variable name'] = 'value';