我有两个函数,我很难理解如何在它们之间传递变量。这是我到目前为止所拥有的。
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为null的错误。如何成功将$ local_image_name从一个函数传递到下一个函数?
谢谢!
答案 0 :(得分:1)
将函数的返回值赋给变量,然后将该变量传递给下一个函数。
$image_name=copy_photo ($image_url, $image_description);//copy photos to folder
store_data ($image_name);//store $image_description in database