This is a discussion on when to use mysql_close and unset() within the PHP Language forums, part of the PHP Programming Forums category; Howdy all, I'm progressing with PHP but I have 2 questions that are kinda bugging me. Say for example, ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Howdy all,
I'm progressing with PHP but I have 2 questions that are kinda bugging me. Say for example, I have a php page similar to this.... if ..... if........ elseif...... else........ else .... etc. where I have if/else statements nested in other ones. Now I guess it's good practice to unset created variables within each if or else section as a) it's less work and b) secure (correct me if I'm wrong). IF I'm using mysql_close, do I just need to open one connection at the top of the page and close it at the bottom, as I think I understand that php will only run queries within if/else which evaluate to true (and so all can use one connection), and then I can close the connection at the bottom of the page as php will go through all the code? I hope that made sense :o) |
|
|||
|
"ahevans" <ahevans@gmail.com> wrote in message
news:d589c3f.0502190717.69ea1823@posting.google.co m... > Now I guess it's good practice to unset created variables within each > if or else section as a) it's less work and b) secure (correct me if > I'm wrong). Never heard of that one, although I can see how some people would do it to simulate the scoping rule in C++. |
|
|||
|
"Chung Leong" <chernyshevsky@hotmail.com> wrote in message news:<gKKdnX7sDIFJqYXfRVn-2A@comcast.com>...
> "ahevans" <ahevans@gmail.com> wrote in message > news:d589c3f.0502190717.69ea1823@posting.google.co m... > > Now I guess it's good practice to unset created variables within each > > if or else section as a) it's less work and b) secure (correct me if > > I'm wrong). > > Never heard of that one, although I can see how some people would do it to > simulate the scoping rule in C++. Is it common practice to unset all variables or are they unset when a browser leaves a page? |
|
|||
|
ahevans wrote:
> "Chung Leong" <chernyshevsky@hotmail.com> wrote in message news:<gKKdnX7sDIFJqYXfRVn-2A@comcast.com>... > >>"ahevans" <ahevans@gmail.com> wrote in message >>news:d589c3f.0502190717.69ea1823@posting.google. com... >> >>>Now I guess it's good practice to unset created variables within each >>>if or else section as a) it's less work and b) secure (correct me if >>>I'm wrong). >> >>Never heard of that one, although I can see how some people would do it to >>simulate the scoping rule in C++. > > > Is it common practice to unset all variables or are they unset when a > browser leaves a page? In PHP, everything ends when the script has completed. So by the time the page has finished rendering in the user's browser, all variables are already unset. (Apart from session variables which may have been saved in a file on the server somewhere.) JP -- Sorry, <devnull@cauce.org> is a spam trap. Real e-mail address unavailable. 5000+ spams per month. |
|
|||
|
.oO(ahevans)
>Is it common practice to unset all variables I don't think so. >or are they unset when a >browser leaves a page? PHP cleans up when the script is finished. The only things I release manually are resources (DB connections etc.), which can be easily done in destructors when using OOP. Micha |
|
|||
|
> IF I'm using mysql_close, do I just need to open one connection at
the > top of the page and close it at the bottom Use only one connection. Connect at the top (before your if statements) and close at the bottom (after all the if statements). You should always do it this way while you are a beginner to "scope issues". > as I think I understand > that php will only run queries within if/else which evaluate to true Becareful here. //example 1 if(true) echo "Hello"; elseif(true) echo " Bob"; else echo " Peterson"; //example 2 if(false) echo "Well! "; elseif(true) echo "Hello"; elseif(true) echo " Bob"; else echo " Peterson"; Both examples will only print "Hello" not "Hello Bob" or "Hello Bob Peterson". Any elseif/else statement below another true statement will not execute. //example 3 if(false) echo "Hello"; elseif(true) { echo "Bob"; if(true) echo " Peterson"; elseif(false) echo " Smith"; else echo " Lesterson"; } This will print "Bob Peterson". > Now I guess it's good practice to unset created variables within each > if or else section as a) it's less work and b) secure (correct me if > I'm wrong). Never heard this either, but I am more curious about (a). How is it less work to unset variables? |