Export local variable to global variable scope? - PHP

This is a discussion on Export local variable to global variable scope? - PHP ; Hello, I have a function, e.g. function foo() { include("bar.php"); } and the bar.php contain contents e.g. $global_v1 = "abc"; $global_v2 = "def"; I want to execute function foo(), but at the same time, let the variables declared in bar.php ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 17

Export local variable to global variable scope?

  1. Default Export local variable to global variable scope?

    Hello,

    I have a function, e.g.

    function foo() {
    include("bar.php");
    }

    and the bar.php contain contents e.g.

    $global_v1 = "abc";
    $global_v2 = "def";

    I want to execute function foo(), but at the same time, let the
    variables declared in bar.php to have global scopem is it possible?

    Thanks.


  2. Default Re: Export local variable to global variable scope?

    howa wrote:
    > Hello,
    >
    > I have a function, e.g.
    >
    > function foo() {
    > include("bar.php");
    > }
    >
    > and the bar.php contain contents e.g.
    >
    > $global_v1 = "abc";
    > $global_v2 = "def";
    >
    > I want to execute function foo(), but at the same time, let the
    > variables declared in bar.php to have global scopem is it possible?
    >
    > Thanks.
    >


    You should not have an include within a function call. Among other
    things, you 'll have problems if you call the function twice.

    Actually, globals are bad things to use, anyway.

    --
    ==================
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attglobal.net
    ==================

  3. Default Re: Export local variable to global variable scope?

    On 29 Sep, 13:23, howa <howac...@gmail.com> wrote:
    > Hello,
    >
    > I have a function, e.g.
    >
    > function foo() {
    > include("bar.php");
    >
    > }
    >
    > and the bar.php contain contents e.g.
    >
    > $global_v1 = "abc";
    > $global_v2 = "def";
    >
    > I want to execute function foo(), but at the same time, let the
    > variables declared in bar.php to have global scopem is it possible?
    >
    > Thanks.


    function foo() {
    global $global_v1;
    global $global_v2;

    include "bar.php";
    }

    Good enough?

    ---
    Bruno Rafael Moreira de Barros

    Adobe Photoshop CS2 and CS3
    -
    XML / XSLT
    -
    MySQL / SQLite / TerraDB
    -
    PHP 3, 4, 5 and 6

    :: Looking For A Permanent Job ::
    ---


  4. Default Re: Export local variable to global variable scope?

    Assume that I know nothing about the stuffs inside bar.php...is it
    possible?



    On 9 29 , 9 49 , Bruno Barros <rage...@gmail.com> wrote:
    > On 29 Sep, 13:23, howa <howac...@gmail.com> wrote:
    >
    >
    >
    > > Hello,

    >
    > > I have a function, e.g.

    >
    > > function foo() {
    > > include("bar.php");

    >
    > > }

    >
    > > and the bar.php contain contents e.g.

    >
    > > $global_v1 = "abc";
    > > $global_v2 = "def";

    >
    > > I want to execute function foo(), but at the same time, let the
    > > variables declared in bar.php to have global scopem is it possible?

    >
    > > Thanks.

    >
    > function foo() {
    > global $global_v1;
    > global $global_v2;
    >
    > include "bar.php";
    >
    > }
    >
    > Good enough?
    >
    > ---
    > Bruno Rafael Moreira de Barros
    >
    > Adobe Photoshop CS2 and CS3
    > -
    > XML / XSLT
    > -
    > MySQL / SQLite / TerraDB
    > -
    > PHP 3, 4, 5 and 6
    >
    > :: Looking For A Permanent Job ::
    > ---




  5. Default Re: Export local variable to global variable scope?

    >> you 'll have problems if you call the function twice.

    this can be solved by using include_once.

    the reason of using function call to include is we want to selectively
    include the needed files, prevent include useless files (i.e. factory
    method?)


    On 9 29 , 9 10 , Jerry Stuckle <jstuck...@attglobal.net> wrote:
    > howa wrote:
    > > Hello,

    >
    > > I have a function, e.g.

    >
    > > function foo() {
    > > include("bar.php");
    > > }

    >
    > > and the bar.php contain contents e.g.

    >
    > > $global_v1 = "abc";
    > > $global_v2 = "def";

    >
    > > I want to execute function foo(), but at the same time, let the
    > > variables declared in bar.php to have global scopem is it possible?

    >
    > > Thanks.

    >
    > You should not have an include within a function call. Among other
    > things, you 'll have problems if you call the function twice.
    >
    > Actually, globals are bad things to use, anyway.
    >
    > --
    > ==================
    > Remove the "x" from my email address
    > Jerry Stuckle
    > JDS Computer Training Corp.
    > jstuck...@attglobal.net
    > ==================




  6. Default Re: Export local variable to global variable scope?

    "Bruno Barros" <ragearc@gmail.com> wrote in message
    news:1191073761.283822.306170@n39g2000hsh.googlegroups.com...
    > On 29 Sep, 13:23, howa <howac...@gmail.com> wrote:


    > include "bar.php";
    >
    > Good enough?


    Better to use "requires_once()" or "include_once()".



  7. Default Re: Export local variable to global variable scope?

    howa wrote:
    >>> you 'll have problems if you call the function twice.

    > On 9 29 , 9 10 , Jerry Stuckle <jstuck...@attglobal.net> wrote:
    >> howa wrote:
    >>> Hello,
    >>> I have a function, e.g.
    >>> function foo() {
    >>> include("bar.php");
    >>> }
    >>> and the bar.php contain contents e.g.
    >>> $global_v1 = "abc";
    >>> $global_v2 = "def";
    >>> I want to execute function foo(), but at the same time, let the
    >>> variables declared in bar.php to have global scopem is it possible?
    >>> Thanks.

    >> You should not have an include within a function call. Among other
    >> things, you 'll have problems if you call the function twice.
    >>
    >> Actually, globals are bad things to use, anyway.
    >>
    >> --
    >> ==================
    >> Remove the "x" from my email address
    >> Jerry Stuckle
    >> JDS Computer Training Corp.
    >> jstuck...@attglobal.net
    >> ==================

    >
    >
    >
    > this can be solved by using include_once.
    >
    > the reason of using function call to include is we want to selectively
    > include the needed files, prevent include useless files (i.e. factory
    > method?)
    >
    >


    (Top posting fixed)

    It isn't that much overhead to include an extra file or two. But if you
    do that a lot, perhaps you should examine your methods.

    And please don't top post. Thanks.

    --
    ==================
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attglobal.net
    ==================

  8. Default Re: Export local variable to global variable scope?

    On 29 Sep, 19:26, "Sanders Kaufman" <bu...@kaufman.net> wrote:
    > "Bruno Barros" <rage...@gmail.com> wrote in message
    >
    > news:1191073761.283822.306170@n39g2000hsh.googlegroups.com...
    >
    > > On 29 Sep, 13:23, howa <howac...@gmail.com> wrote:
    > > include "bar.php";

    >
    > > Good enough?

    >
    > Better to use "requires_once()" or "include_once()".


    Yes I know that.


  9. Default Re: Export local variable to global variable scope?

    >> And please don't top post. Thanks.

    I am posting via Google Group, I am no control.



    On 9 30 , 2 42 , Jerry Stuckle <jstuck...@attglobal.net> wrote:
    > howa wrote:
    > >>> you 'll have problems if you call the function twice.

    > > On 9 29 , 9 10 , Jerry Stuckle <jstuck...@attglobal.net> wrote:
    > >> howa wrote:
    > >>> Hello,
    > >>> I have a function, e.g.
    > >>> function foo() {
    > >>> include("bar.php");
    > >>> }
    > >>> and the bar.php contain contents e.g.
    > >>> $global_v1 = "abc";
    > >>> $global_v2 = "def";
    > >>> I want to execute function foo(), but at the same time, let the
    > >>> variables declared in bar.php to have global scopem is it possible?
    > >>> Thanks.
    > >> You should not have an include within a function call. Among other
    > >> things, you 'll have problems if you call the function twice.

    >
    > >> Actually, globals are bad things to use, anyway.

    >
    > >> --
    > >> ==================
    > >> Remove the "x" from my email address
    > >> Jerry Stuckle
    > >> JDS Computer Training Corp.
    > >> jstuck...@attglobal.net
    > >> ==================

    >
    > >
    > > this can be solved by using include_once.
    > >
    > > the reason of using function call to include is we want to selectively
    > > include the needed files, prevent include useless files (i.e. factory
    > > method?)
    > >
    > >

    >
    > (Top posting fixed)
    >
    > It isn't that much overhead to include an extra file or two. But if you
    > do that a lot, perhaps you should examine your methods.
    >
    > And please don't top post. Thanks.
    >
    > --
    > ==================
    > Remove the "x" from my email address
    > Jerry Stuckle
    > JDS Computer Training Corp.
    > jstuck...@attglobal.net
    > ==================




  10. Default Re: Export local variable to global variable scope?

    On 30 Sep, 08:30, howa <howac...@gmail.com> wrote:
    > >> And please don't top post. Thanks.

    >
    > I am posting via Google Group, I am no control.
    >
    > On 9 30 , 2 42 , Jerry Stuckle <jstuck...@attglobal.net> wrote:
    >
    > > howa wrote:
    > > >>> you 'll have problems if you call the function twice.
    > > > On 9 29 , 9 10 , Jerry Stuckle <jstuck...@attglobal.net> wrote:
    > > >> howa wrote:
    > > >>> Hello,
    > > >>> I have a function, e.g.
    > > >>> function foo() {
    > > >>> include("bar.php");
    > > >>> }
    > > >>> and the bar.php contain contents e.g.
    > > >>> $global_v1 = "abc";
    > > >>> $global_v2 = "def";
    > > >>> I want to execute function foo(), but at the same time, let the
    > > >>> variables declared in bar.php to have global scopem is it possible?
    > > >>> Thanks.
    > > >> You should not have an include within a function call. Among other
    > > >> things, you 'll have problems if you call the function twice.

    >
    > > >> Actually, globals are bad things to use, anyway.

    >
    > > >> --
    > > >> ==================
    > > >> Remove the "x" from my email address
    > > >> Jerry Stuckle
    > > >> JDS Computer Training Corp.
    > > >> jstuck...@attglobal.net
    > > >> ==================

    >
    > > > this can be solved by using include_once.

    >
    > > > the reason of using function call to include is we want to selectively
    > > > include the needed files, prevent include useless files (i.e. factory
    > > > method?)

    >
    > > (Top posting fixed)

    >
    > > It isn't that much overhead to include an extra file or two. But if you
    > > do that a lot, perhaps you should examine your methods.

    >
    > > And please don't top post. Thanks.

    >
    > > --
    > > ==================
    > > Remove the "x" from my email address
    > > Jerry Stuckle
    > > JDS Computer Training Corp.
    > > jstuck...@attglobal.net
    > > ==================

    >
    >


    I post via Google Groups and never top-posted .


+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. global const variable being local to a file by default
    By Application Development in forum c++
    Replies: 6
    Last Post: 12-02-2007, 04:24 AM
  2. Replies: 0
    Last Post: 09-04-2007, 02:15 PM
  3. save local variable as global
    By Application Development in forum basic.visual
    Replies: 15
    Last Post: 03-09-2007, 03:04 PM
  4. Parallel::ForkManager pass a variable to global scope?
    By Application Development in forum Perl
    Replies: 3
    Last Post: 09-02-2006, 07:49 AM
  5. Replies: 1
    Last Post: 02-22-2006, 11:22 AM