RuleBritannia Posted January 2, 2014 Share Posted January 2, 2014 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. Quote Link to comment Share on other sites More sharing options...
gristoi Posted January 2, 2014 Share Posted January 2, 2014 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 Quote Link to comment Share on other sites More sharing options...
RuleBritannia Posted January 2, 2014 Author Share Posted January 2, 2014 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? Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted January 2, 2014 Solution Share Posted January 2, 2014 It's use isn't that uncommon, for example while ($row = $result->fetch_row()) { or if (($pos = strpos($x, $y, 0)) !== false) { Quote Link to comment Share on other sites More sharing options...
RuleBritannia Posted January 2, 2014 Author Share Posted January 2, 2014 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 Quote Link to comment Share on other sites More sharing options...
ignace Posted January 2, 2014 Share Posted January 2, 2014 (edited) 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 January 2, 2014 by ignace Quote Link to comment Share on other sites More sharing options...
RuleBritannia Posted January 2, 2014 Author Share Posted January 2, 2014 (edited) 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 January 2, 2014 by RuleBritannia Quote Link to comment Share on other sites More sharing options...
PaperTiger Posted January 2, 2014 Share Posted January 2, 2014 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. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted January 2, 2014 Share Posted January 2, 2014 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.