יש כמה דרכים או להחזיר ערך מהפונקציה לדוגמא לתוך משתנה ככה:
PHP קוד:
function image() {
header ("Content-type: image/png");
$string = substr(md5(time()), 0,5);
$font = 5;
$width = 60;
$height = 22;
$im = @imagecreate ($width,$height);
$background_color = imagecolorallocate ($im, 224, 224, 224);
$text_color = imagecolorallocate($im, 255, 0, 0);
imagestring ($im, $font, 7, 5,$string, $text_color);
imagepng ($im);
return $string;
}
$image = image();
או לעשות משתנה גלובאלי...
PHP קוד:
$image = "";
function image() {
global $image;
header ("Content-type: image/png");
$string = substr(md5(time()), 0,5);
$font = 5;
$width = 60;
$height = 22;
$im = @imagecreate ($width,$height);
$background_color = imagecolorallocate ($im, 224, 224, 224);
$text_color = imagecolorallocate($im, 255, 0, 0);
imagestring ($im, $font, 7, 5,$string, $text_color);
imagepng ($im);
$image = $string;
}
וככה זה ישמור לך אותו
יש עוד דרכים כמו לקבל פרמטר בפונקציה עם & אבל זה יסבך אותך בתור התחלה...
אני משתמש בד"כ בשיטה הראשונה שהצגתי היא הכי נוחה ומסודרת