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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php at altemachtige dot reageerttochniet dot nl
New email:
PHP Version: OS:

 

 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2022 The PHP Group
All rights reserved.
Last updated: Sat May 28 16:04:40 2022 UTC