Php if、else条件之间的变量
我想在if/else条件的两个块之间使用一个变量Php if、else条件之间的变量,php,Php,我想在if/else条件的两个块之间使用一个变量 if(isset($_GET['aaa'])) { code to be executed; $example='a string or codes(calculating)'; } else //if(isset($_GET['bbb'])) { code to be executed; } 如何在else子句中使用$example?解释一下,例如,第一次if子句为true,计算$example,第二次加载else为true,现
if(isset($_GET['aaa']))
{
code to be executed;
$example='a string or codes(calculating)';
}
else //if(isset($_GET['bbb']))
{
code to be executed;
}
如何在else子句中使用$example?解释一下,例如,第一次if子句为true,计算$example,第二次加载else为true,现在我想使用$example。我将$example声明为静态变量,但它不起作用。
感谢您的帮助:)您可以为此使用会话变量: 添加 在PHP脚本的最开始部分 您的代码可能如下所示:
if(!isset($_SESSION['example'])
{
$_SESSION['example'] = some default value;
}
if(isset($_GET['aaa']))
{
code to be executed;
$_SESSION['example'] ='a string or codes(calculating)';
}
else
{
code to be executed;
use $_SESSION['example'] here
}
$\u会话['example']
可以在任何地方获取您想要的值,您也可以在页面刷新后读取该值
如果您需要有关$\u会话的更多示例和解释,您可以查看此网站:在这两种情况下使用
$example
的最佳方法是在之前声明它,如果
:
$example = '';
if(isset($_GET['aaa']))
{
code to be executed;
$example='a string or codes(calculating)';
}
else
{
//other stuff using $example
$example = 'foo';
}
阅读可能会很有帮助。在你的情况下,尝试会话
if(isset($_GET['aaa']))
{
code to be executed;
$_SESSIONS['example']='a string or codes(calculating)';
}
else
{
//other stuff using $_SESSIONS['example']
$_SESSIONS['example'] = 'foo';
}
或者您可能想在使用某个默认值阻塞之前初始化$\u会话['example'],您的问题是什么?您是否假设变量在不同的请求中可用,或者该代码包装在一个被多次调用的函数中?要在else子句中使用$example,必须在if构造之外定义它,并用默认值初始化它。如果您希望变量在请求之间保持不变,请使用其他方法,如会话、数据库等。第二次加载时,您是指重新加载/刷新页面,还是指此代码是php脚本中调用两次的函数的一部分?@Francodi我指的是页面reload@Rangad这不是一个函数。这个I/EL子句刚好在pAGOK的中间,所以我没有删除我的答案(请看下面的),因为我没有误解你——一个会话是实现这一点的一种方式。
if(isset($_GET['aaa']))
{
code to be executed;
$_SESSIONS['example']='a string or codes(calculating)';
}
else
{
//other stuff using $_SESSIONS['example']
$_SESSIONS['example'] = 'foo';
}