Jump to content

PHP: function help


Ben Phelps

Recommended Posts

function newFunction($var1, $var2, $optVar1, $optVar2)
{
    echo $var1;
    echo $var2;
    if (isset($optVar1)) {
        echo $optVar1;
    }
    if (isset($optVar2)) {
        echo $optVar2;
    }
}

 

I get:

Warning: Missing argument 3 for newFunction()

Warning: Missing argument 4 for newFunction()

and i understand why, but how can I have those optional variables and not get that error.

Link to comment
https://forums.phpfreaks.com/topic/52063-php-function-help/
Share on other sites

By setting them to a default value like this:

 

<?php

function newFunction($var1=NULL, $var2=NULL, $optVar1=NULL, $optVar2=NULL)
{
    echo $var1;
    echo $var2;
    if (isset($optVar1)) {
        echo $optVar1;
    }
    if (isset($optVar2)) {
        echo $optVar1;
    }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/52063-php-function-help/#findComment-256665
Share on other sites

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.