<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Handling of global variables</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="Manual do PHP" HREF="index.html"><LINK REL="UP" TITLE="Migrating from PHP 3 to PHP 4" HREF="migration4.html"><LINK REL="PREVIOUS" TITLE="Cookies" HREF="migration4.cookies.html"><LINK REL="NEXT" TITLE="Migrando do PHP/FI 2 para o PHP 3" HREF="migration.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="section" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Manual do PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="migration4.cookies.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Apêndice D. Migrating from PHP 3 to PHP 4</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="migration.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="migration4.variables" >Handling of global variables</A ></H1 ><P > While handling of global variables had the focus on to be easy in PHP 3 and early versions of PHP 4, the focus has changed to be more secure. While in PHP 3 the following example worked fine, in PHP 4 it has to be unset(<TT CLASS="literal" >unset($GLOBALS["id"])</TT >);. This is only one issue of global variable handling. You should always have used <CODE CLASS="varname" >$GLOBALS</CODE >, with newer versions of PHP 4 you are forced to do so in most cases. Read more on this subject in the <A HREF="language.references.spot.html#references.global" > <TT CLASS="literal" >global</TT > references section</A >. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN276358" ></A ><P ><B >Exemplo D-1. Migration of global variables</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$id </font><font color="#007700">= </font><font color="#0000BB">1</font><font color="#007700">;<br />function </font><font color="#0000BB">test</font><font color="#007700">()<br />{<br /> global </font><font color="#0000BB">$id</font><font color="#007700">;<br /> unset(</font><font color="#0000BB">$id</font><font color="#007700">);<br />}<br /></font><font color="#0000BB">test</font><font color="#007700">();<br />echo(</font><font color="#0000BB">$id</font><font color="#007700">); </font><font color="#FF8000">// This will print out 1 in PHP 4<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="migration4.cookies.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Principal</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="migration.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Cookies</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="migration4.html" ACCESSKEY="U" >Acima</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Migrando do PHP/FI 2 para o PHP 3</TD ></TR ></TABLE ></DIV ></BODY ></HTML >