FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Setting & displaying Variables
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Setting & displaying Variables [message #182859] Sat, 21 September 2013 20:20 Go to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
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.

// echo strlen($comments); // GETS Notice: Undefined variable: comments
in C:\xampp\htdocs... and nothing displays.

echo $_POST["comments"]; // WORKS
//echo $comments; // GETS Notice: Undefined variable: comments in C
.... and nothing displays.

</code>
===================

I'm obviously missing something here since it appears the var is set
according to the 2 statements that do display what I expected. Can you
straighten me out? I've had this happen before with other vars and it's
always confusing.

TIA,

Twayne`
Re: Setting & displaying Variables [message #182860 is a reply to message #182859] Sat, 21 September 2013 20:25 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 9/21/2013 4:20 PM, 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.
>
> // echo strlen($comments); // GETS Notice: Undefined variable: comments
> in C:\xampp\htdocs... and nothing displays.
>
> echo $_POST["comments"]; // WORKS
> //echo $comments; // GETS Notice: Undefined variable: comments in C
> ... and nothing displays.
>
> </code>
> ===================
>
> I'm obviously missing something here since it appears the var is set
> according to the 2 statements that do display what I expected. Can you
> straighten me out? I've had this happen before with other vars and it's
> always confusing.
>
> TIA,
>
> Twayne`
>
>

Your code is obviously screwed up. But by posting excerpts of your
code, you make it impossible for anyone to tell you what you screwed up.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Setting & displaying Variables [message #182861 is a reply to message #182859] Sat, 21 September 2013 20:29 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 21/09/13 21:20, Twayne wrote:

> I'm obviously missing something here
> Twayne`
>

Probably a brain.

>


--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
Re: Setting & displaying Variables [message #182862 is a reply to message #182859] Sat, 21 September 2013 20:47 Go to previous messageGo to next message
Richard Damon is currently offline  Richard Damon
Messages: 58
Registered: August 2011
Karma: 0
Member
On 9/21/13 4:20 PM, 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.
>
> // echo strlen($comments); // GETS Notice: Undefined variable: comments
> in C:\xampp\htdocs... and nothing displays.
>
> echo $_POST["comments"]; // WORKS
> //echo $comments; // GETS Notice: Undefined variable: comments in C
> ... and nothing displays.
>
> </code>
> ===================
>
> I'm obviously missing something here since it appears the var is set
> according to the 2 statements that do display what I expected. Can you
> straighten me out? I've had this happen before with other vars and it's
> always confusing.
>
> TIA,
>
> Twayne`
>
>

As others have commented on, not showing us the full code makes it very
hard to help. The error message you are quoting says that the variable
does NOT at that point have a value assigned to it.

You say that the variables have to be defined for the echo statements to
work, but then you also say that you get the error because they are not
defined.

Note that PHP is not like more strictly declared languages, you do not
need to "create" variables explicitly to use them, they will be created
as needed. Thus a script like:

<?php
echo $comment
?>

will "work" in the sense that the PHP language defines what it will
generate, which includes a possible "Notice" due to using the value of a
variable that was never set, and thus is possibly wrong, but is defined
(which is the reason it is just a NOTICE, one of the lower levels of
messages defined).

Another comment is your "WORKS but not needed, done elsewhere"
statement, that might be refering to the now obsolete functionality of
inserting into the global variable space the contents of some of the
"super-globals" like $_POST. Older version of PHP would default to this
behavior, later ones defaulted to not doing this behavior, and the
latest version (if I remember right), no longer have the option to do
this behavior (except via explicit script code). If you script depended
on this behavior, and was moved or the server upgraded, then it could
break for this reason.
Re: Setting & displaying Variables [message #182864 is a reply to message #182859] Sat, 21 September 2013 21:29 Go to previous messageGo to next message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
On Sat, 21 Sep 2013 16:20:49 -0400, Twayne <nobody(at)spamcop(dot)net> wrote:

Probably those three Ms in '$commments'


> 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.
>
> // echo strlen($comments); // GETS Notice: Undefined variable: comments
> in C:\xampp\htdocs... and nothing displays.
>
> echo $_POST["comments"]; // WORKS
> //echo $comments; // GETS Notice: Undefined variable: comments in C
> ... and nothing displays.
>
> </code>
> ===================
>
> I'm obviously missing something here since it appears the var is set
> according to the 2 statements that do display what I expected. Can you
> straighten me out? I've had this happen before with other vars and it's
> always confusing.
>
> TIA,
>
> Twayne`
>
Re: Setting & displaying Variables [message #182865 is a reply to message #182859] Sat, 21 September 2013 21:55 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
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
Re: Setting & displaying Variables [message #182868 is a reply to message #182862] Sun, 22 September 2013 20:47 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 2013-09-21 4:47 PM, Richard Damon wrote:
> On 9/21/13 4:20 PM, 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.
>>
>> // echo strlen($comments); // GETS Notice: Undefined variable: comments
>> in C:\xampp\htdocs... and nothing displays.
>>
>> echo $_POST["comments"]; // WORKS
>> //echo $comments; // GETS Notice: Undefined variable: comments in C
>> ... and nothing displays.
>>
>> </code>
>> ===================
....
>
> As others have commented on, not showing us the full code makes it very
> hard to help. The error message you are quoting says that the variable
> does NOT at that point have a value assigned to it.

No, and I doubt anyone would go thru 500 lines of code even if I were
allowed to post it.
That said, the snippet above is ONE snippet of consecutive code lines;
they are not simply gathered into one place; That's the precise order
they occur in.

>
> You say that the variables have to be defined for the echo statements to
> work, but then you also say that you get the error because they are not
> defined.

No I say they're defined; as evidenced by the working code I indicated.
PHP/ my Local Server are tossing up the " Notice; undefined ... "
messages.

>
> Note that PHP is not like more strictly declared languages, you do not
> need to "create" variables explicitly to use them, they will be created
> as needed.

Of course I understand that!

Thus a script like:
>
> <?php
> echo $comment
> ?>
>
> will "work" in the sense that the PHP language defines what it will
> generate, which includes a possible "Notice" due to using the value of a
> variable that was never set, and thus is possibly wrong, but is defined
> (which is the reason it is just a NOTICE, one of the lower levels of
> messages defined).

Of course; nothing new there.

>
> Another comment is your "WORKS but not needed, done elsewhere"
> statement, that might be refering to the now obsolete functionality of
> inserting into the global variable space the contents of some of the
> "super-globals" like $_POST.

That doesn't make a lot of sense, whatever idea you wanted to get
across. There are no obsolete issues like that and $_POST functions
properly in myriad other places where it's used.
If I'm missing your point, please clarify further; you can't say
$_POST is obsolete, which appears to be your point here.

Older version of PHP would default to this
> behavior,

Using PHP 5.3.x and HTML 5; 5 has not obsoleted anything I've done.

later ones defaulted to not doing this behavior, and the
> latest version (if I remember right), no longer have the option to do
> this behavior (except via explicit script code). If you script depended
> on this behavior, and was moved or the server upgraded, then it could
> break for this reason.

There have been zero changes on the server; it's my own local server and
like I said, is not obsoleting anything I'm aware of.

I'm sorry, but none of that last para makes much sense in the context of
the query I posed.

Regards,

Twayne`
>
Re: Setting & displaying Variables [message #182869 is a reply to message #182864] Sun, 22 September 2013 20:54 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 2013-09-21 5:29 PM, Richard Yates wrote:
> On Sat, 21 Sep 2013 16:20:49 -0400, Twayne <nobody(at)spamcop(dot)net> wrote:
>
> Probably those three Ms in '$commments'

3 M's? You're kidding!! I'd love to think that's a typo, but ... I
pasted, not copied, the code, sooo ... Ohh, I might be about to feel so
stoopid!

You know, I still didn't see it even after you pointed it out! Had to
use Find to see it! Damn! I'm going to owe you bigtime if that's all it
is! :(


I'll let ya know!

Twayne`

>
>
>> 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.
>>
>> // echo strlen($comments); // GETS Notice: Undefined variable: comments
>> in C:\xampp\htdocs... and nothing displays.
>>
>> echo $_POST["comments"]; // WORKS
>> //echo $comments; // GETS Notice: Undefined variable: comments in C
>> ... and nothing displays.
>>
>> </code>
>> ===================
>>
>> I'm obviously missing something here since it appears the var is set
>> according to the 2 statements that do display what I expected. Can you
>> straighten me out? I've had this happen before with other vars and it's
>> always confusing.
>>
>> TIA,
>>
>> Twayne`
>>
Re: Setting & displaying Variables [message #182870 is a reply to message #182865] Sun, 22 September 2013 20:57 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 2013-09-21 5:55 PM, J.O. Aho wrote:
> On 21/09/13 22:20, Twayne wrote:
....
>>
>> ===================
>> <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).
>

Good advice; thanks.

Twayne`
Re: Setting & displaying Variables [message #182871 is a reply to message #182864] Sun, 22 September 2013 21:19 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 2013-09-21 5:29 PM, Richard Yates wrote:
> On Sat, 21 Sep 2013 16:20:49 -0400, Twayne <nobody(at)spamcop(dot)net> wrote:
>
> Probably those three Ms in '$commments'

CRAP!! I can't tell you how many times I looked at that code and didn't
see it!
But, that's all it took was to spell the var correctly, of course. I
am severely humbled and owe you a drink at the local bar were it
possible, or some other thing you'd appreciate if you're not a drinker!
Any chance you live in far upstate NY?

Regards,

Twayne`



>
>
>> 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.
>>
>> // echo strlen($comments); // GETS Notice: Undefined variable: comments
>> in C:\xampp\htdocs... and nothing displays.
>>
>> echo $_POST["comments"]; // WORKS
>> //echo $comments; // GETS Notice: Undefined variable: comments in C
>> ... and nothing displays.
>>
>> </code>
>> ===================
>>
>> I'm obviously missing something here since it appears the var is set
>> according to the 2 statements that do display what I expected. Can you
>> straighten me out? I've had this happen before with other vars and it's
>> always confusing.
>>
>> TIA,
>>
>> Twayne`
>>
Re: Setting & displaying Variables [message #182872 is a reply to message #182871] Sun, 22 September 2013 21:39 Go to previous messageGo to next message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
On Sun, 22 Sep 2013 17:19:13 -0400, Twayne <nobody(at)spamcop(dot)net> wrote:

> On 2013-09-21 5:29 PM, Richard Yates wrote:
>> On Sat, 21 Sep 2013 16:20:49 -0400, Twayne <nobody(at)spamcop(dot)net> wrote:
>>
>> Probably those three Ms in '$commments'
>
> CRAP!! I can't tell you how many times I looked at that code and didn't
> see it!
> But, that's all it took was to spell the var correctly, of course. I
> am severely humbled and owe you a drink at the local bar were it
> possible, or some other thing you'd appreciate if you're not a drinker!
> Any chance you live in far upstate NY?

No, Salem, Oregon. But my sister and brother live in Ithaca.

Things like those 3 Ms can be very hard to see. We see what we expect.
Music copyists sometimes work with score and copy upside down to
minimize faulty assumptions. Probably doesn't work with php though
(and its hard on the monitor stand).
Re: Setting & displaying Variables [message #182873 is a reply to message #182868] Sun, 22 September 2013 21:58 Go to previous messageGo to next message
Richard Damon is currently offline  Richard Damon
Messages: 58
Registered: August 2011
Karma: 0
Member
On 9/22/13 4:47 PM, Twayne wrote:
> On 2013-09-21 4:47 PM, Richard Damon wrote:
>> On 9/21/13 4:20 PM, 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.
>>>
>>> // echo strlen($comments); // GETS Notice: Undefined variable: comments
>>> in C:\xampp\htdocs... and nothing displays.
>>>
>>> echo $_POST["comments"]; // WORKS
>>> //echo $comments; // GETS Notice: Undefined variable: comments in C
>>> ... and nothing displays.
>>>
>>> </code>
>>> ===================
> ...
>>
>> As others have commented on, not showing us the full code makes it very
>> hard to help. The error message you are quoting says that the variable
>> does NOT at that point have a value assigned to it.
>
> No, and I doubt anyone would go thru 500 lines of code even if I were
> allowed to post it.
> That said, the snippet above is ONE snippet of consecutive code lines;
> they are not simply gathered into one place; That's the precise order
> they occur in.
>
The first step of reporting such a problem is to reduce your problem to
a simple case that demonstrates the issue. It seemed that you had done
that, but never gave us the code.

For example, the above code is NOT a piece of code that demonstrates the
problem, since the line reported as generating the NOTICE has been
commented out.

As pointed out else-thread, the problem was a typo, but since you
clearly weren't showing all thoe code (from things like the comment of
being done elsewhere), we could not tell if that same typo was done THERE.
Re: Setting & displaying Variables [message #182881 is a reply to message #182872] Mon, 23 September 2013 23:54 Go to previous message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 2013-09-22 5:39 PM, Richard Yates wrote:
> On Sun, 22 Sep 2013 17:19:13 -0400, Twayne <nobody(at)spamcop(dot)net> wrote:
>
>> On 2013-09-21 5:29 PM, Richard Yates wrote:
>>> On Sat, 21 Sep 2013 16:20:49 -0400, Twayne <nobody(at)spamcop(dot)net> wrote:
>>>
>>> Probably those three Ms in '$commments'
>>
>> CRAP!! I can't tell you how many times I looked at that code and didn't
>> see it!
>> But, that's all it took was to spell the var correctly, of course. I
>> am severely humbled and owe you a drink at the local bar were it
>> possible, or some other thing you'd appreciate if you're not a drinker!
>> Any chance you live in far upstate NY?
>
> No, Salem, Oregon. But my sister and brother live in Ithaca.
>
> Things like those 3 Ms can be very hard to see. We see what we expect.
> Music copyists sometimes work with score and copy upside down to
> minimize faulty assumptions. Probably doesn't work with php though
> (and its hard on the monitor stand).
>

Well, I could rotate my display 180 easily enough - but I'm not Russian
so upside reading isn't exactly second nature! :)

I think one solution is going to be to look for a better font to use;
one that's more readable or something that sports a real time spelling
dic. Verdana helped a little to see that sort of thing but I think there
are better ones. I'd like one that slashes zero, makes I and L and l
easily distinguishable and perhaps a little wider spacing. Verdana is a
start, but it doesn't do well with lower case letters; thus the need for
a wider letter spacing.
I sort of dread going thru the zillions of fonts available, but ...
it might be worth the trouble.

Sorry; didn't mean to wax philosophical there!

See ya 'round!

Twayne`
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: test for file existance
Next Topic: mail working, not working.
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 12:27:28 GMT 2024

Total time taken to generate the page: 0.02738 seconds