在 PHP 中将变量从一个函数传递到另一个函数时遇到问题


Trouble passing a variable from one function to another in PHP?

我有两个函数,我对如何在它们之间传递变量感到困惑。这是我到目前为止所拥有的。

function copy_photo ($image_url, $image_description){
$rand_string = rand(0001, 9999);//create simple random string   
$image_type = substr(strrchr($image_url,'.'),1);//get image type
$local_directory = "./images/";//folder to put photo
$local_image_name = $local_directory . $image_description . "-" . $rand_string . $image_type;//full directory and new image name to place photo 
copy($image_url, $local_image_name);// I want the function to first copy the image here
return $local_image_name;// <---this is the variable I want to pass on
}
function store_data ($local_image_name){
 //simple PDO statement to insert newly created local image url into database
}
copy_photo ($image_url, $image_description);//copy photos to folder 
store_data ($image_description);//store $image_description in database

我想发生的是让copy_photo函数首先复制照片,然后返回 $local_image_name 并将其传递给 store_data 函数以存储在数据库中。当store_data尝试将其存储到数据库中时,我不断收到 $image_description 为空的错误。如何成功地将 $local_image_name 从一个函数传递到下一个函数?

谢谢!!

将函数的返回值分配给变量,然后将该变量传递给下一个函数。

$image_name=copy_photo ($image_url, $image_description);//copy photos to folder 
store_data ($image_name);//store $image_description in database