Chủ Nhật, 16 tháng 2, 2014

Tài liệu PHP and MySQL by Example- P8 doc

array(20,25,30,35),
);
2 function cube(&$element,$index){
print $index; // prints 012301230123
$element=$element*$element*$element;
}
?>
<table border='1'><caption><font size='-1'>The <em>
array_walk_recursive()</em> function</font></caption>
<?php
3 array_walk_recursive($numbers,'cube');
for($i=0; $i < 3; $i++){
echo "<tr bgcolor='999FFF'>";
for($j=0; $j<4; $j++){
echo "<td><b>".$numbers[$i][$j] ;
}
echo "</td></tr>";
}
echo "</table>";
?>
</div>
</body>
</html>
Explanation
"
$numbers!)6!+!&%4-/)'!+//+>!'*&(+)&)&3!(0/ !+//+>67
D
?0)6!'+22,+'@!$%&'()*&!(+@-6!+!/-$-/-&'-!(*!(0-!+//+>!,-)&3!5+2@ !*B-/!+ 6!)( 6!$)/6( !
+/3%4-&(!+&.!$key=!50)'0!)6!(0-!)& <!*$!-+'0!-2-4-&(!*$!(0-!+//+>7!;(6!$%&'()*&!)6!(*!
5+2@!(0/*%30!(0-!+//+>!'%,)&3!-+'0!*$!)(6!-2-4-&(67
K
?0-!array_walk_recursive()!$%&'()*&!(+@-6!(0-!+//+>!+6!)(6!$)/6(!+/3%4-&(=!+&.!(0-!
&+4-!*$!(0-!$%&'()*&=!'cube'=!+6!+!6(/)&3!B+2%-!$*/!)(6!6-'*&.!+/3%4-&(7!?0-!cube()!
$%&'()*&!)6!(0-!'+22,+'@!$%&'()*&!(0+(!5)22!,-!+112) !(*!-+'0!-2-4-&(!*$!(0-!(5*A
.)4-&6)*&+2!+//+>=!$numbers7!G !8)3%/-!S7"P7
!
!













Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Figure 9.15. Using a callback function. Output from Example 9.12.
!
9.1.5. Scope
Scope refers to the parts of a program that can see or access a variable; that is, where the variable is visible. Lifetime is
how long the variable exists. There are three types of scope for PHP variables: local, global, and static.
Local variables are visible within a function and their life ends when the function ends. Global variables are visible to a
script, but not normally to a function. They live throughout the run of the script, and die when the script ends. Static
variables are local to a function but retain their value from one function call to another. They die when the script ends.
Let’s look at some examples to see how scope and lifetime work.
Local Scope
By default, all variables declared within functions are local to the function; that is, they are not visible outside the
function and die when the function exits. In computer jargon, these variables are pushed onto a stack when the function
starts and popped off when the function ends.
Example 9.13.
<?php
1 $friend = "Sam"; // Global variable, visible outside
functions
2 function who(){
3 $friend = "Joe"; // Local variable; disappears when
// function ends
print "In the function $friend is your local friend.<br
/>";
}
4 who(); // Function call
5 print "Out of the function, your friend is $friend.<br.>";
?>

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Explanation
"
$friend!)6!+!32*,+2!B+/)+,2-!)&!(0+(!)(!)6!B)6),2-!(*!(0-!4+)&!6'/)1(=!+&.!5)22!.)6+11-+/!
50-&!(0-!6'/)1(!-&.67!;(!5)22!&*(!,-!B)6),2-!5)(0)&!$%&'()*&67
D
?0-!$%&'()*&!'+22 !who()!)6! $)& 7
K
[B-&!(0*%30!(0)6!B+/)+,2-!)6!+26*!'+22 !$friend=!)(!)6!+!.)$$-/-&(!B+/)+,2-7!;(!)6!2*'+2!(*!(0-!
$%&'()*&!+&.!5)22!.)6+11-+/!50-&!(0-!$%&'()*&! -&.67! ;$!(0)6!B+/)+,2-!0+.!&*(!, &!
$)& =!(0-!*/)3)&+2!$friend!5*%2.!6()22!&*(!,-!B)6),2-!0-/-7
N
?0-!$%&'()*&=!who()=!)6!'+22 7!`&'-!)&!(0-!$%&'()*&!(0-!$friend!B+2%-!Sam!)6!*%(!*$!6'*1-!
%&()2!(0-!$%&'()*&!-&.67!Joe!)6!(0-!$/)-&.7
P
`%(6) !(0-!$%&'()*&=!32*,+2!B+/)+,2-!$friend!)6!&*5!)&!6'*1-!+&.!Sam!)6!(0-!$/)-&.7!G !
8)3%/-!S7"L7
!
Figure 9.16. Local scope. Output from Example 9.13.


Global Scope
From within a function you cannot access variables that were declared outside the function unless you pass the variable
to the function as an argument, by reference. If you need to access an external variable within a function without
passing it by reference, then you can use the global statement. By placing the global keyword in front of a variable
name, the variable that was defined outside the function can now be accessed.
Format
global variable_name1, variable_name2, variable_name3 ;
!
Example:
global$salary;

Example 9.14.
<html><head><title>Function Arguments</title></head>
<body bgcolor="lightgreen"><font size="+1" face="arial">
<?php
1 function raise_sal(){
2 global $salary;
3 $salary *= 1.1; // 10 percent raise
}
4 $salary = 50000;
5 raise_sal();
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
?>
6 Congratulations! Your new salary is $<?= $salary?>.<br />";
</body>
</html>
Explanation
"
?0-!$%&'()*&!raise_sal()!)6! $)& 7
D
?0-!@->5*/.=!global=!+22*56!(0)6!$%&'()*&!(*!0+B-!+''-66!(*!(0-!B+/)+,2-=!$salary=!
$)& !*%(6) !(0)6!$%&'()*&7
K
?0-!32*,+2!$salary!)6!,-)&3!4*.)$) !)&!(0-!$%&'()*&7
N
?0-!B+/)+,2-=!$salary=!)6! $)& !*%(6) !(0-!$%&'()*&!+&.!)6!&*(!+B+)2+,2-!(*!(0-!
$%&'()*&=!%&2-66!)(!)6!1+66 !,>!/-$-/-&'-!*/!)6!4+ !32*,+2!5)(0)&!(0-!$%&'()*&7
P
?0-!raise_sal()!$%&'()*&!)6!'+22 7
L
?0-!:?QR!*%(1%(!60*56!(0+(!(0-!6+2+/>!5+6!'0+&3 !)&!(0-!$%&'()*&7!G !8)3%/-!S7 "]7
!
Figure 9.17. Global scope. Output from Example 9.14.


The $GLOBALS() Array
The other way to access a global variable within a function is with the $GLOBALS[] array. It contains all the variables
that have global scope in the script (i.e., any variables declared outside of functions).

Example 9.15.
<html><head><title>Function Arguments</title></head>
<body bgcolor="lightgreen"><font size="+1" face="arial">
<?php
function raise_sal(){
1 $GLOBALS['salary'] *= 1.1;
}
$salary = 50000;

raise_sal();
?>
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
2 Congratulations! Your new salary is $<?=$salary?>.<br />
</body>
</html>

Explanation
"
?0-!$_GLOBALS[]!+//+>!3)B-6!(0-!$%&'()*&!+''-66!(*!B+/)+,2-6! $)& !$/*4!*%(6) !(0-!
$%&'()*&Y!)&!(0)6!'+6-=!$salary7!?0-!&+4-!*$!(0-!B+/)+,2-!,-'*4-6!(0-!@->!)&!(0-!
$_GLOBALS[]!+66*')+()B-!+//+>7!?0)6!-<+412-!)6!-<+'(2>!2)@-![<+412-!S7"N!-<'-1(=!)&!(0+(!
-<+412-=!(0-!global!@->5*/.!5+6!%6 !(*!4+@-!(0-!B+/)+,2-!$salary!+B+)2+,2-!(*!(0-!
$%&'()*&7
D
?0-!:?QR!*%(1%(!60*56!(0+(!(0-!6+2+/>!5+6!'0+&3 !)&!(0-!$%&'()*&7!G !8)3%/-!S7 "^7
!
Figure 9.18. Using the $GLOBALS array. Output from Example 9.15.


Static Variables
The variables declared within functions normally disappear when the function ends. They are local to the function.
They are created when the function is called and die when it ends. A static variable is local to the function, meaning it is
not visible outside of the function, but it does not die when the function ends. Once initialized a static variable will not
lose its value between function calls. It “remembers” the value it held from one call to the next.
Example 9.16.
<?php
function trackme(){
1 static $count=0;
2 $count++;
echo "You have been here $count times.\n<br />";
}
3 trackme();
trackme();
trackme();
?>

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Explanation
"
V>!4+@)&3!(0-!B+/)+,2-!6(+()'=!)(!5)22!/-(+)&!)(6!B+2%-!-B-&!+$(-/!( 0-! $%&'()*&!-<)(67![+'0!
()4-!trackme()!)6!'+22 =!(0-!'*%&(!5)22!,-!)&'/-4-&( !,>!"7!?0-!B+/)+,2-=!$count=!)6!
*&2>!)&)()+2)W !(*!W-/*!(0-!$)/6(!()4-!(0-!$%&'()*&!)6!'+22 7
D
?0-!B+2%-!*$!$count!)6!)&'/-4-&( !,>!"7
K
?0-!$%&'()*&!trackme()!)6!'+22 !(0/ !()4-67![+'0!()4-!)(!)6!'+22 =!(0-!B+2%-!*$!(0-!
'*%&(-/!)6!)&'/-4-&( !+&.!.)612+> 7!G !8)3%/-!S7"S7

Figure 9.19. Static variables. Output from Example 9.16.


Example 9.17.
U* !E)-5T!
<html><head><title>Function Arguments</title></head>
<body bgcolor="lightgreen"><font face="verdana">
<?php
1 function increase_font($size){
2 static $total=0; // The value of $total will persist
// between calls
3 $newfont= $size++ ;
4 $total += $newfont; // Keep a running total
?>
5 <font size='<?=+$newfont?>'>bigger<br />
<?php
6 if ($total > 10){
print "+$total: Too big.<br />";
exit;
}
}
7 for ($n=0; $n<=10; $n++){
increase_font($n);
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
}
?>
</body>
</html>
Explanation
"
?0-!$%&'()*&!increase_font()!)6! $)& 7!;(!0+6!1+/+4-(-/=!$size=!50)'0!/-1/-6-&(6!(0-!
$*&(!6)W-!$*/!(0-!B-/.+&+!$*&(!2)6( !+,*B-7
D
?0-!static!B+/)+,2-!$total!)6! $)& !+&.!)6!+66)3& !+&!)&)()+2!B+2%-!*$!Z7!?0)6!B+/)+,2-!
5)22!&*(!$*/3-(!)(6!B+2%-!+$(-/!(0-!$%&'()*&!-<)(67!G(+()'!B+/)+,2-6!/-(+)&!(0-)/!B+2%-!$/*4!
$%&'()*&!'+22!(*!$%&'()*&!'+227
K
?0-!B+/)+,2-=!$newfont=!)6!)&'/-+6 !,>!"!50-&!(0-!$%&'()*&!)6!'+22 7
N
?0-!B+2%-!*$!B+/)+,2-=!$newfont=!)6!+ !(*!$total. $total!)6!6(+()'Y!(0+(!)6=!)(!/-(+)&6!
)(6!B+2%-!$/*4!(0-!2+6(!$%&'()*&!'+227!`(0-/5)6-=!)(!5*%2.!,-!6-(!,+'@!(*!W-/*!-+'0!()4-!(0-!
$%&'()*&!)6!'+22 7
P
?0-!B+2%-!*$!(0-!&-5!$*&(!6)W-!)6!+66)3& 7
L
C0-&!(0-!+''%4%2+( !(*(+2!/-+'0-6!+!B+2%-!3/-+(-/!(0+&!"Z=!(0-!6'/)1(!-<)(67
]
[+'0!()4-!(0/*%30!(0-!for!2**1=!(0-!$%&'()*&=!increase_font()!)6!'+22 =!'+%6)&3!(0-!
$*&(!(*!,-!)&'/-+6 !,>!"!1*)&(!6)W-7!G !8)3%/-!S7DZ7
!
Figure 9.20. Static variables. Output from Example 9.17.



Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
9.1.6. Nesting Functions
PHP supports nesting functions. A nested function is defined and called from within another function. The outer
function, also called the parent function, has to be invoked for the nested function to become available to the program.
Nested functions are rarely used.
Format
function OuterFunction(){
function NestedFunction(){
/*OuterFunction() must be called before
NestedFunction()
is available.*/
statements;
}
}
PHP functions can also be nested inside other statement blocks, such as conditional statements. As with nested
functions, such functions will only be defined when that block of code has been executed.
Example 9.18.
<?php
1 function outer ($a, $b){
print "Greetings from outer()\n<br />";
2 function square($x) { // Nested function
print "Greetings from square()\n<br />";
3 return $x * $x;
}
4 return square($a) + square($b);
}
5 $sum=outer(5,8); // Call to outer()
echo "The sum of the squares is: $sum\n<br";
6 $squared=square(5);
print "5 squared is: $squared.\n<br />";
?>
Explanation
"
?0-!$%&'()*&!outer()!5)22!,-!(0-!1+/-&(!$%&'()*&!$*/!(0-!$%&'()*&!&-6( !5)(0)&!
)(7
D
?0)6!)6!(0-! '2+/+()*&!$*/!(0-!&-6( !$%&'()*&!square()7!V-'+%6-!)(!)6!&-6( !)&!
(0-!outer()!$%&'()*&=!)(!5)22!&*(!,-! $)& !%&()2!outer()!0+6!, &!'+22 7
K
?0-!/-(%/&!B+2%-!$/*4!(0)6!&-6( !$%&'()*&!)6!(0-!6_%+/-!*$!)(6!1+/+4-(-/67
N
?0-!B+2%-!/-(%/& !5)22!,-!(0-!6%4!*$!$a!+&.!$b!+$(-/!(0-!&-6( !square()!
$%&'()*&!0+6!6_%+/ !(0-4!,*(07
P
?0-!1+/-&(!$%&'()*&=!'+22 !outer()=!)6!'+22 =!(0-!(5*!&%4,-/6!+/-!1+66 =!+&.!
(0-!6%4!*$!(0-)/!6_%+/ !B+2%-6!)6!/-(%/& 7!`&'-!outer()!)6!'+22 !(0-!&-6( !
$%&'()*&!5)22!,-! $)& 7!G !8)3%/-!S7D"7
L
d%6(!2)@-!+&>!*(0-/!$%&'()*&=!(0-!&-6( ! $%&'()*&=!square()=!)6!+B+)2+,2-!*&'-!)(!
)6! $)& 7!;(!)6!&*(!+B+)2+,2-!%&()2!(0-!outer()!$%&'()*&!0+6!, &!'+22 7!G !
8)3%/-!S7DD7
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
)6! $)& 7!;(!)6!&*(!+B+)2+,2-!%&()2!(0-!outer()!$%&'()*&!0+6!, &!'+22 7!G !
8)3%/-!S7DD7
!
Figure 9.21. Using nested functions.

Figure 9.22. Output from Example 9.18.

Once the outer or parent function has been executed, the nested function is defined and accessible from anywhere
within the current program just like any other function. You can only execute the parent function once if it contains
nested functions; otherwise, PHP will generate a fatal error as shown in Figure 9.23.
Example 9.19.
<?php
function outer ($a, $b){
print "Greetings from outer()\n<br />";
function square($x){ // Nested function
print "Greetings from square()\n<br />";
return $x * $x;
}
return square($a) + square($b);
}
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
$sum=outer(5,8);
echo "The sum of the squares is: $sum\n<br";
$sum=outer(10,2); // Wrong! Will cause square() to be redeclared
?>
Figure 9.23. Output from Example 9.19.
!

9.1.7. Recursive Functions
A recursive function is a function that calls itself. Recursive functions are often used to handle certain types of
mathematical problems; traverse directories, linked lists, and binary trees; crack passwords; create anagrams, and magic
squares; and so on. If a task is a smaller version of an original base task, then the problem can be solved by writing a
recursive function. When you first encounter recursion, it might seem a little confusing, like being in a house of mirrors.
When a function calls itself, the program starts up the same function again, executes the function statements, and when
it returns, picks up where it left off in the function that called it. The recursion can go on indefinitely, so you must be
careful to create a condition that, at some point, stops it.
An example often used to describe recursion can be demonstrated with a function to produce a Fibonacci number (see
Example 9.20). What is that? Well before getting started, read this little bit of history, if you have the time or interest.
In the beginnning of the 13th century an Italian mathemetician, Leonardo Fibonacci, came up with a formula, called the
Fibonacci sequence, to solve the following problem presented at a mathematical competition in Pisa: How many rabbits
would be produced in a year if, beginning with a single pair of rabbits, every month each pair reproduces a new pair of
rabbits, which become productive when they are one month old, and none of them die, and so on?
Fibonacci came up with a formula, named after himself, to answer the rabbit question. The Fibonacci sequence
normally starts with 0 and 1, and then produces the next Fibonacci number by adding the two previous Fibonacci
numbers together: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946 . . .
Thus, to get the next value after 21, add 13 to 21 resulting in the next Fibonacci number, which is 34. So the number of
pairs of rabbits at the start of each month is 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on.
Example 9.20.
U* !E)-5T!
<html><head><title>Fibonacci Series</title></head>
<body>
<div align="center">
<?php
1 $count=0;
2 function fib($num){
/* Find the Fibonacci value of a number */
global $count;
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

Không có nhận xét nào:

Đăng nhận xét