php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #13315 problem with function definitions
Submitted: 2001-09-15 05:11 UTC Modified: 2001-09-15 07:27 UTC
From: php at altemachtige dot reageerttochniet dot nl Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 4.0.4pl1 OS: WIN98/APACHE1.3.19
Private report: No CVE-ID: None
 [2001-09-15 05:11 UTC] php at altemachtige dot reageerttochniet dot nl
<?php
    $foo = 'foo';
    function showvar( $bar = $foo )
    {
        //global $foo
        //doesn't work either
        echo $bar;
    }
    showvar();
    showvar('bar');
?>

gives me a parse error on line 3:(

also:

<?php
    $foo = 'foo';
    function returnvar()
    {
        global $foo;
        return $foo;
    }

    function whatisvar( $bar = returnvar() )
    {
        echo $bar;
    }
?>

gives me this:
Parse error: parse error, expecting `')'' in c:/path/to/script/test.php on line 9

Should work, shouldn't it?

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-09-15 05:28 UTC] mfischer@php.net
Shouldn't. Default values must be constant expressions.

Not a bug, bogusified.

- Markus
 [2001-09-15 05:43 UTC] php at altemachtige dot reageerttochniet dot nl
why is that? I really need this:(
Will a later version of php support constructions like this?
 [2001-09-15 07:27 UTC] hholzgra@php.net
no way, default values are evaluated at compile time
and there is no $foo set at all as the program didn't
start to run by that time

what you want to do is something like

  function showvar ( $bar = NULL ) {
    if ( $bar === NULL ) {   
      $bar = $GLOBALS['foo'];
    }
    echo $bar;
  }
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC