21
(Introduction To PHP) PHP (Professional Home Page) server side scripting language กก HTML ก client server ก C , Java Perl ก Rasmus Lerdorf code HTML ก php script กกก กก PHP ก ก Object-Oriented ก PHP ก scripting language PHP scripting language กก Web Application language กPHP build-in function กกก PHP Version 6 ก PHP server- side script PHP code PHP code ก ก PHP code HTML code กก HTML PHP 1

ก (Introduction To PHP)athitha/321370/php... · 2010. 3. 24. · ก PHP - 3 1.2.1 Operator ! &˘o$M operator ! &˘o$M ˜?!ก6o˚M˛+>M˛+˜˝;˜ˇˆ ˘o$MN˝0M˛4กM Operator

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

  • �����

    ���������ก������� (Introduction To PHP)

    PHP (Professional Home Page) ��� server side scripting language ��� ก������ !"!#��ก$%&'()����*�+,�-&-�.,!/ #'+0�/�+"!!1234���5�6� HTML 0-+:;/ก1)����*�+!�,0%+"!J?*� U)�=$�4]� ��4 0����D,.ก,.���; -�+ PHP code ,!� HTML code >%/ #'+M/�+�?ก��กL�;%&�)�&M -� 0- >;��� 0- &�+ HTML ,!�0- >;���0- &�+ PHP

    1

  • PHP

    2

    1.1 �����������������ก�� 1.1.1 � ��!!�"#$%&% PHP

    5�6�2?��O2? (PHP) �?,JUก2$�N6J?*:O/กL�;%&�)�&M>%/ 4 ,))��� a. L�;/�%/ . �O-

  • ����������ก���� PHP

    -

    3

    1.2.1 Operator �!&�o$M operator �!&�o$M �?!1ก6o�M�+>�M�+���;��� �o$MN�0M�4�กM$

    Operator 78�" �5;"�=%#

    + ก��) ก $a + $b

    - ก��!) $a - $b

    * ก���

  • PHP

    4

    1.2.3 Operator ก@%:�A>=%

    ����*�+;��.�J-�ก1) (=) ��� operator กL�;%�-�2�(W�

    $total = 0;

    0����D�-�>%/��� w$totalM1(+�-����N<.4 x

    1.2.4 ก%�C=#""ก>=%D%กก%�ก@%:�A>=% ก��:O/ Operator กL�;%�-� 0-+��ก�-�J1(+;�%�!/�.ก1) operator ��* 0����D�&?.�L�01*+:!1ก6o�?(

    $b = 6 + ($a=5);

    $b >%/�1)ก��M1(+�-���� 11 ���@.��L�01*+กL�;%�-�J1(+;�% ��� �-�J?*กL�;%:;/ก1) operand %/�=/�. +�!U)0����D:O/�2$*�$2#4.-�. ='*+JL�+��;��� $3?ก��J�+�!&�o$M ก��"0� Operator กL�;%�-� �O-

    $a +=s;

    �J?.)�J-�ก1)ก���&?. -� $a = $a + s;

    Operator ก%�97� �E��!�E=%ก5!

    += $a+=$b $a=$a + $b

    -= $a-=$b $a=$a - $b

    * = $a*=$b $a=$a * $b

    /= $a/=$b $a=$a / $b

    %= $a%=$b $a=$a % $b

    .= $a.=$b $a=$a.$b

    M���+ 1.2 ก��"0� operator กL�;%�-�

  • ����������ก���� PHP

    -

    5

    1.2.4 ก%��F����GHก%�GA ก="��GH:G5#

    operator ก���2$*� (++) ,!� ก��!% (- -) ก-�,!�;!1+ �!/�.ก1) += ,!� -= operator ก���2$*� J1(+;�% �? 2 "!ก��J) ��� �2$*�,!�กL�;%�-� �O-

    $ a; echo ++$a;

    )��J1%J?* 2 :O/ operator ก���2$*� ���ก���2$*��-� a �?ก1 M1(+,M-J?,�ก ='*+"!:ก��,0%+��ก&�+�-�#�

    �2$*�&'(�!. (&/�01+�กM �-�J?*�กU): $a �?ก����!?*.,�!+,!�>�-:O-ก��0-+��ก�-�%/ .$a+1 ) D/� ++ �.

  • PHP

    6

    1.2.6 Operator �����!�E��!

    :O/���?.)�J?.) 2 �-�$2#4 0-+��ก�-�J�+M��ก���� true ;��� false &'(ก1)"!!1234&�+ก�����?.)�J?.)

    1.2.7 Operator �E=%ก5!

    operator �J-�ก1) (= =) :O/:ก��J%0�) �-��J-�ก1&�+ 2 �-� �O- $a = = $b

    #���� true D/��-��J-�ก1 ;��� false D/�>�-�J-�ก1 0$*+?(JL�:;/01)0ก1) = (operator กL�;%�-� )@%.ก��JL�+�>�-�?� ��"$%2!�% ,M-#�>�-:;/"!!1234� ��M/�+ก�� @%.J1* >� �-�>�-:O-N<.4 >%/�1)ก�������$��� true ,!� N<.4 >%/�1)ก�������$��� false M1 �.-�+�O-

    $a=5; $b=7;

    D/�J%0�) $a = $b "!!1234 ��� true ��*�+#�ก �-�&�+ $a = $b ��� ก��กL�;%�-�:;/ก1)=/�.���:ก�o??( ��� 7 �-�?(>�-:O-N<.4 %1+1(#'+>%/�1)ก�������$��� true D/�M1(+:#J%0�)%/ . $a = = $b ='*+>%/�-���� false JL�:;/M/�+M� #0�)� ��"$%2!�% J�+M��ก� J?*JL�>%/.�ก %1+1(� ��?ก��M� #0�)ก��:O/ 2 operator?(

    1.2.8 Operator �����!�E��!"8��

    PHP 01)0I operator ���?.)�J?.);!�.M1 M��J?*>%/0�I� operator ���?.)�J?.):M���+ 1.3

    * :�%��:�N operator ��ก!1ก6o4 (= = =) #�0-+ true �V2�� D/� 2 operator �J-�ก1 ,!��������5J�%?. ก1

    Operator 78�" ก%�97�

    == �J-�ก1) $a = = $b

    === ��ก!1ก6o4 $a = = = $b

    != >�-�J-�ก1) $a!=$b

    >�-�J-�ก1) $a$b

    < /�.ก -� $a ��กก -� $a>$b

    =$b

    M���+ 1.3 Operator ���?.)�J?.)

  • ����������ก���� PHP

    -

    7

    1.2.9 Operator ���กH

    operator M��ก� :O/� � "!!1234&�+�+�*�>&J�+M��ก� M1 �.-�+ �O- �?� ��0:# �-�&�+ M1 ,�� $a ��; -�+ 0 D'+ 100 M/�+J%0�)�+�*�>& $a>=0 ,!� $a=0 && $a�-� �OR) ,!� NOT @%.OI%&�+ operator M��ก� ,!� 0�I�:M���+ 1.4

    M���+ 1.4 Operator M��ก�

    * ;��.�;MI and ,!� or �?��%1)M*L�ก -� && ,!� ||

    1.2.10 Bitwise Operator

    Bitwise Operator JL�:;/0����D�$)1M$ ก1) �!L� �MU� ���OI%&�+)$MJ?*L��0� )�+��1(+��##�2)ก��:O/ operator �;!-�?(>�-��ก: PHP ,M->%/0�I� bitwise operator :M���+ 1.5

    Operator 78�" ก%�97� SGG5FTU

    & bitwise AND $a & $b OI%)$M: $a ,!� $b ���OI%:"!!1234

    | bitwise OR $a |$b OI%)$M: $a ;��� $b ���OI%:"!!1234

    w bitwise NOT w $a OI%)$M: $a >�-���OI%:"!!1234

    ^ bitwise XOR $a^$b OI%)$M: $a ;��� $b ,M->�-:O-J1(+�

  • PHP

    8

    1.2.11 Operator "8�� �ก#�ก?( operator ��*J?*�?ก��:O/+� >%/,ก- #I!5�� (,) :O/,)-+��ก$ ��M4&�+]T+ก4O1 ,!�

    ��.+�;- .&/�� :O/ instance ก1) class ,!� �&/�D'+0��O$ก&�+ class • Array operator ([]):O/�&/�D'+ array

    �? operator �?ก 3 M1 J?*#�ก!-� D'+M-�>�

    1.2.11.1 Ternary operator ��� Operator JL�+� $3?�%?. ก1):5�6� C �?�@%C5�# ? X�%��>=%��Y�D��#DHE@%��#��6 : ��=X�%[�=D��#DHE@%��#��6 Ternary operator �;���ก1) $2#4:���@.��L�01*+ if-else

    �5;"�=%#

    ($grade > 50 ? wPassedx : wFailedx)

    $2#4?(�����$�ก�%1กN'ก6���� wPassedx ;��� wFailedx

    1.2.11.2 Error Suppression Operator

    Error suppression operator (@) :O/;/�$2#4J?*0�/�+;����?�-� D/��?error#�>�-,0%+error:%��ก���O-

    $ = @(57/0)

    1.2.11.3 Execution Operator

    Execution operator ����

  • ����������ก���� PHP

    -

    9

    ;��� �J-�ก1) ��))�$)1M$ก�� Windows

    $out = ` dir C:`; echo wx.$out. wx;

    � ��4O1*�;!-�?(#��กU)��.+�>%���J��? ,!� �กU): $out #�ก1(0����D0-+:;/ browser ;���M$%M-�: $3?��*M-�>�

    1.2.11.3 ก%�97� operator >@%�;_SG�;��"#`"�U� ���*��

  • PHP

    10

    number_format($totalamount, 2) ; echo " � ��+$) ก5�6?: ". $totalamount." \n";

    M���L�01*+M1 �.-�+ :O/ operator ก���2$*� (+) ,!� ก����-:O-����5J&/��

    ก��:O/ settype() :;/0-+"-�M1 ,��J?*M/�+ก����!?*.����5J ,!� string �กU)����5J:;�-0L�;�1)M1 ,��J?*0-+�&/�>�

    �5;"�=%#>@%C5�#

    $a = 56; echo gettype ($a). w
    x; settype ($a, wdoublex); echo gettype ($a). w
    x;

    ���*� ��?.ก gettype () ��1(+,�ก ����5J&/��

    &/��%/��!?*.��� double

  • ����������ก���� PHP

    -

    11

    PHP �?]T+ก4O1�V2�� #L� ;'*+ ,M-]T+ก4O1L�M1 ,��:W�� ��ก$ ��M4 ,!�0-+��ก true ;��� false ]T+ก4O1�;!-�?( >%/,ก-

    • is_array() • is_double(), is_float(), is_real() ( J1(+;�%���]T+ก4O1�%?. ก1 )

    • is_long(), is_int(), is_integer() ( J1(+;�%���]T+ก4O1�%?. ก1 )

    • is_string()

    • is_object()

    1.3.2 EAC"!CX%�H�5;���

    PHP �?]T+ก4O1J%0�)0D��&�+M1 ,��;!�.]T+ก4O1�O- isset () :O/O�*�M1 ,����� ��ก$ ��M4 ,!�0-+ true D/�M1 ,���?�.

  • PHP

    12

    1.3.3 ก%���G#�5;��� ก��,�!+����5JM1 ,�� 0����D:O/ 3 ]T+ก4O1

    • int intval (mixed var);

    • double doubleval (mixed var);

    • string strval (mixed var);

    1.4 ��H��>>@%C5�# if

    ���@.��L�01*+ if :O/:ก��M1%0$:# @%.M/�+:;/ �+�*�>&ก1)���@.��L�01*+ if :O/+� D/��+�*�>&���#�$+ ก!I-��L�01*+ (block code) #�>%/�1)ก������ !"! �+�*�>&:���@.��L�01*+ if M/�+�.�-�?��.ก�� ��##�)�ก -� wJ-�>�->%/JL���.ก��01*+=�(�#�ก;/�ก-�x

    if ($ total == 0) echo w J-�>�->%/JL���.ก��01*+=�(�#�ก;/�ก-�
    x;

    �+�*�>& $total == 0 ���#�$+ D/� $total �J-�ก1)N<.4 D/� $total >�-�J-�ก1)N<.4 �+�*�>&?(��� false ���*� �+�*�>&��� true ���@.��L�01*+ echo #�>%/�1)ก������ !"!

    1.5 กGN=�>@%C5�# (code block)

    D/��?���@.��L�01*+��กก -� 1 OI% J?*M/�+���� !"!5�.:���@.��L�01*+1( 0����D#1%���@.� %/ .ก��!/��%/ .����*�+;��.�กก� ({ })

    if ($ total == 0) { echo wx; echo w Computer Science
    x; echo wx; }

    1.6 ��H��>>@%C5�# else

    @%.0- :;k- ก��M1%0$:# >�-M/�+ก���2?.+ 1 ก��ก��JL� ,M-M/�+ก��OI%ก��ก��JL�J?*���>�>%/0L�;�1)ก��JL�+����@.��L�01*+ else .��:;/กL�;%ก��ก��JL�J�+�!��ก�2�*�L���:O/ ���*��+�*�>&

  • ����������ก���� PHP

    -

    13

    :���@.� if ��� false M��M1 �.-�+ ���*�ก���M��!�-�?��.ก�� :J�+M�+&/�� D/��?ก��01*+=�(�#�$+� ��?ก��,0%+��.!���?.%01*+=�(�

    if ($ total== 0) { echo w J-�>�->%/JL���.ก��01*+=�(�#�ก;/�ก-�
    x; } else { echo $soap." ก/�
    "; echo $shampoo." & %
    "; echo $conditioner." & %
    "; }

    ���@.��L�01*+ if 0����DL�>�0�/�+ ก��) ก��J�+M��ก�=1)=/�>%/ M1 �.-�+�L�01*+M-�>� >�-����2?.+ก��0�I�M���+�*�>& $total = = 0 ��� true ,M-0����D:;/,M-!�)��J1% 0�I�:ก��,0%+"!M���+�*�>&&�+M1 ��+

    if ($ totalqty == 0) { echo w J-�>�->%/JL���.ก��01*+=�(�#�ก;/�ก-�
    x; } else { if ($soapqty >0) echo $soapqty." ก/�
    "; if ($shampooqty > 0) echo $shampooqty." & %
    "; if ($conditionerqty > 0) echo $conditionerqty." & %
    "; }

  • PHP

    14

    1.7 ��H��>>@%C5�# elseif

    D/�ก��M1%0$:# �?J�+�!��ก��กก -� 2 J�+ �!��ก ���@.��L�01*+ elseif 0����D0�/�+>%/;!�.J�+�!��ก @%.���@.��L�01*+?( ���ก��� ����@.��L�01*+ else ,!� if

    �5;"�=%# ก��:;/0- !%:ก��=�(�,O�2

  • ����������ก���� PHP

    -

    15

    @6o�@J�J1N4 �

  • PHP

    16

    default : echo w

    >�-�?&/��

    ���@.��L�01*+ switch �?2M$ก���M-�+#�ก���@.� if ;��� elseif ���@.��L�01*+ if JL�+�ก1) 1 ���@.��L�01*+ D/�>�-�? +�!U)�กก� 0- switch JL�+�M-�+ก1 ���*� case : switch �&/�0�-�? break ก������ !"!&�+ switch #�JL�+�M-�>�#D'+ case J?*��� true ���*���D'+ break )��J1%M-�>�&�+�L�01*+ ;!1+#�ก ���@.��L�01*+ switch #�>%/�1)ก������ !"!

    1.9 �>�#C��%#;��"!

    ก���L� oM��#L� ��1(+#�0����D:O/ loop �2�*�JL�+�)�+0- &�+@��,ก��=(L� �5;"�=%# : 1กN'ก6� M/�+ก��M���+,0%+�-��%$J�+>�.1+�;� $J.�!1. J?*&'(ก1)��.�J�+#�ก

    )/�D'+�;� $J.�!1.

    ��.ก���L�01*+ 1.3 ,0%+ HTML 0L�;�1),0%+M���+ #�0����D�;U��.�J�+,!�ก��=(L� ��.ก���L�01*+ 1.3 freight.php

    ��.�J�+ �-��%$J�+ 50 5 100 10

  • ����������ก���� PHP

    -

    17

    150 15 200 20 250 25

    ���@.��L�01*+ Loop )�ก:;/ PHP ���� !"!,))=(L�

    1.9.1 while loop Loop +-�.J?*0I%&�+ PHP ��� while loop ='*+�;���ก1)���@.��L�01*+ if J?*&'(ก1)

    �+�*�>&� ��,MกM-�+��; -�+ while loop ก1) ���@.��L�01*+ if ��� ���@.��L�01*+ if ���� !"!ก!I-��L�01*+�2?.+��1(+�%?. D/��+�*�>&���#�$+ while loop ���� !"!ก!I-��L�01*+=(L� M��)�J-�J?*�+�*�>&���#�$+ @%.J1* >� while loop :O/ ���*� >�-J��)#L� ��)���� !"!> .�ก�o42�(W�&�+ while loop ���while (condition) expression

    �5;"�=%# while loop ,0%+;��.�!& #�ก 1 D'+ 5

  • PHP

    18

    SGG5FTUDH[A�

    �� 1,0%+ก��:O/+� while loop

    #I%��$*�M/&�+,M-!���) ��� ก��J%0�)�+�*�>& D/��+�*�>&����JU# #�>�-�?ก������ !"! ,!�0$(0I% loop ���@.��L�01*+M-�>� ;!1+#�ก loop #�>%/�1)ก������ !"!M-�>�

    ��.�J�+ �-��%$J�+

  • ����������ก���� PHP

    -

    19

    ��.ก���L�01*+ 1.4 freight.php :O/ while loop M���+,0%+�-��%$J�+

    SGG5FTUDH[A�

    �� 2 ����ก������� while loop

    1.9.2 for loop for loop 0����D JL�+�ก1)M1 1)>%/%?

    for (expression1; condition ; expression2) expression3;

    • expression1 :O/JL��-���$*�M/&�+M1 1)

    • condition J%0�)$2#4 ก-���$*�,M-!���) D/�$2#40-+��ก�?�-�����JU##����ก��0$(0I%��)ก��JL�+� M���กM$ :O/J%0�)M1 1)ก1)&�)�&M

    • expression2 ���� !"!M�J/�.,M-!���) M���กM$���OI%�L�01*+ for loop 0����D�&?.,0%+M���+�-�&0-+ M��5�2&/�+!-�+ @%.�?�L�01*+ PHP %1+?(

  • PHP

    20

    SGG5FTUDH[A�

    1.9.3 do...while loop @��+0�/�+J1* >�&�+���@.��L�01*+ do while ���

    do expression; while (conditon);

    dowhile loop M/�+�?ก������ !"!5�.: loop �.-�+/�. 1 ��1(+D'+,�/ -� �+�*�>&����JU# ���*���$*�M/ ,!�>�-�?J�+���#�$+ loop #�>%/�1)ก������ !"! 1 ��1(+ ก-�ก��M� #0�)�+�*�>&,!�0$(0I%

  • ����������ก���� PHP

    -

    21

    $num = 100; do { echo $num.x
    x; } while ($num < 1);

    1.10 ก%�""กD%ก�>�#C��%#>;!>N�

    D/�M/�+ก��;.I%ก������ !"!�L�01*+ �? 3 $3? &'(ก1)"!J?*M/�+ก��D/�M/�+ก��;.I%ก������ !"! loop 0����D:O/���@.��L�01*+ break M��ก���5$���.:0- switch D/�:O/���@.��L�01*+ break : loop ก������ !"!0��$�M4 #�JL�+�)��J1%M-�>�&�+ 0��$�M4 M-�#�ก loop

    D/�M/�+ก��ก��@%%>�.1+��)M-�>� 0����D:O/���@.��L�01*+ ContinueD/�M/�+ก��0$(0I%���� !"!0��$�M4 PHP 0����D:O/ exit M���กM$:O/���*��ก$%� ��"$%2!�% M1 �.-�+�O- :O/��1)��I+M1 �.-�+ก-�>%/%1+?(

    if ($ totalqty == 0) { echo w J-�>�->%/JL���.ก��ก-�;/�
    x; exit; }