hatrickpatrick Posted August 16, 2007 Share Posted August 16, 2007 I could have sworn this was possible somehow, but it isn't working for me... Let's say I have a line at the top of my file, which is echo "$links". Then, the variable $links is set further down in the file, depending on some SQL results and GET variables. For me, even though I'm setting $links in the file, it will only echo if it's set before it's echoed. It make sense, but I have a definete memory that I once had a way around that problem - anyone know how to do it? echo "$links" if (isset($board)) { $links="Return to <A HREF=boards.php>Board list</a>"; } if (isset($thread)) { $links="Return to <A HREF=threads.php>Thread list</a>"; } ^ That isn't my code, but an example of what I'm trying to do... Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/ Share on other sites More sharing options...
Psycho Posted August 16, 2007 Share Posted August 16, 2007 Why would you want to echo something before it is set. Good code will always be in control of what is happening. Just bad programming practice in my opioning. The problem might be due to a server setting. I'm not real familiar with them, but I'm sure there is one that will cause an error in such a situation. The one I am familar with is "Option Explicit" in VBScript. With it turned off you can use a variable you have not yet set, but not with it on. This is to ensure your code is properly written. Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-325918 Share on other sites More sharing options...
lemmin Posted August 16, 2007 Share Posted August 16, 2007 if (isset($links)) { echo "$links"; } if (isset($board)) { $links="Return to <A HREF=boards.php>Board list</a>"; } if (isset($thread)) { $links="Return to <A HREF=threads.php>Thread list</a>"; } Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-325939 Share on other sites More sharing options...
hatrickpatrick Posted August 16, 2007 Author Share Posted August 16, 2007 Didn't work, just tried it And the reason I want to do this is because my links() function is called at the top of every page, and I want to be able to change which links are displayed depending on whether certain variables are set later or not... Is there any other way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-325951 Share on other sites More sharing options...
lemmin Posted August 16, 2007 Share Posted August 16, 2007 There must be a problem with your code, then. What I added to your code will make it echo the $links variable if it isn't null and do nothnig if it is. If it isn't echoing, then your $links variable is null. Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-325959 Share on other sites More sharing options...
hatrickpatrick Posted August 16, 2007 Author Share Posted August 16, 2007 It must be the server, then, because I just tested that code by putting it into a new PHP file and trying it with board=1, and still nothing... My server allows me to change the php.ini settings myself, but have you any idea which setting I need to change? Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-325966 Share on other sites More sharing options...
d22552000 Posted August 16, 2007 Share Posted August 16, 2007 put script buffering to ON then it will process the entire file BEFORE handing out the variables. IE... it will read your ffile... set the variable... then read the file again to output the variables. Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-325969 Share on other sites More sharing options...
Daniel0 Posted August 16, 2007 Share Posted August 16, 2007 put script buffering to ON then it will process the entire file BEFORE handing out the variables. IE... it will read your ffile... set the variable... then read the file again to output the variables. I never heard of a such thing... were would you do that? I don't see it in php.ini Don't attempt to do so though, it would be bad programming practice. Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-325975 Share on other sites More sharing options...
hatrickpatrick Posted August 16, 2007 Author Share Posted August 16, 2007 ^ How would I turn on this "script buffering"? And if I can't, how can I actually get this to work? Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-325982 Share on other sites More sharing options...
Daniel0 Posted August 16, 2007 Share Posted August 16, 2007 Don't do so (if it's even possible). Initialize the variables BEFORE using them. Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-325985 Share on other sites More sharing options...
dbo Posted August 16, 2007 Share Posted August 16, 2007 What exactly are you trying to accomplish here? Outing a variable that's not defined will obviously generate an error and output nothing. At best you get rid of the error message and it still outputs nothing b/c there variable has no contents. I'm sure there is an alternative solution for what you're trying to accomplish. Can you explain what it is you want to do? Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-325987 Share on other sites More sharing options...
hatrickpatrick Posted August 16, 2007 Author Share Posted August 16, 2007 I can't, though The if ($board=whatever) statement also displays the entire board list, and I want it to send the links header before anything else If I echo it after setting it, it means I have to echo it inside every single if() statment, and there are a lot of them in this file... Is there any other way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-325988 Share on other sites More sharing options...
dbo Posted August 16, 2007 Share Posted August 16, 2007 Can you show us some of the real code? Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-325990 Share on other sites More sharing options...
hatrickpatrick Posted August 16, 2007 Author Share Posted August 16, 2007 echo "<div id=bar class=lite>Return to: <A HREF=cpanel.php class=menu>Control Panel</a> $exlink</div>"; if ($editpro) { $exlink="| <A HREF=editbrands.php>Brand Editor</a>"; $sqledp="SELECT * FROM processors WHERE pro_id=$editpro"; $resultedp=mysql_query($sqledp); $rowedp=mysql_fetch_assoc($resultedp); ?> <form action="editbrands.php?brand=<?=$brand ?>&pro=<?=$editpro ?>" METHOD=post> Product Name: <input type=text name=edpname><br> Product Type: <input type=text name=edptype><br> <input type=submit name=edprosub value="Add New Product"></form></td></tr> <? } I know the code is messy, but this is only a first draft of the file... but basically, I need that bar at the top, I want the links displayed in it to depend on the variables below it, and I don't want to have to echo the bar in every if() statement, and the bar has to be echoed before any of the other elements are... Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-325995 Share on other sites More sharing options...
dbo Posted August 16, 2007 Share Posted August 16, 2007 So why can't you move your if statement and stuff above that so it's set before you use it? Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-325997 Share on other sites More sharing options...
hatrickpatrick Posted August 16, 2007 Author Share Posted August 16, 2007 Because, take the example I just gave you, I want the links bar to be ABOVE all that, not below it... It's supposed to be the first thing in the file, the links bar... Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-326007 Share on other sites More sharing options...
dbo Posted August 16, 2007 Share Posted August 16, 2007 echo "<div id=bar class=lite>Return to: <A HREF=cpanel.php class=menu>Control Panel</a> $exlink</div>"; if ($editpro) { $exlink="| <A HREF=editbrands.php>Brand Editor</a>"; $sqledp="SELECT * FROM processors WHERE pro_id=$editpro"; $resultedp=mysql_query($sqledp); $rowedp=mysql_fetch_assoc($resultedp); ?> <form action="editbrands.php?brand=<?=$brand ?>&pro=<?=$editpro ?>" METHOD=post> Product Name: <input type=text name=edpname><br> Product Type: <input type=text name=edptype><br> <input type=submit name=edprosub value="Add New Product"></form></td></tr> <? } I know the code is messy, but this is only a first draft of the file... but basically, I need that bar at the top, I want the links displayed in it to depend on the variables below it, and I don't want to have to echo the bar in every if() statement, and the bar has to be echoed before any of the other elements are... I guess I'm blind but I don't see why this won't work. if ($editpro) { $exlink="| <A HREF=editbrands.php>Brand Editor</a>"; echo "<div id=bar class=lite>Return to: <A HREF=cpanel.php class=menu>Control Panel</a> $exlink</div>"; $sqledp="SELECT * FROM processors WHERE pro_id=$editpro"; $resultedp=mysql_query($sqledp); $rowedp=mysql_fetch_assoc($resultedp); ?> <form action="editbrands.php?brand=<?=$brand ?>&pro=<?=$editpro ?>" METHOD=post> Product Name: <input type=text name=edpname><br> Product Type: <input type=text name=edptype><br> <input type=submit name=edprosub value="Add New Product"></form></td></tr> <? } Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-326012 Share on other sites More sharing options...
dbo Posted August 16, 2007 Share Posted August 16, 2007 It will output in the exact same position as it was before (given the if condition is met) the only difference is the variable now has data.... Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-326013 Share on other sites More sharing options...
hatrickpatrick Posted August 16, 2007 Author Share Posted August 16, 2007 Yeah I know I could do that, but the problem is I have about 6 if statements and I wanted to output the links bar before them all... Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-326029 Share on other sites More sharing options...
lemmin Posted August 16, 2007 Share Posted August 16, 2007 echo "<div id=bar class=lite>Return to: <A HREF=cpanel.php class=menu>Control Panel</a>"; if (isset($exlink)) echo $exlink; echo "</div>"; That should work, whatever the case is. Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-326036 Share on other sites More sharing options...
Psycho Posted August 16, 2007 Share Posted August 16, 2007 What does the position of content on your page have to do with the order in which code is processed in your script? You should be able to separate the two completely. I think your approach to the problem just needs to be modified. This is not a PHP problem per se. Have all of your condition statments at the top of the page to set any variables and to grab any data. Then at the bottom of the page acutally build the HTML content. Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-326074 Share on other sites More sharing options...
dbo Posted August 16, 2007 Share Posted August 16, 2007 I concur. Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-326077 Share on other sites More sharing options...
lemmin Posted August 16, 2007 Share Posted August 16, 2007 It is because he is using if statements for the content to be in its specified locations. It is definately not the best way to output the data, but there is no reason that it shouldn't be possible. Quote Link to comment https://forums.phpfreaks.com/topic/65266-echoing-a-variable-which-isnt-set-until-later-in-the-file/#findComment-326084 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.