Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #10827
| 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" <user@example.net> |
| Newsgroups | comp.lang.php |
| Subject | Re: Setting & displaying Variables |
| Date | Sat, 21 Sep 2013 23:55:33 +0200 |
| Lines | 67 |
| Message-ID | <ba6iumFqfo0U1@mid.individual.net> (permalink) |
| References | <l1kv32$ll0$1@speranza.aioe.org> |
| 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 | <l1kv32$ll0$1@speranza.aioe.org> |
| Xref | csiph.com comp.lang.php:10827 |
Show key headers only | View raw
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?
>
> ===================
> <code>
> echo strlen($_POST["comments"])."<br />"; //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
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Setting & displaying Variables Twayne <nobody@spamcop.net> - 2013-09-21 16:20 -0400
Re: Setting & displaying Variables Jerry Stuckle <jstucklex@attglobal.net> - 2013-09-21 16:25 -0400
Re: Setting & displaying Variables The Natural Philosopher <tnp@invalid.invalid> - 2013-09-21 21:29 +0100
Re: Setting & displaying Variables Richard Damon <Richard@Damon-Family.org> - 2013-09-21 16:47 -0400
Re: Setting & displaying Variables Twayne <nobody@spamcop.net> - 2013-09-22 16:47 -0400
Re: Setting & displaying Variables Richard Damon <Richard@Damon-Family.org> - 2013-09-22 17:58 -0400
Re: Setting & displaying Variables Richard Yates <richard@yatesguitar.com> - 2013-09-21 14:29 -0700
Re: Setting & displaying Variables Twayne <nobody@spamcop.net> - 2013-09-22 16:54 -0400
Re: Setting & displaying Variables Twayne <nobody@spamcop.net> - 2013-09-22 17:19 -0400
Re: Setting & displaying Variables Richard Yates <richard@yatesguitar.com> - 2013-09-22 14:39 -0700
Re: Setting & displaying Variables Twayne <nobody@spamcop.net> - 2013-09-23 19:54 -0400
Re: Setting & displaying Variables "J.O. Aho" <user@example.net> - 2013-09-21 23:55 +0200
Re: Setting & displaying Variables Twayne <nobody@spamcop.net> - 2013-09-22 16:57 -0400
csiph-web