Jump to content

[SOLVED] Declaring a function


Azu

Recommended Posts

function dumbyfunction($test_var="lol"){die($test_var);}

Works

 

But

function dumbyfunction($test_var="getenv('REMOTE_ADDR')"){die($test_var);}

function dumbyfunction($test_var=getenv('REMOTE_ADDR')){die($test_var);}

function dumbyfunction($test_var="$GLOBALS[MyIp]"){die($test_var);}

function dumbyfunction($test_var='$GLOBALS[MyIp]'){die($test_var);}

function dumbyfunction($test_var='getenv('REMOTE_ADDR')'){die($test_var);}

all crash and burn. Please tell me why this happens and how I can fix it.

Link to comment
https://forums.phpfreaks.com/topic/41620-solved-declaring-a-function/
Share on other sites

As stated in the manual.

 

The default value must be a constant expression, not (for example) a variable, a class member or a function call.

 

In other words, you need to default your value to NULL or something else recognizable, and if the default value comes through, assign the function call within your declared function:

<?php
function dummyfunction($test_var = '') {
  if (empty($test_var)) $test_var = getenv('REMOTE_ADDR');
  die($test_var);
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.