Professional Web Applications Themes

Donít unset that variable you need globally - PHP Development

In comp.lang.php steve <ab.com> wrote:  No no, Steve was asking an existentialistic question: How to check if a variable is set? isset() is not the answer to the question, easily demonstrated with a little script which outputs: $ php4 ./huh.php isset(foo): false, isset(bar): false Notice: Undefined variable: bar in /tmp/huh.php on line 8 foo: [], bar: [] Both $foo and $bar aren't set (according ot isset()), using them anyway results in an undefined notice for $bar only. So $foo is defined. But defined() is for contants only. So the question remains how to findout if a variable is set. A ...

  1. #21

    Default Re: Don't unset that variable you need globally

    In comp.lang.php steve <ab.com> wrote: 

    No no, Steve was asking an existentialistic question:

    How to check if a variable is set?

    isset() is not the answer to the question, easily demonstrated with a
    little script which outputs:
    $ php4 ./huh.php
    isset(foo): false, isset(bar): false

    Notice: Undefined variable: bar in /tmp/huh.php on line 8
    foo: [], bar: []

    Both $foo and $bar aren't set (according ot isset()), using them anyway
    results in an undefined notice for $bar only. So $foo is defined. But
    defined() is for contants only.

    So the question remains how to findout if a variable is set. A
    workaround for variables in the global scope exists by checking for the
    key in the $GLOBALS array:
    keyexists(foo): true, keyexists(bar): false

    But how can this check be computed for variables in any other scope?

    My sample code:
    <?php
    error_reporting(E_ALL);

    $foo=null;

    echo "isset(foo): ".(isset($foo)?"true":"false").", isset(bar): ".(isset($bar)?"true":"false")."\n";

    echo "foo: [$foo], bar: [$bar]\n";

    echo "keyexists(foo): ".(array_key_exists('foo',$GLOBALS)?"true":"false" ).", keyexists(bar): ".(array_key_exists('bar',$GLOBALS)?"true":"false" )."\n";
    ?>

    Daniel Guest

  2. #22

    Default Re: Don't unset that variable you need globally

    gotcha.


    "Daniel Tryba" <tryba.nl> wrote in message
    news:42cbf146$0$87250$xs4all.nl...
    | In comp.lang.php steve <ab.com> wrote:
    | > | I am looking for something like defined(CONSTANT) but I guess not..
    | >
    | > what?
    | >
    | > you're crossing my wires. are you trying to define a constant or work
    with
    | > variable scope? why not just post your code that's giving you fits. i'd
    be
    | > willing to bet that the problem is not with either constants or globals.
    | > but, that's just me.
    |
    | No no, Steve was asking an existentialistic question:
    |
    | How to check if a variable is set?
    |
    | isset() is not the answer to the question, easily demonstrated with a
    | little script which outputs:
    | $ php4 ./huh.php
    | isset(foo): false, isset(bar): false
    |
    | Notice: Undefined variable: bar in /tmp/huh.php on line 8
    | foo: [], bar: []
    |
    | Both $foo and $bar aren't set (according ot isset()), using them anyway
    | results in an undefined notice for $bar only. So $foo is defined. But
    | defined() is for contants only.
    |
    | So the question remains how to findout if a variable is set. A
    | workaround for variables in the global scope exists by checking for the
    | key in the $GLOBALS array:
    | keyexists(foo): true, keyexists(bar): false
    |
    | But how can this check be computed for variables in any other scope?
    |
    | My sample code:
    | <?php
    | error_reporting(E_ALL);
    |
    | $foo=null;
    |
    | echo "isset(foo): ".(isset($foo)?"true":"false").", isset(bar):
    ".(isset($bar)?"true":"false")."\n";
    |
    | echo "foo: [$foo], bar: [$bar]\n";
    |
    | echo "keyexists(foo):
    ".(array_key_exists('foo',$GLOBALS)?"true":"false" ).", keyexists(bar):
    ".(array_key_exists('bar',$GLOBALS)?"true":"false" )."\n";
    | ?>
    |


    steve Guest

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Any way to unset a variable?
    By trave801 webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 9
    Last Post: January 25th, 05:18 AM
  2. storing values non-globally
    By Kevin Pfeiffer in forum PERL Beginners
    Replies: 1
    Last Post: October 5th, 11:26 PM
  3. #21569 [Com]: PHP can't set cookies (setcookie) after invoking an unset variable
    By ggoswami at sbcglobal dot net in forum PHP Development
    Replies: 0
    Last Post: September 15th, 12:13 AM
  4. Replies: 0
    Last Post: July 9th, 04:39 PM
  5. #22014 [Com]: html_entity_decode with unset variable results in segfault
    By msw at altexa dot com in forum PHP Development
    Replies: 0
    Last Post: July 9th, 03:18 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139