Path: csiph.com!usenet.pasdenom.info!aioe.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "J.O. Aho" Newsgroups: comp.lang.php Subject: Re: Setting & displaying Variables Date: Sat, 21 Sep 2013 23:55:33 +0200 Lines: 67 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net uMOEBnWxDqHkqlWLLMOfFgUB5pXx8BsjeXhTRhgFfp9bVsnQ5f Cancel-Lock: sha1:Mbkpzc+53pQcfHVaglxvG45wFXs= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130808 Thunderbird/17.0.8 In-Reply-To: Xref: csiph.com comp.lang.php:10827 On 21/09/13 22:20, Twayne wrote: > Hi, > > Obviously, the issue is around the comments var. It works, but it > doesn't and I'm hoping someone can explain why. > > Both echo strlen($comments); and echo $comments; throw a Notice > about an undefined variable. But the variable "comments" HAS to have > been defined in order for the two echo statements to work! > The variable 'comments' is also in SESSION data, which will also get > the same Notice of an undefined variable. > > What the heck am I missing? Why do I get the undefined var in those two > statements? > > =================== > > echo strlen($_POST["comments"])."
"; //WORKS > > $commments = $_POST["comments"]; // WORKS but not needed; done elsewhere > too. Assuming this is just a typo and it was supposed to be $comment > // echo strlen($comments); // GETS Notice: Undefined variable: comments > in C:\xampp\htdocs... and nothing displays. Variables ain't super global, with other words if you do something inside a function it will not automatically be the same outside the function $comment = "q"; function b() { $comment = "b"; } b(); echo $comment; This will result in "q" and not "b", as the $comment inside the function is another than the one outside the function. just to clarify, values will not magically get into functions $comment = "q"; function b() { echo $comment; } b(); this will return an empty output (logging undefined variable). There is a few exception from this, super global variable is one of those, like $_POST, $_GET, $_SESSION and so on, thos can be accessed anywhere in your code. You can use global in a function to access variable set outside a function (this is really bad coding and you will most likely get in bad trouble, so avoid using it). -- //Aho