r/PHPhelp • • 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?

8 Upvotes

7 comments sorted by

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.

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

u/[deleted] 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/RepliesNice at 9932 nices

2. u/Manan175 at 7108 nices

3. u/DOCTORDICK8 at 7101 nices

...

783. u/BarnabyJones792 at 117 nices


I AM A BOT | REPLY !IGNORE AND I WILL STOP REPLYING TO YOUR COMMENTS

10

u/[deleted] May 04 '20 edited May 04 '20

[removed] — view removed comment