How to use mixed parameter type in my own functions?

FYI mixed is not a Type. (Refer to the Documentation). It is only a pseudo type: a Hint that any Type might be passed to or returned from a Method... or perhaps for a variable which was loosely typed as mixed for any reason...

Unlike in C#, what you are trying to achieve might be tricky in PHP especially with strict_types set to true.

However, you can achieve a nearly similar effect without Strict Typing - in which case your Methods may accept any Type so long as you don't supply any Type Hints. Though For C# Programmers, that's bad - yet, that is the juice of PHP.


To indicate that the function also accepts null the ?-operator works for this also as mentioned on http://php.net/manual/de/functions.returning-values.php#functions.returning-values.type-declaration

But this is only usable if the code hasn't to be backward compatible because this feature is only available on PHP >= 7.1 As you can see on https://wiki.php.net/rfc/mixed-typehint there as RFC for adding mixed for type hinting. So actually there seem to be no chance to defined a correct type hint for parameters that accept more the one type.

So a phpDoc comment could be a solution (and it's also backward compatible). Example:

/**
* Functiondescription
* @author FSharpN00b
* @param mixed $s
* @return mixed
**/
function test ($s){
    return $s;
}

UPDATE 2021

Mixed type has been introduced in PHP8. It can be used exactly as shown in the question.