I really enjoy || operator in JavaScript, where we can do inline conditional assignation.
var a = 0;
var b = 42;
var test = a || b || 'default value';
console.log(test); // 42
This is clear to read, and don't take too many lines.
In PHP, this logical operator return booleans:
$a = 0;
$b = 42;
$test = $a || $b || 'default value';
print_r($test); // bool(true)
Of course, we can do inline assignation using ternaries:
$test = $a ? $a : $b ? $b : 'default';
print_r($test); // int(42)
But this make code ambiguous, this is not that easy to read.
So here my question come:
Do you know a nice PHP hack to do inline conditional assignation ?
In PHP 5.3+ you can do this:
$test = $a ?: ($b ?: 'default value');
This will work as long as you don't need to short-circuit side effects:
function either_or() {
$nargs = func_num_args();
if ($nargs == 0) {
return false;
}
$args = func_get_args();
for ($i = 0; $i < $nargs-1; $i++) {
if ($args[$i]) {
return $args[$i];
}
}
return $args[$nargs-1];
}
$test = either_or($a, $b, "Default value");
Related
Here is the function:
function simplePresent($e) {
$w = ($f = preg_match)('/ey|se|d |[sI]$|We/', $a = $e[0]);
be == ($b = $e[1])
?
$b = $w ? $a == I ? am : are : is
:
$w ?: $b = $b == have ? has : $b .= $f('/[h-z]$/', $b) ? es : s;
return "$a $b $e[2]";
}
this is a solution on codefights. it handles formatting under defined parameters. I am trying to understand the function assignment to the variables $f and $w. Also the the use of variables with no quotes. lastly the nested question marks and colons. How is this functioning?
Thanks
The function preg_match() is being assigned to the variable $f and reused later in the code. $w is simply the result of preg_match(). For example:
<?php
$w = ($f = 'sprintf')("foo");
// same as this:
$f = 'sprintf';
$w = $f("foo");
// which is the same as this:
$w = sprintf('foo');
The unquoted values will first be interpreted as constants, and the undefined constants will be interpreted as strings. Here's how a proper constant definition looks:
<?php
define("foo", "bar");
echo foo;
echo bar;
// PHP Notice: Use of undefined constant bar - assumed 'bar'
// same as this:
echo "bar";
echo "bar";
The question marks and colons are part of a ternary statement. This is a useful shorthand, but PHP recommends against stacking them together as is done there (the result is "non-obvious.") A simple one looks like this:
<?php
echo ($foo == "foo" ? "equal" : "inequal");
$a = $b ? $c : $d;
// this is the same as:
if ($foo == "foo") {
echo "equal";
} else {
echo "inequal";
}
if ($b) {
$a = $c;
} else {
$a = $d;
}
Is it possible to use the result of an if with an OR statement as a variable for a function?
As example:
$a = true;
$b = false;
if ($a || $b) {
$this->functionCall($a)
}
Other example:
$a = false;
$b = true;
if ($a || $b) {
$this->functionCall($b)
}
Third and final exmaple:
$a = true;
$b = true;
if ($a || $b) {
$this->functionCall($a, $b)
}
So I need to detect what variable is true and pass it as a paramater. Is this even possible?
Any helps is appreciated!
Many thanks in advance
I'd do the logic bit inside a two-parameter function if I were you, as such :
function myFunc($a = false, $b = false) {
if ($a == true)
echo 'a';
if ($b == true)
echo 'b';
}
myFunc(); // echoes nothing
$a = true;
$b = false;
myFunc($a, $b); // echoes 'a'
$a = false;
$b = true;
myFunc($a, $b); // echoes 'b'
$a = true;
$b = true;
myFunc($a, $b); // echoes 'ab'
PHP 5.6+ version, filter out the falsely values (you can pass a callback to array_filter for different checks) and use those with the splat operator.
$params = array_filter([$a, $b]);
$this->callFunction(...$params);
No need for any IF checks and confusing in IF assignments.
Explore Variadic functions and Argument unpacking.
where:
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
I'm not sure how to work out a.
So I understand that this is a shorthand operator, and usually it's a case of:
$value ? true : false
meaning
if $a = true { true } else { false };
so:
if $a{
if $a{
true;}
else{
0;};
else{
if $0{
$a;}
else{
true;}
};
does this make the value of $a true?
The value of $a would be true
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
The shorthand can be interpreted like this:
if($a) {
if($a) {
$a = $b;
} else {
$a = $c;
}
} else {
if($c) {
$a = $a;
} else {
$a = $b;
}
}
Because $a is false for not existing in the first place, it immediately jumps to the else statement in that. So the only part that matters to you is:
if($c) {
$a = $a;
} else {
$a = $b;
}
0 is the same as false, so $c will come back as false, therefore $a is equal to $b, which is true.
Edit:
There is some discussion on the notice that is thrown, but this fails to account for the fact that notices are not truly errors and because of this there is no interruption to the code. The result is not Notice: Undefined variable: a, the "result" (think these people mean output) would be blank if it weren't for us determining the value of $a at the end with var_dump. The question was as to what the value of $a becomes, not what appears on your screen.
Something displaying on your screen in re to a variable not being set has nothing to do with the value of what $a is.
If you execute the following code, the notice is not the only thing realized:
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
var_dump($a);
So the output is:
E_NOTICE : type 8 -- Undefined variable: a -- at line 5
bool(true)
The fact that a notice was thrown does not prevent $a from becoming true.
Also notices are easily suppressed...
error_reporting(0);
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
var_dump($a);
would result in $a still becoming true, and without seeing the notice.
bool(true)
If you run the code as is, you would get: Notice: Undefined variable: a in myfile.php on line 4
Therefore, I would postulate $a is set somewhere earlier. Yet, whatever value $a has prior, if $a is can be evaluated to true or false, $a would still be true after running your code for the following reason:
If $a were true, then the first part would yield $a = $b and we know $b = true.
if(TRUE) {
if(TRUE) {
$a = $b; //AND $b == TRUE
} else {
$a = $c;
}
} else {
...
}
If $a were false, then the second part would yield $a = $b again
if(FALSE) {
...
} else {
if(0) { // 0 will equate to FALSE
...
} else {
// 0 is the same as FALSE so we end up again with $a = $b
$a = $b; //AND $b == TRUE
}
}
In fact, if you run this code, it will show you the value of $a is true both times:
<?php
$a = false;
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
echo $a;
$a = true;
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
echo $a;
I'm creating a function which passing parameter by reference. I just can't get return value with this code and I need your help.
function wow ($id, &$a, &$b)
{
$detail[0][0] = 1;
$detail[0][1] = 2;
$detail[0][2] = 3;
$detail[0][3] = 4;
$detail[1][0] = -1;
$detail[1][1] = -2;
$detail[1][2] = -3;
$detail[1][3] = -4;
for($i=0; $i<=$id; $i++)
if ($detail[$i][3] == 4)
{
$a = $detail[$i][0];
$a = $detail[$i][1];
$a = $detail[$i][2];
$a = $detail[$i][3];
}
else
{
$b = $detail[$i][0];
$b = $detail[$i][1];
$b = $detail[$i][2];
$b = $detail[$i][3];
}
}
This is the way I call the function.
$a = $b = null;
wow(1, $a, $b);
echo $a[0]." ".$a[1]." ".$a[2]." ".$a[3]." ".$a[4]." ".$b[0]." ".$b[1]." ".$b[2]." ".$b[3]." ".$b[4]." ";
I think you are missing how arrays work:
for($i=0; $i<=$id; $i++)
if ($detail[$i][3] == 4)
{
$a = $detail[$i][0];
$a = $detail[$i][1];
$a = $detail[$i][2];
$a = $detail[$i][3];
}
Each of these statemenets is overwriting the previous one.
Try something like this:
for($i=0; $i<=$id; $i++)
if ($detail[$i][3] == 4)
{
$a = $detail[$i];
}
That was you are copying the entire array inside the first one into $a.
Additionally, you set both $a and $b to null, yet only set one or the other in your function - you will always return at least a warning telling you about a null variable you are trying to echo.
What's a nicer way to do the following, that doesn't call f() twice?
$x = f() ? f() : 'default';
In PHP 5.3, you can also do:
$a = f() ?: 'default';
See the manual on ?: operator.
This seems to work fine:
$x = f() or $x = 'default';
function f()
{
// conditions
return $if_something ? $if_something : 'default';
}
$x = f();
$x = ($result = foo()) ? $result : 'default';
test
You could save it to a variable. Testcase:
function test() {
echo 'here';
return 1;
}
$t = test();
$x = $t ? $t : 0;
echo $x;