Using return in ternary operator

前端 未结 7 799
無奈伤痛
無奈伤痛 2020-12-03 17:46

I\'m trying to use return in a ternary operator, but receive an error:

Parse error: syntax error, unexpected T_RETURN 

Here\'s the code:

相关标签:
7条回答
  • 2020-12-03 18:16

    Plop,

    if you want modify your return with ternary ?

    it's totally possible.

    In this exemple, i have a function with array in parameters. This exemple function is used to parse users array. On return i have an array with user id and user username. But what happens if I do not have any users?

    <?php
    
    public funtion parseUserTable(array $users) {
       $results = [];
       foreach ($users as $user) {
          $results[$users['id']] = $user['username'];
       }
      return $results ?: array('false', $e, "No users on table."); // Ternary operator.
    }
    

    Sorry for my bad english, i'm french user haha.

    N-D.

    0 讨论(0)
提交回复
热议问题