0

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

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 从一个函数传递到下一个函数?

谢谢!!

4

1 回答 1

1

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

$image_name=copy_photo ($image_url, $image_description);//copy photos to folder 
store_data ($image_name);//store $image_description in database
于 2013-01-26T06:01:21.093 回答