r/PHPhelp • u/jack_skellington • May 04 '20
What is this syntax? Creating a function with what looks like a ternary operator....
This is the code the developer sent in:
function extract_task_data(array $row) : array {
$assignee = explode(", ", $row['assignee']);
// more lines of code
}It is the first line, the creation of the function. What syntax allows a colon and the raw token "array" before the braces? What is it doing?
1
u/TransientVoltage409 May 04 '20 edited May 04 '20
This is a pseudo-code convention to declare the function's return type. I don't think PHP itself uses this syntax though. [Edit: TIL it does now!]
3
May 05 '20
it was primarily done with docblocks and annotations yeah - now the interpreter will throw runtime errors if it returns a different type
1
u/BarnabyJones792 Jun 22 '20
Nice
1
u/nice-scores Jun 22 '20
𝓷𝓲𝓬𝓮 ☜(゚ヮ゚☜)
Nice Leaderboard
1.
u/RepliesNiceat 9932 nices2.
u/Manan175at 7108 nices3.
u/DOCTORDICK8at 7101 nices...
783.
u/BarnabyJones792at 117 nicesI AM A BOT | REPLY !IGNORE AND I WILL STOP REPLYING TO YOUR COMMENTS
10
2
u/Innominate8 May 04 '20
https://www.php.net/manual/en/functions.returning-values.php
It specifies that the function will return an array.