Discussion:
undefined variable
Tim Streater
2014-03-22 23:37:00 UTC
Permalink
$str = "fcc: " . $PTR_fcc;
Now, upon examination it appears that I have an instance where $str ends up
fcc: undefined
My question is, under what conditions can a variable be set by the PHP system
to the string "undefined"? Would this require that my ajax call in the
... &PTR_fcc=undefined 

Not explicit as such, but that is what’s happening. Javascript will use the
string "undefined" whenever an undefined variable is concatenated on to a
string.
How about if PTR_fcc is not so much undefined on the JavaScript side, as an empty string. So that I'm passing e.g. this, via AJAX:

... &PTR_fcc=&myvar=27 ...

What does that mean in my PHP script when I then do:

$PTR_fcc = isset($_POST['PTR_fcc'])==true ? $_POST['PTR_fcc'] : 0;

i.e. what value does $_POST['PTR_fcc'] have?
--
Cheers -- Tim
Aziz Saleh
2014-03-22 23:43:14 UTC
Permalink
$str = "fcc: " . $PTR_fcc;
Now, upon examination it appears that I have an instance where $str
ends up
fcc: undefined
My question is, under what conditions can a variable be set by the PHP
system
to the string "undefined"? Would this require that my ajax call in the
... &PTR_fcc=undefined 

Not explicit as such, but that is what’s happening. Javascript will use
the
string "undefined" whenever an undefined variable is concatenated on to a
string.
How about if PTR_fcc is not so much undefined on the JavaScript side, as
... &PTR_fcc=&myvar=27 ...
$PTR_fcc = isset($_POST['PTR_fcc'])==true ? $_POST['PTR_fcc'] : 0;
i.e. what value does $_POST['PTR_fcc'] have?
--
Cheers -- Tim
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
A variable is undefined if it was never initialized. In your question, the
value of $PTR_fcc would be empty "" because $_POST['PTR_fcc'] was
initialized to an empty string.

To validate for empty strings as well:

$PTR_fcc = !empty($_POST['PTR_fcc']) ? $_POST['PTR_fcc'] : 0;

Aziz
Jim Lucas
2014-03-24 16:11:07 UTC
Permalink
Post by Tim Streater
$str = "fcc: " . $PTR_fcc;
Now, upon examination it appears that I have an instance where $str ends up
fcc: undefined
My question is, under what conditions can a variable be set by the PHP system
to the string "undefined"? Would this require that my ajax call in the
... &PTR_fcc=undefined …
Not explicit as such, but that is what’s happening. Javascript will use the
string "undefined" whenever an undefined variable is concatenated on to a
string.
... &PTR_fcc=&myvar=27 ...
$PTR_fcc = isset($_POST['PTR_fcc'])==true ? $_POST['PTR_fcc'] : 0;
i.e. what value does $_POST['PTR_fcc'] have?
http://www.php.net/manual/en/function.var-dump.php is your new best friend!
Post by Tim Streater
--
Cheers -- Tim
--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Loading...