php中的变量在全局变为null

时间:2011-11-25 00:31:44

标签: php function null joomla1.5 global

我已经尝试了许多方法并且之前没有遇到任何问题,但我无法让这个工作:

$item_img = "my_image_name.jpg";

function GetImage(){
    global $item_img;
    return $item_img;
}

返回null。为什么呢?

2 个答案:

答案 0 :(得分:1)

<?php

$item_img = "my_image_name.jpg";

function GetImage(){
    global $item_img;
    return $item_img;
}

var_dump( GetImage() );

?>

它返回 string(17)“my_image_name.jpg”而不是NULL。你做错了什么......

在Joomla模块中试试这个:

<?php

$item_img = "my_image_name.jpg";

function GetImage($img){
    # do something with $img
    return $img;
}

var_dump( GetImage($item_img) );

?>

答案 1 :(得分:0)

不会为我返回null。您使用的是哪个版本的PHP?

<?php
$item_img = "my_image_name.jpg";

function GetImage(){
    global $item_img;
    return $item_img;
}

var_dump(GetImage());
// string 'my_image_name.jpg' (length=17)