PHP Define var = one or the other (aka: $ var = ($ a || $ b);)

Is there a way to define a php variable as one or the other, as you would do var x = (y||z) in javascript?

Get screen size, 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 am sending a post variable and I want to save it for later use in the session. What I want to do is set $x to $_POST['x'] if it exists, then check and use $_SESSION['x'] if it exists, and leave $x undefined if none of they are not installed;

 $x = ($_POST['x'] || $_SESSION['x'); 

According to http://php.net/manual/en/language.operators.logical.php

$ a = 0 || 'Avacado'; print "A: $ a \ n";

will print:

A: 1

in PHP, as opposed to printing "A: avacado", as it would in a language such as Perl or JavaScript.

This means that you cannot use '||' statement to set the default value:

$ a = $ fruit || 'An Apple';

instead, you should use the "?:" operator:

$ a = ($ fruit? $ fruit: 'apple');

so I had to go with the extra if encapsulating the operation ?: like this:

 if($_POST['x'] || $_SESSION['x']){ $x = ($_POST['x']?$_POST['x']:$_SESSION['x']); } 

or the equivalent also works:

 if($_POST['x']){ $x=$_POST['x']; }elseif($_SESSION['x']){ $x=$_SESSION['x']; } 

I have not tested the theses, but I believe that they will work too:

 $x = ($_POST['x']?$_POST['x']: ($_SESSION['x']?$_SESSION['x']:null) ); 

for a larger number of 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 easy way to achieve the same in php?

EDIT for clarification: in the case when we want to use a lot of variables $x=($a||$b||$c||$d);

+6
source share
4 answers

Update

I managed to create a function for you that achieves exactly what you want, allowing endless arguments to be supplied and received at will:

 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 above function, just read the comments above.

Using:

 $a = _vars($_POST['x'], $_SESSION['x']); 

And here is yours:

Example


This is a very simple triple operation. You just need to check the message 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
source

Yes, you need to use the simple ternary operator that you used in your example, as well as some other PHP functions, for example, from isset or empty PHP functions. Thus, your variable $x will be assigned values ​​accordingly

Example

 $x = (!empty($_POST['x'])) ? $_POST['x'] : (!empty($_SESSION['x'])) ? $_SESSION['x'] : NULL; 

So the above function shows that if your $_POST['x'] set than the value

 $x = $_POST['x']; 

else, it will check the next value of $_SESSION , if its value, then the value of $x will be

 $x = $_SESSION['x']; 

else the final value will 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, 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; 
+2
source

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 the variable I will do ...

 var $x = getFirstValid($_POST["x"],$_SESSION["y"],$_POST["z"); 

the result will be that var x will assign the first variable that is set or set to null if none of the variables pass.

explanation:

the getFirstValid function takes a variable number of variable pointers (& ...) and cyclically checks to see if it is set, the first variable to be set will be returned, and will be returned.

+1
source

It would be simple -

 $x = (!empty($_POST['x']) ? $_POST['x'] : (!empty($_SESSION['x']) ? $_SESSION['x'] : null) ); 
0
source

Source: https://habr.com/ru/post/989107/


All Articles