我需要这个。可能吗?使用函数变量之外的函数WiTHOUT调用函数
我尝试以下,但它不工作:
$test;
function func_name() {
global $test;
$test = 'string';
}
echo $test; // I get nothing
我需要这个。可能吗?使用函数变量之外的函数WiTHOUT调用函数
我尝试以下,但它不工作:
$test;
function func_name() {
global $test;
$test = 'string';
}
echo $test; // I get nothing
如果不调用函数,什么都不会发生。
您需要echo $test;
不要使用global
代替参数传递给你的函数。你也没有从你的函数返回值,也没有调用你的函数func_name
。
你一定在做这样的事情。
<?php
function func_name() { //<---- Removed the global keyword as it is a bad practice
$test = 'string';
return $test; //<---- Added a retuen keyword
}
$test=func_name(); //<---- Calls your function and the value is returned here
echo $test; //"prints" string
可以像
function func_name() {
$test = 'string';
return $test;
}
echo func_name();
甚至你可以尝试像
function func_name() {
$test = 'string';
echo $test;
}
func_name();
如果能够避免它永远不会使用全局变量添加
func_name();
。 –@RonniSkansing我did'nt – Gautam3164
使用$这个课外不会工作 – MSadura