declaring multiple variables with includes as well?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Darkzero
Forum Newbie
Posts: 18
Joined: Thu Oct 25, 2007 8:48 pm

declaring multiple variables with includes as well?

Post by Darkzero »

Im kinda confused about this simple thing, a little help would save me alot of time. Lets say i have a code like this:

Code: Select all

<?php
$con = mysql_connect("localhost","user","pass")or die(mysql_error())
$ip = $_SERVER['REMOTE_ADDR']
(some random code having to do with $ip)
mysql_close($con)
?>
Then some random html, and so on. But then, in the same document,

Code: Select all

<?php
$con = mysql_connect("localhost","user","pass")or die(mysql_error())
$ip = mysql_fetch(blahblahblah)
(some random code having to do with $ip)
mysql_close($con)
?>

<p>and now ima stick an include that has the variable $ip declared as a seperate thing</p>
<? include('ip.php'); ?>

Do the variables cancel between every "mysql_close()"?

And, if I never close the connection between closing tags (<?php ?>) & html, will i have to re-open the connection at the next set of php tags?
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

Post by ska »

For efficiency, I tend to open the connection at the start of my application and then close it at the end.

i.e.

Code: Select all

dbConnect();
// all my code here, SQL queries etc.
mysql_close();
That way I'm not opening and closing connections the whole time. To answer your specific queries,
Do the variables cancel between every "mysql_close()"?
If you mean, will you lose whatever is stored in $ip if you do a mysql_close() on the next line, then no, the value of $ip will still be there. If you declare the value of $ip somewhere else, say in your include file, then yes, the value of $ip is replaced.
And, if I never close the connection between closing tags (<?php ?>) & html, will i have to re-open the connection at the next set of php tags?
No, the connection will still be open. As I said, best to open your connection at the start of your document and then close at the end. ANd do make sure you close your connections eventually. I think PHP is more forgiving than some other languages, ASP for example, but too many connections left open are considered a bad thing.

Incidentally, you mention putting HTML in between your PHP. Just a general pointer on this - you're best to keep your HTML and PHP seperate if you can otherwise code becomes hard to read. Try coming back to a document with bits of PHP and HTML all mixed up a few months after you wrote it and see if you can still work out what's going on. ;) Templating systems like SMARTY are great for this.
Darkzero
Forum Newbie
Posts: 18
Joined: Thu Oct 25, 2007 8:48 pm

Post by Darkzero »

Thanks, I appreciate the help. :)
Post Reply