GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Downloads
  • Documentation
  • Get Involved
  • Help
  • Getting Started
  • Introduction
  • A simple tutorial
  • Language Reference
  • Basic syntax
  • Types
  • Variables
  • Constants
  • Expressions
  • Operators

This view

  • Original page
  • New address

GeezerVison is showing www.php.net — PHP: putenv - Manual

PHP 8.6.0 RC5 available for testing
    restore_include_path »
    « phpversion
    • Preface
    • Function Reference
    • Affecting PHP's Behaviour
    • PHP Options/Info
    • PHP Options/Info Functions

    putenv

    (PHP 4, PHP 5, PHP 7, PHP 8)

    putenv — Sets the value of an environment variable

    Description

    function putenv(string $assignment): bool

    Adds assignment to the server environment. The environment variable will only exist for the duration of the current request. At the end of the request the environment is restored to its original state.

    Parameters

    assignment

    The setting, like "FOO=BAR"

    Return Values

    Returns true on success or false on failure.

    Examples

    Example #1 Setting an environment variable

    <?php
    putenv("UNIQID=$uniqid");
    ?>

    See Also

    • getenv() - Gets the value of a single or all environment variables
    • apache_setenv() - Set an Apache subprocess_env variable

    Found A Problem?

    Learn How To Improve This Page • Submit a Pull Request • Report a Bug
    +add a note

    User Contributed Notes 6 notes

    up
    down
    83
    php at keith tyler dot com ¶
    16 years ago
    putenv/getenv, $_ENV, and phpinfo(INFO_ENVIRONMENT) are three completely distinct environment stores. doing putenv("x=y") does not affect $_ENV; but also doing $_ENV["x"]="y" likewise does not affect getenv("x"). And neither affect what is returned in phpinfo().
    
    Assuming the USER environment variable is defined as "dave" before running the following:
    
    <?php
    print "env is: ".$_ENV["USER"]."\n";
    print "(doing: putenv fred)\n";
    putenv("USER=fred");
    print "env is: ".$_ENV["USER"]."\n";
    print "getenv is: ".getenv("USER")."\n";
    print "(doing: set _env barney)\n";
    $_ENV["USER"]="barney";
    print "getenv is: ".getenv("USER")."\n";
    print "env is: ".$_ENV["USER"]."\n";
    phpinfo(INFO_ENVIRONMENT);
    ?>
    
    prints:
    
    env is: dave
    (doing: putenv fred)
    env is: dave
    getenv is: fred
    (doing: set _env barney)
    getenv is: fred
    env is: barney
    phpinfo()
    
    Environment
    
    Variable => Value
    ...
    USER => dave
    ...
    up
    down
    37
    JM ¶
    19 years ago
    The other problem with the code from av01 at bugfix dot cc is that
    the behaviour is as per the comments here, not there:
    <?php
    putenv('MYVAR='); // set MYVAR to an empty value.  It is in the environment
    putenv('MYVAR'); // unset MYVAR.  It is removed from the environment
    ?>
    up
    down
    8
    domger at freenet dot de ¶
    3 years ago
    Values of variables with dots in their names are not output when using getenv(), but are still present and can be explicitly queried.
    
    (saw this behaviour using PHP 8.2.4)
    
    <?php
    
    // set
    putenv('foo.bar=baz');
    
    // dump all
    var_dump(getenv());    # <== variable 'foo.bar' NOT included, its value is not dumped
    
    // dump explicitely 'foo.bar'
    var_dump(getenv('foo.bar'));    # works, value 'baz' is shown
    up
    down
    12
    t7to7 ¶
    7 years ago
    White spaces are allowed in environment variable names so :
    
    <?php
    putenv('U =33');
    ?>
    
    Is not equivalent to 
    
    <?php
    putenv('U=33');
    ?>
    up
    down
    8
    Anonymous Coder ¶
    14 years ago
    It's the putenv() type of environment variables that get passed to a child process executed via exec().
    
    If you need to delete an existing environment variable so the child process does not see it, use:
    
    putenv('FOOBAR');
    
    That is, leave out both the "=" and a value.
    up
    down
    11
    david dot boyce at messagingdirect dot comnospam ¶
    26 years ago
    Environment variables are part of the underlying operating system's
    way of doing things, and are used to pass information between a parent
    process and its child, as well as to affect the way some internal
    functions behave.  They should not be regarded as ordinary PHP
    variables.
    
    A primary purpose of setting environment variables in a PHP script is
    so that they are available to processes invoked by that script using
    e.g. the system() function, and it's unlikely that they would need to
    be changed for other reasons.
    
    For example, if a particular system command required a special value
    of the environment variable LD_LIBRARY_PATH to execute successfully,
    then the following code might be used on a *NIX system:
    
    <?php
     $saved = getenv("LD_LIBRARY_PATH");        // save old value
     $newld = "/extra/library/dir:/another/path/to/lib";  // extra paths to add
     if ($saved) { $newld .= ":$saved"; }           // append old paths if any
     putenv("LD_LIBRARY_PATH=$newld");        // set new value
     system("mycommand -with args");        // do system command; 
                            // mycommand is loaded using
                            // libs in the new path list
     putenv("LD_LIBRARY_PATH=$saved");        // restore old value
    ?>
    
    It will usually be appropriate to restore the old value after use;
    LD_LIBRARY_PATH is a particularly good example of a variable which it
    is important to restore immediately, as it is used by internal
    functions.
    
    If php.ini configuration allows, the values of environment variables
    are made available as PHP global variables on entry to a script, but
    these global variables are merely copies and do not track the actual
    environment variables once the script is entered.  Changing
    $REMOTE_ADDR (or even $HTTP_ENV_VARS["REMOTE_ADDR"]) should not be
    expected to affect the actual environment variable; this is why
    putenv() is needed.
    
    Finally, do not rely on environment variables maintaining the same
    value from one script invocation to the next, especially if you have
    used putenv().  The result depends on many factors, such as CGI vs
    apache module, and the exact way in which the environment is
    manipulated before entering the script.
    +add a note
    • PHP Options/Info Functions
      • assert
      • cli_​get_​process_​title
      • cli_​set_​process_​title
      • dl
      • extension_​loaded
      • gc_​collect_​cycles
      • gc_​disable
      • gc_​enable
      • gc_​enabled
      • gc_​mem_​caches
      • gc_​status
      • get_​cfg_​var
      • get_​current_​user
      • get_​defined_​constants
      • get_​extension_​funcs
      • get_​include_​path
      • get_​included_​files
      • get_​loaded_​extensions
      • get_​required_​files
      • get_​resources
      • getenv
      • getlastmod
      • getmygid
      • getmyinode
      • getmypid
      • getmyuid
      • getopt
      • getrusage
      • ini_​alter
      • ini_​get
      • ini_​get_​all
      • ini_​parse_​quantity
      • ini_​restore
      • ini_​set
      • memory_​get_​peak_​usage
      • memory_​get_​usage
      • memory_​reset_​peak_​usage
      • php_​ini_​loaded_​file
      • php_​ini_​scanned_​files
      • php_​sapi_​name
      • php_​uname
      • phpcredits
      • phpinfo
      • phpversion
      • putenv
      • set_​include_​path
      • set_​time_​limit
      • sys_​get_​temp_​dir
      • version_​compare
      • zend_​thread_​id
      • zend_​version
    • Deprecated
      • assert_​options
      • get_​magic_​quotes_​gpc
      • get_​magic_​quotes_​runtime
      • restore_​include_​path
    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.