Chủ Nhật, 16 tháng 2, 2014
Tài liệu PHP and MySQL by Example- P6 ppt
Explanation
"
T-!4*6*&4!print_r()!,&!,//*3*5&,.!;55.%,&!,)4'(%&3!51!TRUE7!-5'!+,&!+,B3')%!3$%!
5'3B'3!51!print_r()!*&!,!6,)*,;.%8
9
#$%!$display!6,)*,;.%!+5&3,*&0!3$%!5'3B'3!51!3$%!print_r()!1'&+3*5&U!3$,3!*07!3$%!
+5&3%&30!51!3$%!,)),-8!P%%!D*4')%!E8"=8
=
#$%!reset()!1'&+3*5&!B'30!3$%!*&3%)&,.!,)),-!B5*&3%)!;,+J!,3!3$%!;%4*&&*&4!51!3$%!,)),-8!
#$%!?@?!(,&',.!0'44%030!'0*&4!3$*0!1'&+3*5&!35!)%0%3!3$%!,)),-!B5*&3%)7!;'3!*&!?@?!R!*3!*0!
&53!&%+%00,)-8
!
Figure 8.13. Saving the output from print_r() in a variable. Output from Example 8.9.
The var_dump() Function
The var_dump() function displays the array (or object), the number of elements, and the lengths of each of the string
values. It also provides output with indentation to show the structure of the array or object. (See Chapter 17, “Objects,”
for more on objects.)
Example 8.10 demonstrates the var_dump() function. The output is shown in Figure 8.14.
Format
void var_dump ( mixed expression [, mixed expression [, ]] )
!
Example:
$states=array('CA' => 'California','MT'=>'Montana','NY'=>'New York');
var_dump($states); // Dumps output in a structured format
Example 8.10.
<html>
<head><title>The var_dump() Function</title></head>
<body bgcolor="CCFF99">
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
<pre>
<b>
<?php
1 $colors=array('red','green', 'blue','yellow');
2 $book=array('Title' => 'War and Peace',
'Author' => 'Tolstoy',
'Publisher' => "Oxford University Press",
);
3 var_dump($colors);
var_dump($book);
?>
</b>
</pre>
</body>
</html>
Explanation
"
#$%!&'(%)*+!,)),-7!$colors7!*0!,00*4&%/!,!.*03!51!+5.5)08
9
#$%!,005+*,3*6%!,)),-7!$book7!*0!,00*4&%/!J%-Q6,.'%!B,*)08
=
#$%!var_dump()!1'&+3*5&!3,J%0!3$%!&,(%!51!3$%!,)),-!,0!*30!,)4'(%&37!,&/!
/*0B.,-0!3$%!5'3B'3!;-!1*)03!B)*&3*&4!3$%!&'(;%)!51!%.%(%&30!*&!3$%!,)),-7!*&/%:!
6,.'%7!,&/!3$%&!3$%!&'(;%)!51!+$,),+3%)0!*&!%,+$!51!3$%!,00*4&%/!03)*&4!6,.'%0!
,&/!*30!6,.'%8!P%%!D*4')%!E8"A8
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Figure 8.14. Printing an array with the var_dump() function. Output Example 8.10.
!
8.1.4. Using Loops to Access Array Elements
Loops make it easy to step through an array. The for and while loops are useful for numeric arrays where you can
determine the size, and starting point of the array is usually 0, incremented by 1 for each element. The best way to loop
through an associative array is with the foreach loop, although you can use this loop for numerically indexed arrays
as well.
The for Loop
The for loop can be used to iterate through a numeric array. The initial value of the for loop will be the first index
value of the array, which will be incremented each time through the loop until the end of the array is reached.
Example 8.11 demonstrates how the for loop is used to view each element of an array. The output is displayed in
Figure 8.15.
Example 8.11.
<html>
<head><title>The for Loop</title></head>
<body bgcolor="lightgreen">
<table border="1" bordercolor="black" bgcolor="yellow">
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
<caption>Elements</caption>
<?php
1 $colors=array('red','green', 'blue','yellow');
2 for($i = 0; $i < count($colors); $i++){
3 echo "<tr><td><b>$colors[$i]</b></td></tr>";
}
?>
</table>
</body>
</html>
Explanation
"
#$%!&'(%)*+!,)),-7!$colors7!*0!,00*4&%/!,!.*03!51!+5.5)08
9
#$%!1*)03!,)4'(%&3!35!3$%!for!.55B7!$i = 07!0%30!3$%!*&*3*,.!6,.'%!51!$i!35!07!
2$*+$!)%B)%0%&30!3$%!1*)03!*&/%:!*&!3$%!,)),-8!K1!3$%!*&/%:!6,.'%7!$i7!*0!.%00!3$,&!
3$%!0*`%!51!3$%!,)),-!C)%3')&%/!1)5(!3$%!count()!1'&+3*5&F7!3$%!.55B!;5/-!*0!
%&3%)%/7!,13%)!/*0B.,-*&4!3$%!+5.5)7!3$%!3$*)/!,)4'(%&3!51!3$%!for!.55B!*0!
%:%+'3%/U!3$,3!*07!*&+)%(%&3!3$%!6,.'%!51!$i!;-!"8
=
O,+$!3*(%!3$)5'4$!3$%!.55B7!3$%!&%:3!%.%(%&3!*&!3$%!$colors!,)),-!*0!/*0B.,-%/!
*&!,!3,;.%!+% 8
Figure 8.15. Using the for loop to loop through an array. Output from Example 8.11.
!
!
The while Loop
The while loop can be used to iterate through a numeric array as shown in Examples 8.12 and 8.13.
By setting the initial value to 0, the loop will iterate from the first element of the array (assuming that the array starts at
element zero) until it reaches the end of the array. The count() or sizeof() functions can be used to find the
length of the array.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Example 8.12.
<html>
<head><title>The while Loop</title></head>
<body bgcolor="lightgreen">
<table border='1' bordercolor='black' bgcolor='yellow'>
<caption>Elements</caption>
<?php
1 $colors=array('red','white', 'aqua','yellow');
2 $i = 0;
3 while( $i < count($colors)){
4 echo "<tr bgcolor=$colors[$i]><td><b>$colors[$i]
</b></td></tr>";
5 $i++;
}
?>
</table>
</body>
</html>
Explanation
"
>!&'(%)*+!,)),-!+, %/!$colors!*0!+)%,3%/!,&/!,00*4&%/!03)*&4!6,.'%08
9
H,)*,;.%7!$i7!0%3!35!<7!2* !;%!3$%!*&*3*,.!6,.'%!*&!3$%!.55B7!,&/!2* !'0%/!,0!3$%!,)),-a0!
*&/%:8
=
#$%!count()!1'&+3*5&!)%3')&0!3$%!&'(;%)!51!%.%(%&30!*&!3$%!,)),-8!#$%!while!
%:B)%00*5&!3%030!3$,3!3$%!6,.'%!51!$i!*0!.%00!3$,&!3$%!0*`%!51!3$%!,)),-8
A
#$%!6,.'%!51!$i!2* !;%!'0%/!,0!3$%!*&/%:!6,.'%!51!3$%!,)),-7!$colors8!O,+$!3*(%!3$)5'4$!
3$%!.55B7!3$%!6,.'%!2* !;%!*&+)%(%&3%/!;-!"8!K&!3$*0!%:,(B.%7!3$%!6,.'%!51!3$%!%.%(%&37!
,!+5.5)7!2* !;%!'0%/!,0!3$%!;,+J4)5'&/!+5.5)!15)!3$%!+'))%&3!)527!,&/!,0!3$%!3%:3!
B)*&3%/!2*3$*&!3$%!3,;.%a0!/,3,!+% 8!P%%!D*4')%!E8"V8
R
#$%!6,.'%!51!$i!*0!*&+)%(%&3%/!;-!"8!K1!-5'!15)4%3!35!*&+)%(%&3!$i7!3$%!.55B!2* !45!
15)%6%)!;%+,'0%!$i!2* !,.2,-0!;%!.%00!3$,&!3$%!0*`%!51!3$%!,)),-8
!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Figure 8.16. Using the while loop to iterate through an array. Output from Example 8.12.
Example 8.13.
G5/%!H*%2I!
<html><head><title>Table Colors</table></head>
<body bgcolor="blue">
<table border=1 bordercolor="white" align="center"
cellpadding="2">
<caption><b><font size="+2" color="yellow">Colored
Rows</font></b>
</caption>
<?php
1 $colors=array("orange","lightgreen", "lightblue","yellow");
2 $i=0;
3 while ( $i< 8 ){
// Each time through the loop the index value in the
array
// will be changed, with values 0, 1, 2, 3, 0, 1, 2, 3,
etc.
4 $color=$colors[$i%4];
?>
<tr bgcolor="<?=$color?>">
5 <td><?=$color?></td>
<td><?=$color?></td>
<td><?=$color?></td>
<td><?=$color?></td>
<td><?=$color?></td>
</tr>
<?php
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
6 $i++; // Increment the value of the loop counter
7 }
?>
</body>
</html>
Explanation
"
>&!,)),-!+, %/!$colors!*0!,00*4&%/!15')!+5.5)!6,.'%08
9
#$%!6,)*,;.%7!$i7!*0!*&*3*,.*`%/!35!<8
=
#$%!while!.55B!%6,.',3%0!3$%!%:B)%00*5&!*&!B,)%&3$%0%08!K0!3$%!6,.'%!51!$i!.%00!
3$,&!Eb!K1!*3!*07!3$%!.55B!;5/-!*0!%&3%)%/8
A
#$%!*&/%:!6,.'%!51!3$%!$colors!,)),-!*0!/*6*/%/!;-!A!,&/!3$%!)%(,*&/%)!
C(5/'.'0F!*0!)%B.,+%/!,0!3$%!&%2!*&/%:!6,.'%8
R
#$%!1*)03!3*(%!*&!3$%!.55B!3$%!*&/%:!*0!<8!#$%!6,.'%!$color[0]!*0!"orange"!,&/!
2* !;%!1* %/!*&!3$%!3,;.%!15)!,!)52!51!R!3,;.%!+% 08!P%%!D*4')%!E8"S8
V
#$%!6,.'%!51!$i!*0!*&+)%(%&3%/!;-!"8!X%:3!3*(%!3$)5'4$!3$%!.55B7!$color[1]!*0!
"lightgreen"!,&/!3$,3!+5.5)!2* !1* !,!)52!51!3,;.%!+% 08
S
#$*0!*0!3$%!+.50*&4!;),+%!15)!3$%!while!.55B8
Figure 8.17. The while loop and arrays. Output from Example 8.13.
!
The foreach Loop
The foreach statement is designed to work with both numeric and associative arrays (works only on arrays). The
loop expression consists of the array name, followed by the as keyword, and a user-defined variable that will hold each
successive value of the array as the loop iterates. The expression in the foreach statement can include both the key
and value as shown in Example 8.14. The foreach loop operates on a copy of the original array. If you change the
value of an element of the array, it will only change the copy, not the value in the original.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Format
(Numeric Array)
foreach ($array_name as $value){
do-something with the element's value;
}
foreach($array_name as $index=>$value){
do-something with the element's index and value
}
Example:
$suit=("diamond", "spade", "club", "heart");
foreach ( $suit as $card_type){
echo $card_type . "<br />"; // displays: diamond
}
spade
club
heart
(Associative Array)
foreach ($array_name as $key => $variable){
do-something $key and/or $variable;
}
Example:
$courses=("101A"=>"Intro to CS",
"200B"=>"Data Structures",
"130A"=>"Visual Basic"
);
foreach ( $courses as $number=>$class_name){
echo $number . '=>' . $class_name . "<br />"; // displays keys
// and values
}
Example 8.14.
G5/%!H*%2I!!
<html>
<head><title>The foreach Loop</title></head>
<body bgcolor="lightgreen">
<b>
<?php
1 $colors=array('red','green', 'blue', 'yellow');
2 $employee=array('Name' => 'Jon Doe',
'ID' => '23d4',
'Job Title'=> 'Designer',
'Department'=>'Web Development',
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
);
3 foreach ($colors as $value){// Each value is stored in
$value
echo "$value <br />";
}
echo "<hr>";
4 foreach ($employee as $key => $value){// Associative array
echo "employee[$key] => $value<br />";
}
?>
</b>
</body>
</html>
Explanation
"
>!&'(%)*+!,)),-!51!15')!+5.5)0!*0!/%1*&%/8
9
>&!,005+*,3*6%!,)),-!51!15')!J%-Q6,.'%!B,*)0!*0!/%1*&%/8
=
#$%!foreach!.55B!*0!'0%/!35!*3%),3%!3$)5'4$!%,+$!%.%(%&3!51!3$%!$colors!,)),-8!
#$%!%:B)%00*5&!$colors as $value7!(%,&0I!K&!3$%!$colors!,)),-!,00*4&!3$%!
6,.'%!51!%,+$!%.%(%&3!35!3$%!6,)*,;.%!$value7!5&%!,3!,!3*(%7!'&3*.!3$%!,)),-!
%&/08!O,+$!6,.'%!2* !;%!/*0B.,-%/!*&!3')&8!P%%!D*4')%!E8"E8
A
#$*0!foreach!.55B!*3%),3%0!3$)5'4$!%,+$!J%-Q6,.'%!B,*)!51!,&!,005+*,3*6%!,)),-7!
+, %/!employee8!#$%!,)),-!&,(%!*0!15 52%/!;-!3$%!as!J%-25)/!,&/!,!6,)*,;.%!
35!)%B)%0%&3!3$%!J%-7!+, %/!$key7!15 52%/!;-!3$%!=>!5B%),35)7!,&/!,!6,)*,;.%!35!
)%B)%0%&3!3$%!6,.'%7!+, %/!$value8
Figure 8.18. Looping through an array with the foreach loop. Output from Example 8.14.
!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Modifying a Value by Reference with a foreach Loop
As of PHP 5, you can easily modify an array’s elements by preceding the value after the as keyword with &. This will
assign by reference instead of copying the value; that is, whatever you do to the array while in the loop will change the
original array, not a copy of it.
Example 8.15.
G5/%!H*%2I!
<html>
<head><title>The foreach Loop Changing Values by Reference
</title></head>
<body bgcolor="lightblue">
<b>
<pre>
Original array
<?php
1 $val="hello";
2 $years=array(44, 53, 64, 77);
3 print_r($years);
4 foreach ($years as &$val){// $val is a reference
5 $val += 1900;
echo "$val<br />"; // $val is global in scope
}
6 echo "After foreach: \$val=$val<br />";
?>
<hr>
Array has been changed
<?php
7 print_r($years);
?>
</pre>
</b>
</body>
</html>
Explanation
"
>!6,)*,;.%7!$val7!*0!,00*4&%/!3$%!03)*&47"hello"8
9
>!&'(%)*+!,)),-7!+, %/!$years7!*0!,00*4&%/!15')!&'(;%)08
=
#$%!print_r()!1'&+3*5&!B)*&30!3$%!5)*4*&,.!,)),-8!P%%!D*4')%!E8"c8
A
K&!3$%!foreach!%:B)%00*5&!3$%!6,)*,;.%!$val!*0!'0%/!,0!,!)%1%)%&+%!35!%,+$!%.%(%&3!51!3$%!
$years!,)),-8!#$%!&!B)%+%/*&4!3$%!6,)*,;.%!&,(%!(,J%0!*3!,!)%1%)%&+%8!>&-!+$,&4%0!
(,/%!35!3$%!6,.'%!3$,3!$val!)%1%)%&+%0!2* !+$,&4%!3$%!5)*4*&,.!,)),-8
R
1900!*0!,//%/!35!%,+$!6,.'%!*&!3$%!,)),-!6*,!3$%!)%1%)%&+%7!$val8
V
T%+,'0%!$val!*0!,!4.5;,.!6,)*,;.%!C*8%87!6*0*;.%!3$)5'4$5'3!3$%!B)54),(F7!*30!5)*4*&,.!6,.'%!
*0!+$,&4%/!*&!3$%!foreach!.55B7!,&/!3$%!.,03!6,.'%!,00*4&%/!35!*3!)%(,*&0!,13%)!3$%!.55B!
%:*308
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Đăng ký:
Đăng Nhận xét (Atom)
Không có nhận xét nào:
Đăng nhận xét