Jump to content

Echoing a variable which isn't set until later in the file?


hatrickpatrick

Recommended Posts

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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>
<?
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.