Checking for arguments in a function

I am a little versed in functions to understand their use a little more. I ran into a problem that I cannot find the answer to, well, at least I cannot relate this problem.

<?php

// some variables.

$firstName = "foo";
$surname = "bar";

//The function

function myName ($firstName, $surname) {

    echo "Hello $firstName $surname. <br>";

}

// The output.

myName($firstName, $surname);

?>

As you can see if both $ firstName and $ username exist, everything is fine, however, if $ surname does not exist and the script is somewhere else, which leads the information to only verify the existence of $ first_name, then the function will fail.

So my question is how can I check inside the function if there are values ​​for both variables and is there a way to prevent it from crashing in the event, for example, that there is only $ firstName.

+4
1

:

$firstName = "foo";
$surname = "bar";

function myName ($firstName, $surname) {

if(isset($firstName) && isset($surname))
{    echo "Hello $firstName $surname. <br>";
}

else if(isset($firstName))
{    echo "Hello $firstName. <br>";
}

}

myName($firstName, $surname);
+3

Source: https://habr.com/ru/post/1538278/


All Articles