1

I have three-rows code:

echo '<div>'; for ($i=1; $i<=4; $i++) echo $i; echo '</div>'; 

Is there any syntax or function let me rewrite above code in only ONE row, for example:

echo '<div>'.(for...).'</div>'; //error 

Thanks

2
  • 2
    you want to make it harder to read and maintain - why? Commented Mar 17, 2015 at 1:06
  • In some case, it's not harder to read and maintain, I think. It's clear to see. Commented Mar 17, 2015 at 1:12

2 Answers 2

2

While readability is completely personal opinion, there are "common" methods, guidelines, and coding standards which are used by a lot of developers.

And while you don't need to adhere to them, it is always up to you or the company you work for, some of these "ways" are just common sense regardless of your personal choice.

Even if there was a way to do it, why would you want to?
How far do you want to go mixing things together for supposed readability?

echo and a loop are completely different things, and should remain separate.

Answer

In answer to your question, no, it is not possible because PHP needs to differentiate between different "things" - functions, echo statements, variable declaration and usage, etc.
The only things you can "mix" are things which PHP allows to be together, like having a variable in a function parenthesis (function($someVar)).

The only thing you could do (and again this has no point, but your choice) is to put it all on one line, like so:

echo '<div>'; for ($i=1; $i<=4; $i++) { echo $i; } echo '</div>'; 

But that looks terrible.
I want to quickly see different things - "ah an echo, it prints out XYZ", then I want to see a loop separately so I can see easily what the loop is doing.

Sign up to request clarification or add additional context in comments.

Comments

2

Yes its possible, no you dont want to do it:

echo '<div>' . implode('', array_keys(array_fill(1,4,0))) . '</div>'; 

2 Comments

0_o - Did you even ponder for a second and consider the poor world before you clicked "submit"?
Thanks Steve these types of answers are great for code golf

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.