Can You Assign Multiple Variables At Once in PHP Like You Can With Java

Can you assign multiple variables at once in PHP like you can with Java?

Yes, you can.

$a = $b = $c = $d = array();

Assign same value to multiple variables at once?

$var_a = $var_b = $same_var = $var_d = $some_var = 'A';

Are multiple variable assignments done by value or reference?

With raw types this is a copy.

test.php

$a = $b = 0;

$b = 3;

var_dump($a);
var_dump($b);

Output:

int(0) 
int(3)

With objects though, that is another story (PHP 5)

test.php

class Obj
{
public $_name;
}

$a = $b = new Obj();

$b->_name = 'steve';

var_dump($a);
var_dump($b);

Output

object(Obj)#1 (1) { ["_name"]=> string(5) "steve" } 
object(Obj)#1 (1) { ["_name"]=> string(5) "steve" }

Would like to assign multiple variables from split() on one line of code

It may look like a simple answer (that you may have already considered), but well here it goes: using a single local var to hold the split() result, then refer to it in fields' assignments ->

Simple answer:

{
local ports = ['tcp-1514', 'tcp-8080', 'tcp-8443'],

ports: [
local name_split = std.split(name, '-');
{

name: name,
protocol: name_split[0],
containerPort: name_split[1],
}
for name in ports
],
}

Obfuscated answer (no interim local w/split() result):

// Return a map from zipping arr0 (keys) and arr1 (values)
local zipArrays(arr0, arr1) = std.foldl(
// Merge each (per-field) object into a single obj
function(x, y) x + y,
// create per-field object, e.g. { name: <name> },
std.mapWithIndex(function(i, x) { [arr0[i]]: x }, arr1),
{},
);

{
local ports = ['tcp-1514', 'tcp-8080', 'tcp-8443'],
// Carefully ordered set of fields to "match" against: [name] + std.split(...)
local vars = ['name', 'protocol', 'containerPort'],

ports: [
zipArrays(vars, [name] + std.split(name, '-'))
for name in ports
],
}

How can i write an array that holds multiple values for each product

you can handle this in php (server side) which is more common, and also you can handle in client side with java-script which i do'nt recommand .
you can use key-value arrays in php, for example you can easily do as bellow:

$basket[0]['productName']='Movies1';
$basket[0]['qty']=1;
$basket[0]['price']=15;
$basket[1]['productName']='Movies2';
$basket[1]['qty']=2;
$basket[1]['price']=30;

in this way you will have an array which can hold all off required information about your products.

Parallel assignment in Java?

Not really. You can do x = y = 0 to set several variables, but not a parallel assignment like Python.

Php for loop with 2 variables?

Try this:

for ($i=0, $k=10; $i<=10 ; $i++, $k--) {
echo "Var " . $i . " is " . $k . "<br>";
}

The two variables $i and $k are initialized with 0 and 10 respectively. At the end of each each loop $i will be incremented by one ($i++) and $k decremented by one ($k--). So $i will have the values 0, 1, …, 10 and $k the values 10, 9, …, 0.

Is it possible to create multiple variables with the same name?

In a given scope, you can only have one variable with a given name. A for loop has its own scope and only has one variable named wordView. Each time the loop iterates, it creates a new instance of the TextView class and assigns a reference to the variable wordView.

It is important to understand the difference between reference variables and class instances (or objects). You only have one variable named wordView here. You create 10 instances of TextView.



Related Topics



Leave a reply



Submit