Lets assume I have the following function:
<?php
function MyFunction($color="green", $type="wood", $shape="circle")
{
echo "Its a ", $color , " " , $shape, ", made of " , $type , ".";
}
MyFunction(); // prints Its a green circle, made of wood.
?>
All three parameters in the function are optional parameters.
I know I can skip a parameter by typing null in its parameter slot. For example, to change wood to plastic, I would have to type in:
MyFunction(null,"plastic");
How can I call the same function and parse to the function which variable I want to change without using something fancy like an array?
For example, if we take Powershell, the code would be like this:
function MyFunction()
{
[CmdletBinding()]
param (
[Parameter(Mandatory=$False)][string]color=green,
[Parameter(Mandatory=$False)][string]type=wood,
[Parameter(Mandatory=$False)][string]circle=circle
)
write-host "Its a" $color $shape", made out of" $type"."
}
In Powershell, I can call the function in two ways.
MyFunction "red", "plastic", "square"
or
MyFunction -type "plastic"
How can I achieve the second way in php? For example, lets assume the following code is valid:
MyFunction ($type="plastic");