我在尝试通过函数传递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值,但每次运行代码它都不起作用,我已经尝试过全局,全局变量,数组和会话,但没有任何工作。我正在制作它的corse在某些方面有点错误,但是有人知道这样做的正确方法是什么?
答案 0 :(得分: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。
答案 1 :(得分:2)
session_start()
$_SESSION
如果您希望在函数中使用它们并遵循最佳实践,则需要将函数中使用的任何变量作为参数传递
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' ");
}
}
答案 2 :(得分:1)
您需要将变量传递给函数,或者在函数内部声明它是全局变量。
E.g。
function updateuserinformation($myvar){
或
global $myvar;
echo $myvar;
此外,在访问会话变量之前,您需要调用session_start()
。