PHP return Statement
Description
PHP return statement immediately terminates the execution of a function when it is called from within that function.
This function is also used to terminate the execution of an eval() function or script file.
If this function is called from a global scope, the function stops the execution of the current script. If the current script file was included using include() or required(), then control goes back to the calling file.
Syntax:
return expression
Where expression is any valid expression. Note that, since the return is not a function but a language construct, using parenthesis is not required.
Return values
Condition | Return value |
---|---|
If called from within a function | Returns the argument of the function. |
If the current script file was included using include() or required() | Value given to return(). |
If no parameter is supplied | NULL is returned. |
Example:
Code of p.php
<?php
include("q.php");
echo "p";
?>
Code of q.php
<?php
echo "q";
return;
?>
See also
Previous: declare statement
Next: include and require
PHP: Tips of the Day
How to Sort Multi-dimensional Array by Value?
Try a usort, If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:
Example:
function sortByOrder($a, $b) { return $a['order'] - $b['order']; } usort($myArray, 'sortByOrder');
Starting in PHP 5.3, you can use an anonymous function:
usort($myArray, function($a, $b) { return $a['order'] - $b['order']; });
And finally with PHP 7 you can use the spaceship operator:
usort($myArray, function($a, $b) { return $a['order'] <=> $b['order']; });
To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.
usort($myArray, function($a, $b) { $retval = $a['order'] <=> $b['order']; if ($retval == 0) { $retval = $a['suborder'] <=> $b['suborder']; if ($retval == 0) { $retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder']; } } return $retval; });
If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual
Ref : https://bit.ly/3i77vCC
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework