PHP short sentence of stating new vairable - php

I wonder if there is any way can short sentence to state a variable.
Purpose: only for if you are in a situation have to state 20 variables at the time. more convenience
$a = 1; $b = 2;
//Imagination like below
$a, $b = 1, 2;
$a = 1, $b = 2;
Thank you very much for your advice of alternatives.
(If you do not have any ideas, please do not accuse the way of why have to think about this), because arrray, (object) are alternatives, but not match what I need on my question

The closest you can get to that syntax is using list():
<?php
list ($a, $b) = array(1, 2);
echo $a . ' ' . $b; // prints "1 2"
Its magic!
EDIT:
For even more magic, you can use short array notation from PHP 5.4 onwards:
<?php
list($a, $b) = [1, 2];
print $a . ', ' . $b; // prints 1, 2

How is $a = 1, $b = 2; shorter than $a = 1; $b = 2; ?
If for any reason you need two assignments in one statement, you could to it with list:
list($a,$b) = array(1,2);
However, shortening is not a valid reason for this.

Related

How to swap the values of two variable without using third variable

How to swap the values of two variable without using third variable. I tried doing it with array but it doesn't work. Does anyone have a quick solution
You can try like this
This will work for any variable type
$a = 10;
$b = 15;
list($a, $b) = array($b, $a);
print $a . ',' . $b;
This below method will work only for numbers not for strings
$a = $a + $b; // 10 + 15 = 25
$b = $a - $b; // 25 - 15 = 10
$a = $a - $b; // 25 - 10 = 15
print $a . ',' . $b;

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;
?>`

php syntax $a = $b = 1;

Is
$a = 1;
$b = $a;
equal to writing this?
$a = $b = 1;
Will the second example always put 1 as value to both $a and $b, even if $a and $b already has a value assigned to them?
Quoting the documentation:
The value of an assignment expression is the value assigned. That is,
the value of "$a = 3" is 3. This allows you to do some tricky things:
<?php
$a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.
?>
So, to answer your question, the result of the assignment $b = 1 is 1, and therefore, $a = $b = 1 would assign the value of $b = 1--which is to say 1--to $a.
That being said, abusing this can lead to code that is hard to read.
Yes, PHP will put 1 in $b then put $b value in $a, i.e. 1.
There is no ambiguity as the first assignment is $b = 1, the next is $a = $b.

php variable $a contains variable $b in it update $b and $a should automatically update

I have two variables in PHP, say $a and $b. $a is a string variable. It contains $b. I want to update $a automatically if $b is updated.
$b = 4;
$a = "value is ".$b;
echo $a; // value is 4
$b = 5;
echo $a; // should print value is 5
Yes, $a can be updated automatically if you assign $b to $a by reference, but there should not be any string concatenation assigned to $a.
Try:
$b = 4;
$a = &$b;
$c = 'Value is ';
echo $c.$a;
$b = 5;
echo $c.$a;
Here is a demo
Not possible the way you want it. You see, variables can be passed by reference, like so:
$a = &$b;
Which will cause $a to automatically update when $b changes, however, it may not contain any other value, (like the string you want), so you'll have to use a function or another variable to do it.
$b = &$a;
echo "Value is $b";
or
$b = &$a;
$description = "Value is ";
echo $description . $b;
PHP doesn't have that feature. Related features you could use are:
References, which let you alias one variable to another. The value of each variable is the same, since they're simply symbol table aliases.
$b = "I'm b."
$a =& $b;
echo $a;
Variable variables, in which one variable holds the name of the other.
$b = "I'm b."
$a = 'b';
echo $$a;
However, variable variables should generally be avoided as they generally cause needless obfuscation.
Functions (as mithunsatheesh suggests). This is closest to what you want, as a function call is an expression that will have the value you're looking for. The only place a function wouldn't work where a variable would is when interpolating the value into a double-quoted string or a heredoc. Instead, you'd have to use string concatenation, or assign the result of the function call to a local variable and interpolate that.
You should pass it by reference. How to do it ?
Make a function:
function showValue(&$b)
{
return 'value is ' . $b;
}
echo showValue($b);
I think this should work.
Take a look at http://www.php.net/manual/en/language.references.whatdo.php
$a = 4;
$b =& $a;
$a = 5;
echo $b; // should print 5;
When a php script runs it runs "line after line". When you assign like this
$b = 4;
$a = "value is ".$b;
Value of $b is already assigned to $a as a integer 4 (not $b). So, if next $b is updated to some other value. Variable $a has no idea about it.
In this kind of case you have to use function or variable reference as describe in some other answers
$a = 4;
$b =& $a;
$a = 5;
echo $b;

min function that ignores negative values in php

I have three numbers:
$a = 1
$b = 5
$c = 8
I want to find the minimum and I used the PHP min function for that. In this case it will give 1.
But if there is a negative number, like
$a = - 4
$b = 3
$c = 9
now the PHP min() should give $b and not $a, as I just want to compare positive values, which are $b and $c. I want to ignore the negative number.
Is there any way in PHP? I thought I will check if the value is negative or positive and then use min, but I couldn't think of a way how I can pass only the positive values to min().
Or should I just see if it's negative and then make it 0, then do something else?
You should simply filter our the negative values first.
This is done easily if you have all of them in an array, e.g.
$a = - 4;
$b = 3;
$c = 9;
$values = array($a, $b, $c);
$values = array_filter($values, function($v) { return $v >= 0; });
$min = min($values);
print_r($min);
The above example uses an anonymous function so it only works in PHP >= 5.3, but you can do the same in earlier versions with
$values = array_filter($values, create_function('$v', 'return $v >= 0;'));
See it in action.
$INF=0x7FFFFFFF;
min($a>0?$a:$INF,$b>0?$b:$INF,$c>0?$c:$INF)
or
min(array_filter(array($a,$b,$c),function($x){
return $x>0;
}));
http://codepad.org/DVgMs7JF
<?
$test = array(-1, 2, 3);
function removeNegative($var)
{
if ($var > 0)
return $var;
}
$test2 = array_filter($test, "removeNegative");
var_dump(min($test2));
?>

Categories