Lets say we have function that gets 5 variables.
function func($var1, $var2, $var3, $var4, $var5 )
{
if ($var1==1)
{
work with $var2, $var3
}
if ($var1==2)
{
work with $var4, $var5
}
}
For example, we want to call this function like that: func(1,$var2, $var3) (without unused variables) or like that func(2,$var4, $var5).
Is it possible? How to send exact variables to php function?
UPDATE
Based on @marcus’s answer I modified my function.
<?
function GenerateTopNav($current, $lang, $db)
{
$result=$db->query("SELECT `id`, `parent`, $lang FROM `nav` WHERE `menu`='1'");
while ($row=$result->fetch_object()){
echo '<a ';
if($row->id==$current)
echo 'class="active"';
echo 'href="index.php?id='.$row->id.'">'.$row->$lang.'</a> | ';
}
function GenerateLeftNav($parent, $level, $lang, $db){
$q = $db->query("SELECT `id`, `$lang` AS name FROM nav WHERE parent = '$parent' AND `menu`='2'");
if($level > 0 && $q->num_rows > 0){
echo "\n<ul>\n";
}
while($row=$q->fetch_object()){
echo "<li>";
echo '<a href="?page=' . $row->id . '">' . $row->name . '</a>';
//display this level's children
GenerateLeftNav($row->id, $level+1, $lang, $db);
echo "</li>\n\n";
}
if($level > 0 && $q->num_rows > 0){
echo "</ul>\n";
}
}
}
?>
Why do you even need to specify 4 variables? You’re basically saying specify a type and then 2 input variables for that type, so if there will always be 3 variables, just do the follow:
You really don’t need to spam your function with an excess number of variables that won’t get used.
To be more clear: the variables don’t need to be named appropriately, you just need to comment that if type = 1, these two variables mean these two things, and if type = 2, these two variables mean these two things and process both types accordingly.