Connecting Tech Pros Worldwide Forums | Help | Site Map

when to use mysql_close and unset()

ahevans
Guest
 
Posts: n/a
#1: Jul 17 '05
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)

Chung Leong
Guest
 
Posts: n/a
#2: Jul 17 '05

re: when to use mysql_close and unset()


"ahevans" <ahevans@gmail.com> wrote in message
news:d589c3f.0502190717.69ea1823@posting.google.co m...[color=blue]
> 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).[/color]

Never heard of that one, although I can see how some people would do it to
simulate the scoping rule in C++.


ahevans
Guest
 
Posts: n/a
#3: Jul 17 '05

re: when to use mysql_close and unset()


"Chung Leong" <chernyshevsky@hotmail.com> wrote in message news:<gKKdnX7sDIFJqYXfRVn-2A@comcast.com>...[color=blue]
> "ahevans" <ahevans@gmail.com> wrote in message
> news:d589c3f.0502190717.69ea1823@posting.google.co m...[color=green]
> > 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).[/color]
>
> Never heard of that one, although I can see how some people would do it to
> simulate the scoping rule in C++.[/color]

Is it common practice to unset all variables or are they unset when a
browser leaves a page?
Jan Pieter Kunst
Guest
 
Posts: n/a
#4: Jul 17 '05

re: when to use mysql_close and unset()


ahevans wrote:[color=blue]
> "Chung Leong" <chernyshevsky@hotmail.com> wrote in message news:<gKKdnX7sDIFJqYXfRVn-2A@comcast.com>...
>[color=green]
>>"ahevans" <ahevans@gmail.com> wrote in message
>>news:d589c3f.0502190717.69ea1823@posting.google. com...
>>[color=darkred]
>>>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).[/color]
>>
>>Never heard of that one, although I can see how some people would do it to
>>simulate the scoping rule in C++.[/color]
>
>
> Is it common practice to unset all variables or are they unset when a
> browser leaves a page?[/color]

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.
Michael Fesser
Guest
 
Posts: n/a
#5: Jul 17 '05

re: when to use mysql_close and unset()


.oO(ahevans)
[color=blue]
>Is it common practice to unset all variables[/color]

I don't think so.
[color=blue]
>or are they unset when a
>browser leaves a page?[/color]

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
gildir
Guest
 
Posts: n/a
#6: Jul 17 '05

re: when to use mysql_close and unset()


> IF I'm using mysql_close, do I just need to open one connection at
the[color=blue]
> top of the page and close it at the bottom[/color]

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".
[color=blue]
> as I think I understand
> that php will only run queries within if/else which evaluate to true[/color]

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".
[color=blue]
> 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).[/color]

Never heard this either, but I am more curious about (a). How is it
less work to unset variables?

Closed Thread