Is there a way to define a php variable to be one or the other just like you would do var x = (y||z)
in javascript?
是否有一种方法可以将php变量定义为一个或另一个,就像在javascript中使用var x = (y||z)一样?
Get the size of the screen, current web page and browser window.
获取屏幕大小、当前网页和浏览器窗口。
var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; var height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
i'm sending a post variable and i want to store it for a later use in a session. What i want to accomplish is to set $x
to the value of $_POST['x']
, if any exist, then check and use $_SESSION['x']
if it exist and leave $x
undefined if neither of them are set;
我正在发送一个post变量,我想将它保存到以后的会话中使用。我要完成的是将$x设置为$_POST['x']的值,如果存在,则检查并使用$_SESSION['x'],如果没有设置,则不定义$x;
$x = ($_POST['x'] || $_SESSION['x');
According to http://php.net/manual/en/language.operators.logical.php
据http://php.net/manual/en/language.operators.logical.php
$a = 0 || 'avacado'; print "A: $a\n";
$a = 0 || 'avacado';打印“:$ \ n”;
will print:
将打印:
A: 1
答:1
in PHP -- as opposed to printing "A: avacado" as it would in a language like Perl or JavaScript.
在PHP中——而不是像在Perl或JavaScript那样打印“A: avacado”。
This means you can't use the '||' operator to set a default value:
这意味着不能使用“||”操作符设置默认值:
$a = $fruit || 'apple';
$a = $fruit || 'apple';
instead, you have to use the '?:' operator:
相反,你必须使用?:“接线员:
$a = ($fruit ? $fruit : 'apple');
(a =美元水果吗?美元的水果:“苹果”);
so i had to go with an extra if encapsulating the ?: operation like so:
因此,如果要封装?:操作,我就必须添加一个额外的:
if($_POST['x'] || $_SESSION['x']){
$x = ($_POST['x']?$_POST['x']:$_SESSION['x']);
}
or the equivalent also working:
或者同样有效的:
if($_POST['x']){
$x=$_POST['x'];
}elseif($_SESSION['x']){
$x=$_SESSION['x'];
}
I didn't test theses but i presume they would work as well:
我没有测试这些论文,但我认为它们也可以:
$x = ($_POST['x']?$_POST['x']:
($_SESSION['x']?$_SESSION['x']:null)
);
for more variables i would go for a function (not tested):
对于更多的变量,我会选择一个函数(未测试):
function mvar(){
foreach(func_get_args() as $v){
if(isset($v)){
return $v;
}
} return false;
}
$x=mvar($_POST['x'],$_SESSION['x']);
Any simple way to achieve the same in php?
在php中有什么简单的实现方法吗?
EDIT for clarification: in the case we want to use many variables $x=($a||$b||$c||$d);
编辑澄清:在这个例子中,我们想要使用许多变量$x=($a| bbbbbbbb| $c||$d);
4 个解决方案
#1
2
Update
I've managed to create a function for you that achieves exactly what you desire, allowing infinite arguements being supplied and fetching as you desire:
我已经成功地为您创建了一个函数,它实现了您想要的效果,允许您提供无限的讨论,并根据您的需要进行取回:
function _vars() {
$args = func_get_args();
// loop through until we find one that isn't empty
foreach($args as &$item) {
// if empty
if(empty($item)) {
// remove the item from the array
unset($item);
} else {
// return the first found item that exists
return $item;
}
}
// return false if nothing found
return false;
}
To understand the function above, simply read the comments above.
要理解上面的函数,只需阅读上面的注释。
Usage:
用法:
$a = _vars($_POST['x'], $_SESSION['x']);
And here is your:
这是你的:
例子
It is a very simply ternary operation. You simply need to check the post first and then check the session after:
这是一个非常简单的三元运算。你只需要先查看邮件,然后再查看会议:
$a = (isset($_POST['x']) && !empty($_POST['x']) ?
$_POST['x']
:
(isset($_SESSION['x']) && !empty($_SESSION['x']) ? $_SESSION['x'] : null)
);
#2
2
Yes you need to use simple ternary
operator which you have used within your example along with some other functions of PHP like as of isset
or empty
functions of PHP. So your variable $x
will be assigned values respectively
是的,您需要使用简单的三元运算符,这是您在示例中使用过的,还有一些PHP的其他函数,如isset或PHP的空函数。变量$x将被分别赋值
Example
例子
$x = (!empty($_POST['x'])) ? $_POST['x'] : (!empty($_SESSION['x'])) ? $_SESSION['x'] : NULL;
So the above function depicts that if your $_POST['x']
is set than the value of
因此,上面的函数描述了如果您的$_POST['x']是设置的,而不是值。
$x = $_POST['x'];
else it'll check for the next value of $_SESSION
if its set then the value of $x
will be
否则,它将检查$_SESSION的下一个值,如果它的集合,那么$x的值将是
$x = $_SESSION['x'];
else the final value'll be
否则最后的值就是
$x = null;// you can set your predefined value instead of null
$x = (!empty($a)) ? $a : (!empty($b)) ? $b : (!empty($c)) ? $c : (!empty($d)) ? $d : null;
If you need a function then you can simply achieve it as
如果您需要一个函数,那么您可以简单地实现它
$a = '';
$b = 'hello';
$c = '';
$d = 'post';
function getX(){
$args = func_get_args();
$counter = 1;
return current(array_filter($args,function($c) use (&$counter){ if(!empty($c) && $counter++ == 1){return $c;} }));
}
$x = getX($a, $b, $c, $d);
echo $x;
#3
1
A simpler approach is to create a function that can accept variables.
一种更简单的方法是创建一个可以接受变量的函数。
public function getFirstValid(&...$params){
foreach($params as $param){
if (isset($param)){
return $param;
}
}
return null;
}
and then to initialize a variable i would do...
然后初始化一个变量。
var $x = getFirstValid($_POST["x"],$_SESSION["y"],$_POST["z");
the result will be that the var x will be assign the first variable that is set or is set to null if none of the variables pass are set.
结果是,如果没有设置任何变量,那么var x将被赋值为设置的第一个变量,或者设置为null。
explanation:
解释:
function getFirstValid accepts a variable number of variable pointers(&...) and loops through each checking if it is set, the first variable encountered that is set will be returned.
函数getFirstValid接受变量指针(&…)的变量数,并循环遍历每次检查,如果它被设置,将返回遇到的第一个被设置的变量。
#4
0
It would be simple -
很简单-
$x = (!empty($_POST['x']) ? $_POST['x'] :
(!empty($_SESSION['x']) ? $_SESSION['x'] : null)
);
#1
2
Update
I've managed to create a function for you that achieves exactly what you desire, allowing infinite arguements being supplied and fetching as you desire:
我已经成功地为您创建了一个函数,它实现了您想要的效果,允许您提供无限的讨论,并根据您的需要进行取回:
function _vars() {
$args = func_get_args();
// loop through until we find one that isn't empty
foreach($args as &$item) {
// if empty
if(empty($item)) {
// remove the item from the array
unset($item);
} else {
// return the first found item that exists
return $item;
}
}
// return false if nothing found
return false;
}
To understand the function above, simply read the comments above.
要理解上面的函数,只需阅读上面的注释。
Usage:
用法:
$a = _vars($_POST['x'], $_SESSION['x']);
And here is your:
这是你的:
例子
It is a very simply ternary operation. You simply need to check the post first and then check the session after:
这是一个非常简单的三元运算。你只需要先查看邮件,然后再查看会议:
$a = (isset($_POST['x']) && !empty($_POST['x']) ?
$_POST['x']
:
(isset($_SESSION['x']) && !empty($_SESSION['x']) ? $_SESSION['x'] : null)
);
#2
2
Yes you need to use simple ternary
operator which you have used within your example along with some other functions of PHP like as of isset
or empty
functions of PHP. So your variable $x
will be assigned values respectively
是的,您需要使用简单的三元运算符,这是您在示例中使用过的,还有一些PHP的其他函数,如isset或PHP的空函数。变量$x将被分别赋值
Example
例子
$x = (!empty($_POST['x'])) ? $_POST['x'] : (!empty($_SESSION['x'])) ? $_SESSION['x'] : NULL;
So the above function depicts that if your $_POST['x']
is set than the value of
因此,上面的函数描述了如果您的$_POST['x']是设置的,而不是值。
$x = $_POST['x'];
else it'll check for the next value of $_SESSION
if its set then the value of $x
will be
否则,它将检查$_SESSION的下一个值,如果它的集合,那么$x的值将是
$x = $_SESSION['x'];
else the final value'll be
否则最后的值就是
$x = null;// you can set your predefined value instead of null
$x = (!empty($a)) ? $a : (!empty($b)) ? $b : (!empty($c)) ? $c : (!empty($d)) ? $d : null;
If you need a function then you can simply achieve it as
如果您需要一个函数,那么您可以简单地实现它
$a = '';
$b = 'hello';
$c = '';
$d = 'post';
function getX(){
$args = func_get_args();
$counter = 1;
return current(array_filter($args,function($c) use (&$counter){ if(!empty($c) && $counter++ == 1){return $c;} }));
}
$x = getX($a, $b, $c, $d);
echo $x;
#3
1
A simpler approach is to create a function that can accept variables.
一种更简单的方法是创建一个可以接受变量的函数。
public function getFirstValid(&...$params){
foreach($params as $param){
if (isset($param)){
return $param;
}
}
return null;
}
and then to initialize a variable i would do...
然后初始化一个变量。
var $x = getFirstValid($_POST["x"],$_SESSION["y"],$_POST["z");
the result will be that the var x will be assign the first variable that is set or is set to null if none of the variables pass are set.
结果是,如果没有设置任何变量,那么var x将被赋值为设置的第一个变量,或者设置为null。
explanation:
解释:
function getFirstValid accepts a variable number of variable pointers(&...) and loops through each checking if it is set, the first variable encountered that is set will be returned.
函数getFirstValid接受变量指针(&…)的变量数,并循环遍历每次检查,如果它被设置,将返回遇到的第一个被设置的变量。
#4
0
It would be simple -
很简单-
$x = (!empty($_POST['x']) ? $_POST['x'] :
(!empty($_SESSION['x']) ? $_SESSION['x'] : null)
);