Jump to: navigation, search

Php declare

From w3cyberlearnings

Contents

declare

declare uses to set execution directives for a block of code.

Syntax

register_tick_function(functionCall);
declare(directive);
statement

Note

This function may not be available in PHP 6.

Example 1

<?php

function myfunc() {
	print "In tick func<br/>";
}

register_tick_function("myfunc");
declare(ticks=4);

for ($i = 0; $i < 10; ++$i) {
	echo " {$i} Hello<br/>";
}

?>

Output

0 Hello
In tick func
1 Hello
2 Hello
In tick func
3 Hello
4 Hello
In tick func
5 Hello
6 Hello
In tick func
7 Hello
8 Hello
In tick func
9 Hello
In tick func 

Example 2

<?php

function myfunc() {
	print "In tick func<br/>";
}

register_tick_function("myfunc");
declare(ticks=3);

for ($i = 0; $i < 10; ++$i) {
	echo " {$i} Hello<br/>";
}

?>

Output

0 Hello
In tick func
1 Hello
In tick func
2 Hello
3 Hello
In tick func
4 Hello
In tick func
5 Hello
6 Hello
In tick func
7 Hello
In tick func
8 Hello
9 Hello
In tick func
In tick func

Example 3: set ticks=2, and called the if statement

<?php

declare(ticks=2);

// A function called on each tick event
function tick_function() {
	echo "tick_function() called<br/>";
}

register_tick_function('tick_function');

$a = 1;

if ($a > 0) {
	$a += 2;
	echo $a;
}
?>

Output

tick_function() called
tick_function() called
tick_function() called
3tick_function() called
tick_function() called
tick_function() called
tick_function() called

Related Links


Navigation
Web
SQL
MISC
References