Fallthrough variable assignment in PHP? [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Coalesce function for PHP?
I'm not sure what this is normally called, but I hope the title communicates well enough. What I have is a handful of variables some of which might be null.
I want to do:
$a = $b || $c || $d;
Where $a ends up being = to the first non-null variable.

To my knowledge, PHP doesn't support this in the same way JavaScript does.
You can, however do something like this:
$a = $b ? $b : ($c ? $c : $d);
A more general solution:
function fallthrough($arr) {
//$arr should be an array of possible values. The first non-null value is returned
do $a = array_shift($arr);
while($a === null && $arr);
return $a;
}

<?php
$a = 0;
$b = false;
$c = true; //should become this
$d = '1';
$e = $a ?: $b ?: $c ?: $d;
var_dump($e);
//bool(true)
//should be '1' if order is different
$e = $a ?: $b ?: $d ?: $c;
var_dump($e);
//string(1) "1"
... however ?: is kinda new, you will confuse your colleagues / fellow coders.

I don't think that's possible. I think you'd have to use some other, more laborious, way. I.e. make an array of the variables, iterate through it until you find a non-null value and break the loop, like so:
$vars = array("b" => $b, "c" => $c, "d" => $d);
foreach($vars as $var) {
if($var != null) {
$a = $var;
break;
}
}
Well, like some other answers here say, you can use the shorthand way of writing this, but writing readable code is important too. The above code is pretty readable.

Related

How to avoid undefined index warning for chains of 'default' values?

Let's say I want to check for a couple of different parameters, and then fall back to a default value, is there a way to do it without ugly and verbose writing of isset()?
For example in JS we can do:
var someVariable = otherVar || anotherVar || 'fallback here';
The equivalent in PHP would be something like:
$someVariable = (isset($otherVar) ? $otherVar : (isset($anotherVar) ? $anotherVar : 'fallback here'));
which is obviously a mess and horrible to read.
Lots of solutions exist for single fallbacks, i.e.:
$someVariable = $otherVar ?: 'fallback here';
but that doesn't help me with requiring more than one in the line of checks.
Given that I am only interested in whether or not the value is set or truth-y (i.e. I am happy for 1 to be accepted as the used value, and for 0/false/null to be skipped and for the next parameter in the chain to be used), what is the best way to avoid the undefined index warning?
In reality, I would be doing this on arrays in most cases, but not all, and it's probably that they will be different arrays. I may want to use $_POST for the first, then check $_GET under a different key, and then fall back to a default string for example.
Ignoring PHP warnings and notices is not a good idea at all.
But just for the experiment, I can suggest you the error control operator #.
PHP supports one error control operator: the at sign (#). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.
$someVariable = #$otherVar ? #$var : 'fallback here';
echo $someVariable;
// output: fallback here
http://php.net/manual/en/language.operators.errorcontrol.php
You can chain ?: but you'll still need to suppress errors with #.
For example, the following will work similarly to your JavaScript example:
$foo = #$bar ?: #$baz ?: #$qux ?: "I give up";
It's the equivalent of multiple "single fallbacks":
$foo = (#$bar ?: (#$baz ?: (#$qux ?: "I give up")));
Be careful though, ?: only checks for truthyness. There are scenarios where isset() and truthyness do not agree. Additionally, ?: fallback functionality was introduced in PHP 5.3.
If you only care about nullness and have PHP 7 available, it introduced a null coalescing operator.
This is ambiguous and it is deprecated...
$something = $a ? $b : $c ?: $d;
So, you have 2 alternatives for grouping:
$something = $a ? $b : ($c ?: $d); // version 1
$something = ($a ? $b : $c) ?: $d; // version 2
Which one is correct? Let's test this...
$posibilities = [TRUE, FALSE, NULL, [], new stdClass()];
$v1_is_correct = TRUE;
$v2_is_correct = TRUE;
foreach ($posibilities as $a) {
foreach ($posibilities as $b) {
foreach ($posibilities as $c) {
foreach ($posibilities as $d) {
$original = (int)($a ? $b : $c ?: $d);
$v1 = (int)($a ? $b : ($c ?: $d));
$v2 = (int)(($a ? $b : $c) ?: $d);
$v1_is_correct = $v1_is_correct && ($original === $v1);
$v2_is_correct = $v2_is_correct && ($original === $v2);
print "ORIG: $original - V1: $v1 - V2: $v2 \n";
}
}
}
}
print "\n";
$v1_is_correct ? print "V1 is correct \n" : NULL;
$v2_is_correct ? print "V2 is correct \n" : NULL;
Result: V2 is correct.
$a ? $b : $c ?: $d
is equivalent to...
($a ? $b : $c) ?: $d

How to use a pointer in a PHP function? [duplicate]

This question already has answers here:
Are there pointers in php?
(9 answers)
Closed 7 years ago.
In C we can use pointer for a function parameter:
test( *text);
function test( *txt){
//codes goes here
}
Is it possible in PHP?
Variable names in PHP start with $ so $entryId is the name of a variable.
$this is a special variable in Object Oriented programming in PHP, which is reference to current object.
-> is used to access an object member (like properties or methods) in PHP, like the syntax in C++.
So your code means this: place the value of variable $entryId into the entryId field (or property) of this object.
The & operator in PHP, means pass reference. Here is an example:
$b=2;
$a=$b;
$a=3;
print $a;
print $b;
// output is 32
$b=2;
$a=&$b; // note the & operator
$a=3;
print $a;
print $b;
// output is 33
In the above code, because we used & operator, a reference to where $b is pointing is stored in $a. So $a is actually a reference to $b.
There is a good explanation of pointers on this page
There are references, but that's not the same as pointers.
php.net has multiple pages explaining What References Are, What References Do and What References Are Not.
There too, it is mentioned multiple times that
References are not pointers.
They provide a way to assign $a to $b so that if you reassign $b, $a changes as well:
$a = 'a';
$b = &$a; // Reference
$b = 'b'; // Now $a == 'b'
This can be used for function arguments as well:
function myFunc(&$b)
{
$b = 'b';
}
$a = 'a';
myFunc($a); // Now $a == 'b'
Before PHP 5.3.0 is was also possible to do a thing called "call-time pass-by-reference", where you would have a "normal" function declaration and use the & operator in the call instead:
function myFunc($b)
{
$b = 'b';
}
$a = 'a';
myFunc(&$a); // As of PHP 5.3.0 produces a Fatal Error, saying:
// Call-time pass-by-reference has been removed; If you would like to pass argument by reference, modify the declaration of myFunc().
But beware! Assigning another reference will not update the original reference:
$a = 'a';
$b = 'b';
$c = &$a;
$c = &$b; // $a == 'a'
[ Demo ]
A trap resulting from that exists with the global keyword.
$a = 'a';
$b = 'b';
function myFunc()
{
global $a, $b;
$a = &$b;
var_dump($a);
}
myFunc(); // 'b'
var_dump($a); // 'a'
That is because global $a effectively means $a = &$GLOBALS['a'], so assigning it a new reference will not update $GLOBALS.
Of course you can prevent that by using $GLOBALS directly.
But if you're using globals at all, you should probably rethink your design anyway.
With references, there is now also a difference between setting a variable = NULL and using unset().
= NULL follows references, unset() does not:
$a = 'a';
$b = &$a;
unset($b); // $a == 'a'
$a = 'a';
$b = &$a;
$b = NULL; // $a == NULL
[ Demo ]
Bottom line:
References allow for things that wouldn't be possible without them, but sometimes do not behave the way one would expect them to, because of how PHP was built.

PHP OR surprising behavior [duplicate]

This question already has answers here:
The behaviour of the or operator in PHP
(4 answers)
Closed 8 years ago.
I have some surprising results using OR as a logical OR in php.
Considering the following code:
$a = false;
$b = false;
$c = true;
# Test 1, using OR and assigning to a variable
$value = $a OR $b OR $c;
var_dump( $value );
# return bool(false) but why?
# Test 2, using OR directly in var_dump
var_dump( $a OR $b OR $c );
# return bool(true) as expected
# Test 3, using || and assigning to a variable
$value = $a || $b || $c;
var_dump( $value );
# return bool(true) as expected
# Test 4, using || directly in var_dump
var_dump( $a || $b || $c );
# return bool(true) as expected
Why Test 1 and Test 2 give different results even though they do the same logical operation?
The || operator and OR operator do not behave the same. They cannot be used interchangably.
If you want || behaviour, then use it. Do not use OR unless you're in a situation where || would do the wrong thing.
As for your situation, these two lines of code will behave exactly the same:
$value = $a OR $b OR $c;
($value = $a) OR $b OR $c;
In other words, your code is basically just:
$value = $a;
If you used the || operator, then these two are identical as if you had braces like this:
$value = $a || $b || $c;
$value = ($a || $b || $c);
For more details: http://php.net/manual/en/language.operators.precedence.php
If you wrap test 1 in parenthesis, it will behave as expected:
$value = ($a OR $b OR $c);
When you run var_dump on test 2, you get the expected result because var_dump is wrapping the operation in parenthesis.
It is usually a good idea to wrap an operation in parenthesis like this, especially with variable assignment.
Also, the "OR" keyword and "||" do not behave the same way. See documentation here:
http://php.net/manual/en/language.operators.logical.php

PHP assign first not-null value

I've got some three values like this:
$a = null
$b = 3;
$c = null
(I never know what will be null: maybe all of them, maybe none of them.)
Following so called lazy loading, I've tried to assign the first not-null value this way:
$d = $a or $b or $c;
(It is similar to JavaScript way var d = a || b; (it will assign b if there is no a).)
But in PHP it seems to not work.
Am I doing it wrong, or what is best and simplest way to do this?
You can use the short ternary operator in PHP 5.3+:
$d = $a ?: $b ?: $c;
Note that this does type coercion like in JS.
Update (PHP 7)
In PHP 7.0+ you would do (called null coalescing - more informations):
$d = $a ?? $b ?? $c;
Try this...
$d = array_filter(array($a, $b, $c))[0]; //newer PHP only
or this:
$d = current(array_filter(array($a, $b, $c))); //warning about references
or this:
$tmp = array_filter(array($a, $b, $c));
$d = current($tmp); //most safe
In PHP 7 you can use the "Null coalescing" operator:
$d = $a ?? $b ?? $c;
This will assign the first non-null value (or null if there isn't one), as the question asked.
Unlike some of the other answers it won't be tripped up by implicit type casting so, for example,
$b = 0; $c = 1;
$d = $a ?? $b ?? $c;
echo $d;
will output 0: It won't mind that $a hasn't been set at all and won't pass over $b even though it type-casts to false.
If you can put variables in an array this can help:
$d = current(array_filter(array($a, $b, $c)));
or this can be a apporach as well:
if(!empty($a)) {
$d = $a;
}
this check may conitnue for all the variables like $b and $c
You can try with:
$d = !is_null($a) ? $a : (!is_null($b) ? $b : $c);
in php you can do it like this:-
`<?php
$a = 1;
$b = null;
$c = null;
$d = $a ? $a:($b?$b:($c?$c:'null'));
echo $d;
?>`

Assign variable with the aid of boolean operators

Is there some short way to do this
if (!empty($b))
$a = $b;
else if (!empty($c)) {
$a = $c;
i know you could use ternary operator but its not what i asking like in JavaScript there is way to assign like this
my_var = some_Var || fu_bar || 0;
so if first dont exist it uses second and if second dont exist it uses third one.
is there similar thing in php?
cant think of other way than this:
$a = ! empty($b) ? $b : (! empty($c) ? $c : 0)
you can use one of the two ways whatever suits you. both of these functions will check either we have the non empty variable or not.
1: $a = (isset($b) && $b)?$b:$c;
2: if($b)
$a = $b;
elseif($c)
$a = $c;

Categories