<?php
function array() {
return array();
}
?>
The following function won't work. I get this error:
Message : syntax error, unexpected T_ARRAY, expecting T_STRING or '('
You can't name a function array
. It's reserved by the language.
Array
is a language construct (in other words it's not a function, it's a command to create a data structure). If it were simply a function you could override it but language constructs are reserved because they do specific things.
You can find the complete list of reserved words here.
Here is a small example of function in PHP
function getFullName($firstname,$lastname){
return $firstname.' '.$lastname;
}
echo getFullName('Abhik','Chakraborty');
The above will out put Abhik Chakraborty.
So the user defined functions can not be reserved key words as mentioned by others. So when you choose a function name make sure its not one of them.
The arguments in the functions must be in the function signature as
function getFullName($firstname,$lastname)
The function may or may not return any data depending on how you want to implement it. If we change the above function as
function getFullName($firstname,$lastname){
echo $firstname.' '.$lastname;
}
And then do something like
getFullName('Abhik','Chakraborty');
this will out put as
Abhik Chakraborty.
You can learn more here http://www.php.net/manual/en/language.functions.php
Your question is not clear first. Secondly you have used a language reserved construct word. if you mean that you would like to use a function which returns php array.
you can try some thing like
function returnArray($assocArray = array()) {
return $assocArray;
}
var_dump(returnArray(array('A'=> 1, 'B'=>2, 'C'=>3)));
or like this:
function returnArray($assocArray = array('A'=> 'imik', 'B'=>2, 'C'=>3)) {
return $assocArray;
}
foreach (returnArray() as $key=>$value){
echo " $key : $value <br/>";
}