PDA

View Full Version : تبدیل نتیجه تابع به متغیر



artablog
پنج شنبه 02 بهمن 1393, 20:25 عصر
سلام
فرض کنید وقتی از تابع زیر در یک صفحه استفاده میکنم

my_function($text) ;
کلمه hello در صفحه پرینت میشه حالا من میخوام این تابع را بریزم تو یه متغیر چیکار کنممثلا :

$my_variabl = my_function($text) ;
echo $variabl; // resut = hello

فرزند کوروش
پنج شنبه 02 بهمن 1393, 20:46 عصر
function my_function ($text){
return $text;
}
$str = my_function('hello');
echo $str; //output:hello

artablog
پنج شنبه 02 بهمن 1393, 20:57 عصر
ممنون از پاسختون
تو تابع
my_function ($text) نمیتونم از return استفاده کنم الان چطور میطونم همیتو که هست بریزم تو متغییر؟

us1234
پنج شنبه 02 بهمن 1393, 21:10 عصر
ممنون از پاسختون
تو تابع
my_function ($text) نمیتونم از return استفاده کنم الان چطور میطونم همیتو که هست بریزم تو متغییر؟

دقیقا چرا نمیتونید از return استفاده کنید ؟!

می توانید تابع call by reference تعریف کنید :



function my_function (& $text);

echo $text;

abolfazl-z
پنج شنبه 02 بهمن 1393, 21:45 عصر
function test($myText)
{
$GLOBALS['var1'] = 'hello';
}
test('a');
echo $var1;

Tarragon
پنج شنبه 02 بهمن 1393, 22:37 عصر
ob_start();
my_function('abcdefgh ... ... ...'); //would normally get printed to the screen/output to browser
$output = ob_get_contents();
ob_end_clean();
echo $output;// @return result of my_function

artablog
جمعه 03 بهمن 1393, 13:29 عصر
ob_start();
my_function('abcdefgh ... ... ...'); //would normally get printed to the screen/output to browser
$output = ob_get_contents();
ob_end_clean();
echo $output;// @return result of my_function

ممنون این جواب داد