Jump to content

Professional to do this?


Go to solution Solved by Barand,

Recommended Posts

I checked numerous reputable sources(php.net/stack overflow etc) couldn't find any answer in regards.

echo $var = 'this var was undeclared before this';

As you can see, I am echoing and setting a variable all at once.

 

It seems to work ok with no errors, but I do not remember reading this in the past in any php books, nor can I remember seeing it in any open source code.

Wondering if it is bad practice?

 

On another note,

echo $var[1] = 'this array and key|value was undeclared before this';

works too

 

Thanks in advance.

 

 

Link to comment
https://forums.phpfreaks.com/topic/285039-professional-to-do-this/
Share on other sites

it may work, but for readability it makes no sense. If you are going to asign the value to a variable then you must want to use it further on in the code. So assign first, then echo out the var. If your not using it anywhere else then it is pointless, just echo out the string

it may work, but for readability it makes no sense. If you are going to asign the value to a variable then you must want to use it further on in the code. So assign first, then echo out the var. If your not using it anywhere else then it is pointless, just echo out the string

by assigning first then echoing like you suggest, it will look like this

$var = 'this var was undeclared before this';
echo $var;
echo $var = 'this var was undeclared before this';

 seems to do the same,

so whats the point of taking another line of code?

 

It's use isn't that uncommon, for example

while ($row = $result->fetch_row()) {

or

if (($pos = strpos($x, $y, 0)) !== false) {

Yes your right, I didnt seem to notice it in this context.

With that being said, I think I will use it universally when I can.

 

Thanks

by assigning first then echoing like you suggest, it will look like this

$var = 'this var was undeclared before this';
echo $var;
echo $var = 'this var was undeclared before this';
 seems to do the same,

so whats the point of taking another line of code?

 

That's not what he said, he said to do this:

 

echo 'this var was undeclared before this';

If your not using it anywhere else then it is pointless, just echo out the string

Edited by ignace

That's not what he said, he said to do this:

 

echo 'this var was undeclared before this';

 

That is exactly what he said.

 

"If you are going to asign the value to a variable then you must want to use it further on in the code. So assign first, then echo out the var."

 

I wouldn't do what you said, as I need the variable.

Edited by RuleBritannia

by assigning first then echoing like you suggest, it will look like this

$var = 'this var was undeclared before this';
echo $var;
echo $var = 'this var was undeclared before this';

 seems to do the same,

so whats the point of taking another line of code?

 

Readability and making it easier to debug later on.

If you don't need to use what you're echoing later, just echo it directly.  If you do, assign it to a variable, then echo.  And doing both on one line is, like others have said, bad for readability and debugging.

 

Just because you can do something doesn't mean you should.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.