1

我在尝试通过函数传递 PHP 变量值时遇到了一些麻烦,每次我尝试在我的一个函数中使用变量时,它的值都会变为 nil,让我更具体一些。我的 php 文件中有以下代码:

$myvar = $Session['username'];

function updateuserinformation(){

        if(trim($_FILES["fileUpload"]["tmp_name"]) != ""){

            $images = $_FILES["fileUpload"]["tmp_name"];
            $new_images = "thumbnails_".$_FILES["fileUpload"]["name"];
            copy($_FILES["fileUpload"]["tmp_name"],"Photos/".$_FILES["fileUpload"]["name"]);
            $width=200; //*** Fix Width & Heigh (Autu caculate) ***//
            $size=GetimageSize($images);
            $height=round($width*$size[1]/$size[0]);
            $images_orig = ImageCreateFromJPEG($images);
            $photoX = ImagesX($images_orig);
            $photoY = ImagesY($images_orig);
            $images_fin = ImageCreateTrueColor($width, $height);
            ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
            ImageJPEG($images_fin,"Photos/".$new_images);
            ImageDestroy($images_orig);
            ImageDestroy($images_fin);

            print $data["foo"];

            echo"$myvar";

            mysql_query("UPDATE users SET userpictureaddress = 'http://www.litsdevelopment.com/litsapplication/userimages/MATEUS' WHERE username = 'Mateus' ");

        }
}

我试图在我的函数中使用 $myvar 值,但每次运行代码时它都不起作用,我已经尝试过全局、全局、数组和会话,但没有任何效果。当然我犯了一个小错误在它的某些部分,但有人知道这样做的正确方法是什么?

4

3 回答 3

2

如果您想了解变量范围在 PHP 中的工作原理,请阅读以下内容:

http://php.net/manual/en/language.variables.scope.php

$myvar = $Session['username'];

function updateuserinformation(){
    global $myvar;

        if(trim($_FILES["fileUpload"]["tmp_name"]) != ""){

            $images = $_FILES["fileUpload"]["tmp_name"];
            $new_images = "thumbnails_".$_FILES["fileUpload"]["name"];
            copy($_FILES["fileUpload"]["tmp_name"],"Photos/".$_FILES["fileUpload"]["name"]);
            $width=200; //*** Fix Width & Heigh (Autu caculate) ***//
            $size=GetimageSize($images);
            $height=round($width*$size[1]/$size[0]);
            $images_orig = ImageCreateFromJPEG($images);
            $photoX = ImagesX($images_orig);
            $photoY = ImagesY($images_orig);
            $images_fin = ImageCreateTrueColor($width, $height);
            ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
            ImageJPEG($images_fin,"Photos/".$new_images);
            ImageDestroy($images_orig);
            ImageDestroy($images_fin);

            print $data["foo"];

            echo"$myvar";

            mysql_query("UPDATE users SET userpictureaddress = 'http://www.litsdevelopment.com/litsapplication/userimages/MATEUS' WHERE username = 'Mateus' ");

        }
}

但是正如@JohnConde 指出的那样,如果您尝试从会话中读取,那么您的意思似乎是 $_SESSION 。

于 2012-04-06T23:24:07.123 回答
2
  1. 你没有打电话session_start()
  2. 你拼错了$_SESSION
  3. 如果您希望在函数中使用它们并遵循最佳实践,则需要将要在函数中使用的任何变量作为参数传递

    session_start();
    $myvar = $_SESSION['username'];
    
    function updateuserinformation($myvar){
    
        if(trim($_FILES["fileUpload"]["tmp_name"]) != ""){
    
            $images = $_FILES["fileUpload"]["tmp_name"];
            $new_images = "thumbnails_".$_FILES["fileUpload"]["name"];
            copy($_FILES["fileUpload"]["tmp_name"],"Photos/".$_FILES["fileUpload"]["name"]);
            $width=200; //*** Fix Width & Heigh (Autu caculate) ***//
            $size=GetimageSize($images);
            $height=round($width*$size[1]/$size[0]);
            $images_orig = ImageCreateFromJPEG($images);
            $photoX = ImagesX($images_orig);
            $photoY = ImagesY($images_orig);
            $images_fin = ImageCreateTrueColor($width, $height);
            ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
            ImageJPEG($images_fin,"Photos/".$new_images);
            ImageDestroy($images_orig);
            ImageDestroy($images_fin);
    
            print $data["foo"];
    
            echo"$myvar";
    
            mysql_query("UPDATE users SET userpictureaddress = 'http://www.litsdevelopment.com/litsapplication/userimages/MATEUS' WHERE username = 'Mateus' ");
    
        }
    

    }

于 2012-04-06T23:25:28.450 回答
1

您需要将变量传递给函数,或者在函数内部声明它是全局变量。

例如

function updateuserinformation($myvar){

或者

global $myvar;
echo $myvar;

此外,在访问使用会话变量之前,您需要调用session_start().

于 2012-04-06T23:20:10.327 回答