[PHP] Different variable names, same value

Status
Not open for further replies.

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
I know this isn't the most interesting tip ever, but it saves lines in your code.

If you've ever wanted to set different variables to the same value as each other, you can do it in one line instead of using multiple lines.

Instead of doing:
PHP:
<?php
$var1 = "mark";
$var2 = "mark";
$var3 = "mark";
?>

You can just easily do:
PHP:
<?php
$var1 = $var2 = $var3 = "mark";
?>

Like I said, it's not the biggest help ever, but it saves line in your code.

To test speed in processing time, I wrote this code:
PHP:
<?php
$start = microtime(true);
 
$var1 = $var2 = $var3 = "mark";
 
echo microtime(true) - $start;
?>

There isn't a huge difference in time, but it does save time when you actually write the code.

Anyway, hope it somehow helps you.
- Mark
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,934
3,933
Cool. :up:

Never knew you could do that, and to be honest, it looks really weird!
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
Cool. :up:

Never knew you could do that, and to be honest, it looks really weird!
No problem. And yeah, it does look quite strange.

Like I said, it doesn't save much processing time, but it saves writing time so I'mma start using this way to make multiple variables have the same value from now on.
 

brsy

nah mang
May 12, 2011
1,530
272
It does look weird, but why not simply use one variable with one value, instead of numerous variables set to the same value?
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
It does look weird, but why not simply use one variable with one value, instead of numerous variables set to the same value?
You might use to whole '$var1 = $var2 = $var3 = "mark";' thing for setting default values at the start of a script, the values of $var2 and $var3, or even $var1 may need to be changed throughout the script.
 
Status
Not open for further replies.

Users who are viewing this thread

Top