PHP Declare Statement
Description
In PHP declare construct is used to set execution directives for a block of code. At present two directives are recognized ticks and encoding.
Syntax:
declare (directive) statement
The following table describes two directives currently supported.
Directive | Description |
---|---|
ticks | A tick is an event. While the format of specifying the tick directive is tick=N, where N is an integer. The tick event occurs for every N statements (following the declare). Usually, condition expressions and argument expressions are excluded from being executed. Register_tick_function() is used to specify each event(s) that occur on each tick. Remember that this directive is deprecated in PHP5.3. |
encoding | The encoding directive specifies a script's encoding. Usage of this detective is decal re(encoding="EncodingType") where EncodingType is a encoding type like ISO-8859-1. This directive can be used only if PHP is compiled with --enable-zend-multibyte. You can use phpinfo() to know whether a PHP installation |
Example of PHP declare statement using tick directive
<?php
declare(ticks=5);
// the following function is called on each tick event
function w3r_tick()
{
echo "w3r_tick() called<br>";
}
register_tick_function('w3r_tick');
$a = 5;
if ($a > 0)
{
$a += 2;
print($a);
}
?>
View the example in the browser
Previous: switch statement
Next: return statement
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