PHP PHP PHP HTML PHP PHP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

‫ﭘﻴﻤﺎﻧﻪ ﻣﻬﺎﺭﺗﻰ‪ :‬ﻃﺮﺍﺣﻰ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬

‫‪117‬‬ ‫ﻭﺍﺣﺪ ﻛﺎﺭﺷﺸﻢ‪ :‬ﻛﺎﺭ ﺑﺎ ﻣﺘﻐﻴﺮﻫﺎ‪ ،‬ﻋﻤﻠﮕﺮﻫﺎ ﻭ ﺳﺎﺧﺘﺎﺭﻫﺎﻯ ﻛﻨﺘﺮﻟﻰ‬

‫ﻛﺎﺭ ﺑﺎ ﻣﺘﻐﻴﺮﻫﺎ‪ ،‬ﻋﻤﻠﮕﺮﻫﺎ ﻭ ﺳﺎﺧﺘﺎﺭﻫﺎﻱ ﻛﻨﺘﺮﻟﻲ‬

‫ﻭﺍﺣﺪ ﻛﺎﺭﺷﺸﻢ‬

‫ﭘﺲ ﺍﺯ ﻣﻄﺎﻟﻌﻪ ﺍﻳﻦ ﻭﺍﺣﺪ ﻛﺎﺭ ﺍﺯ ﻓﺮﺍﮔﻴﺮ ﺍﻧﺘﻈﺎﺭ ﻣﻲﺭﻭﺩ ﻛﻪ‪:‬‬


‫‪ - 1‬ﺗﮓﻫﺎﻱ ﺁﻏﺎﺯ ﻭ ﭘﺎﻳﺎﻥ ‪ PHP‬ﺭﺍ ﺑﺸﻨﺎﺳﺪ‪.‬‬
‫‪ - 2‬ﺍﺻﻮﻝ ﻧﺤﻮﻩ ﻧﻮﺷﺘﻦ ﺩﺳﺘﻮﺭﺍﺕ ﺩﺭ ‪ PHP‬ﺭﺍ ﺑﺸﻨﺎﺳﺪ‪.‬‬
‫‪ - 3‬ﺑﺘﻮﺍﻧﺪ ﻛﺪﻫﺎﻱ ‪ HTML‬ﻭ ‪ PHP‬ﺭﺍ ﺩﺭ ﻛﻨﺎﺭ ﻳﻜﺪﻳﮕﺮ ﺍﺳﺘﻔﺎﺩﻩ ﻛﻨﺪ‪.‬‬
‫‪ - 4‬ﺑﺎ ﻧﺤﻮﻩ ﺩﺭﺝ ﺗﻮﺿﻴﺤﺎﺕ ﺩﺭﻭﻥ ﻛﺪﻫﺎﻱ ‪ PHP‬ﺁﺷﻨﺎ ﺑﺎﺷﺪ‪.‬‬
‫‪ - 5‬ﺍﻧﻮﺍﻉ ﻣﺘﻐﻴﺮﻫﺎﻱ ‪ PHP‬ﺭﺍ ﺑﺸﻨﺎﺳﺪ‪.‬‬
‫‪ - 6‬ﺑﺘﻮﺍﻧﺪ ﻣﺘﻐﻴﺮﻫﺎﻱ ﺍﺯ ﭘﻴﺶ ﺗﻌﺮﻳﻒ ﺷﺪﻩ ﺭﺍ ﺩﺭ ﺑﺮﻧﺎﻣﻪ ﺑﻪ ﻛﺎﺭ ﺑﺒﺮﺩ‪.‬‬
‫‪ - 7‬ﺑﺎ ﺛﺎﺑﺖﻫﺎ ﻭ ﻋﻤﻠﻜﺮﺩ ﺁﻥﻫﺎ ﺁﺷﻨﺎ ﺑﺎﺷﺪ‪.‬‬
‫‪ - 8‬ﺑﺎ ﺍﺻﻮﻝ ﻧﺎﻣﮕﺬﺍﺭﻱ ﻣﺘﻐﻴﺮﻫﺎ ﺁﺷﻨﺎ ﺑﺎﺷﺪ‪.‬‬
‫‪ - 9‬ﺍﻧﻮﺍﻉ ﻋﻤﻠﮕﺮﻫﺎﻱ ‪ PHP‬ﺭﺍ ﺑﺸﻨﺎﺳﺪ‪.‬‬
‫‪ - 10‬ﺍﺻﻮﻝ ﻛﺎﺭ ﺑﺎ ﺍﻧﻮﺍﻉ ﺳﺎﺧﺘﺎﺭﻫﺎﻱ ﻛﻨﺘﺮﻟﻲ ﺭﺍ ﺑﺪﺍﻧﺪ‪.‬‬

‫ﺯﻣﺎﻥ)ﺳﺎﻋﺖ(‬

‫ﻋﻤﻠﻰ‬ ‫ﻧﻈﺮﻯ‬
‫‪12‬‬ ‫‪6‬‬
‫ﻭﺯﺍﺭﺕ ﻓﺮﻫﻨﮓ ﻭ ﺍﺭﺷﺎﺩ ﺍﺳﻼﻣﻰ‬
‫ﻧﺎﻡ ﺍﺳﺘﺎﻧﺪﺍﺭﺩ‪ :‬ﻃﺮﺍﺡ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫ﺷﻤﺎﺭﻩ‪ -15 / 4/ 2 :‬ﻑ‪ ،‬ﻫـ‬ ‫‪118‬‬

‫ﻣﻘﺪﻣﻪ‬
‫ﺑﺮﺍﻱ ﺍﻳﺠﺎﺩ ﻓﺎﻳﻞﻫﺎﻱ ‪ PHP‬ﻧﻴﺎﺯﻱ ﻧﻴﺴﺖ ﺣﺘﻤ ًﺎ ﺑﺮﻧﺎﻣﻪ ﺧﺎﺻﻲ ﺭﻭﻱ ﺳﻴﺴﺘﻢ ﻧﺼﺐ ﺷﻮﺩ ﻭ‬
‫ﻛﺪﻫﺎﻱ ‪ PHP‬ﺭﺍ ﻣﻴﺘﻮﺍﻥ ﺩﺭ ﻳﻚ ﻭﻳﺮﺍﻳﺸﮕﺮ ﺳﺎﺩﻩ ﻣﺘﻨﻲ ﻣﺎﻧﻨﺪ‪ Notepad‬ﻧﻮﺷﺖ‪.‬‬
‫ﻧﺮﻡﺍﻓﺰﺍﺭ ‪ Dreamweaver‬ﻧﻴﺰ ﻣﻲﺗﻮﺍﻧﺪ ﻓﺎﻳﻞﻫﺎﻱ ‪ PHP‬ﺭﺍ ﭘﺸــﺘﻴﺒﺎﻧﻲ ﻛﺮﺩﻩ ﻭ ﻣﺤﻴﻂ ﻣﻨﺎﺳﺒﻲ‬
‫ﺑﺮﺍﻱ ﻛﺪﻧﻮﻳﺴﻲ ﻓﺮﺍﻫﻢ ﻛﻨﺪ‪.‬‬

‫‪ 6-1‬ﺷﺮﻭﻉ ﺑﺮﻧﺎﻣﻪﻧﻮﻳﺴﻲ ﺩﺭ ‪PHP‬‬

‫ﺑﺮﺍﻱ ﻧﻮﺷــﺘﻦ ﺩﺳــﺘﻮﺭﺍﺕ ‪ PHP‬ﺩﻭ ﺭﻭﺵ ﺍﺻﻠﻲ ﻭﺟﻮﺩ ﺩﺍﺭﺩ ﻛﻪ ﺩﺭ ﺍﺩﺍﻣﻪ ﺑﻪ ﺗﻮﺿﻴﺢ ﺁﻥﻫﺎ‬
‫ﺧﻮﺍﻫﻴﻢ ﭘﺮﺩﺍﺧﺖ‪ .‬ﺷﻤﺎ ﺑﻪ ﺻﻮﺭﺕ ﺩﻟﺨﻮﺍﻩ ﻣﻲﺗﻮﺍﻧﻴﺪ ﻫﺮ ﻛﺪﺍﻡ ﺍﺯ ﺍﻳﻦ ﺳﺎﺧﺘﺎﺭﻫﺎ ﺭﺍ ﺩﺭ ﺑﺮﻧﺎﻣﻪ‪،‬‬
‫ﻣﻮﺭﺩ ﺍﺳﺘﻔﺎﺩﻩ ﻗﺮﺍﺭ ﺩﻫﻴﺪ‪.‬‬
‫• ﺭﻭﺵ ﺍﻭﻝ‪ :‬ﺩﺳﺘﻮﺭﺍﺕ ﻭ ﻛﺪﻫﺎﻱ ‪ PHP‬ﺩﺭﻭﻥ ﻋﻼﺋﻢ >? … ?< ﻗﺮﺍﺭ ﻣﻲﮔﻴﺮﻧﺪ‪.‬‬
‫?<‬

‫ﻛﺪﻫﺎﻱ‪PHP‬‬ ‫ﺩﺳﺘﻮﺭﺍﺕ ﻭ‬
‫>?‬
‫‪ <?php‬ﻗﺮﺍﺭ ﻣﻲﮔﻴﺮﻧﺪ‪.‬‬ ‫>? …‬ ‫• ﺭﻭﺵ ﺩﻭﻡ‪ :‬ﺩﺳﺘﻮﺭﺍﺕ ﻭ ﻛﺪﻫﺎﻱ ‪ PHP‬ﺩﺭﻭﻥ ﻋﻼﺋﻢ‬
‫‪<?php‬‬

‫ﻛﺪﻫﺎﻱ‪PHP‬‬ ‫ﺩﺳﺘﻮﺭﺍﺕ ﻭ‬
‫>?‬

‫ﺗﻮﺻﻴﻪ‪ :‬ﺑﻪ ﻣﻨﻈﻮﺭ ﺗﺴــﻬﻴﻞ ﺩﺭ ﻛﺪﻧﻮﻳﺴــﻲ ﻭ ﺣﺪﺍﻛﺜﺮ ﺳــﺎﺯﮔﺎﺭﻱ ﻛﺪﻫﺎ ﺩﺭ ﻣﺮﻭﺭﮔﺮﻫﺎﻱ‬


‫ﻣﺨﺘﻠﻒ‪ ،‬ﺗﻮﺻﻴﻪ ﻣﻲﺷﻮﺩ ﺑﺮﺍﻱ ﻧﻮﺷﺘﻦ ﻛﺪﻫﺎﻱ ‪ PHP‬ﺍﺯ ﺭﻭﺵ ﺩﻭﻡ ﺍﺳﺘﻔﺎﺩﻩ ﻧﻤﺎﻳﻴﺪ‪.‬‬

‫ﺑﺮﺍﻱ ﺍﻳﺠﺎﺩ ﻳﻚ ﻓﺎﻳﻞ ﺍﺟﺮﺍﻳﻲ ﺑﺴــﻴﺎﺭ ﺳــﺎﺩﻩ ﺩﺭ ‪ PHP‬ﻣﺘــﻦ ‪ hello‬ﺭﺍ ﺩﺭ ﻣﺮﻭﺭﮔﺮ ﻧﻤﺎﻳﺶ‬
‫ﻣﻲﺩﻫﻴﻢ‪ .‬ﺩﺭ ﺍﻧﺘﻬﺎﻱ ﻫﺮ ﺧﻂ ﺍﺯ ﺩﺳﺘﻮﺭﺍﺕ ﻧﻴﺰ ﺑﺎﻳﺪ ﺍﺯ ﻋﻼﻣﺖ »;« ﺍﺳﺘﻔﺎﺩﻩ ﺷﻮﺩ‪.‬‬
‫ﺑﺮﺍﻱ ﻧﻤﺎﻳﺶ ﺍﻃﻼﻋﺎﺕ ﺩﺭ ﻣﺮﻭﺭﮔﺮ‪ ،‬ﺩﺳﺘﻮﺭﺍﺕ ﻣﺨﺘﻠﻔﻲ ﺑﻪﻛﺎﺭ ﻣﻲﺭﻭﺩ‪ ،‬ﺩﺳﺘﻮﺭ ‪ Print‬ﻳﻜﻲ‬
‫ﺍﺯ ﭘﺮﻛﺎﺭﺑﺮﺩﺗﺮﻳﻦ ﺁﻥﻫﺎﺳﺖ‪ .‬ﻛﺪ ﺯﻳﺮ ﻣﻴﺘﻮﺍﻧﺪ ﻣﺘﻦ ‪ hello‬ﺭﺍ ﺩﺭ ﻣﺮﻭﺭﮔﺮ ﻧﻤﺎﻳﺶ ﺩﻫﺪ‪.‬‬
‫ﭘﻴﻤﺎﻧﻪ ﻣﻬﺎﺭﺗﻰ‪ :‬ﻃﺮﺍﺣﻰ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫‪119‬‬ ‫ﻭﺍﺣﺪ ﻛﺎﺭﺷﺸﻢ‪ :‬ﻛﺎﺭ ﺑﺎ ﻣﺘﻐﻴﺮﻫﺎ‪ ،‬ﻋﻤﻠﮕﺮﻫﺎ ﻭ ﺳﺎﺧﺘﺎﺭﻫﺎﻯ ﻛﻨﺘﺮﻟﻰ‬

‫‪<? PHP‬‬
‫;)”!‪Print (”hello‬‬

‫>?‬
‫ﺍﻳﻦ ﻛﺪ ﺭﺍ ﺩﺭ ‪ Notepad‬ﺑﻨﻮﻳﺴــﻴﺪ‪ ،‬ﻓﺎﻳﻞ ﺭﺍ ﺑﺎ ﭘﺴــﻮﻧﺪ ‪ PHP‬ﺩﺭ ﻣﺴﻴﺮ ﻧﺼﺐ ‪ WAMP‬ﻭ ﺩﺭ‬
‫ﺷﺎﺧﻪ ‪ www‬ﺫﺧﻴﺮﻩ ﻛﻨﻴﺪ‪.‬‬
‫ﺳﭙﺲ ﻣﺮﻭﺭﮔﺮ ﺭﺍ ﺑﺎﺯ ﻛﺮﺩﻩ ﻭ ﺩﺭ ﻧﻮﺍﺭ ﺁﺩﺭﺱ ﺍﻳﻦ ﻣﺘﻦ ﺭﺍ ﺗﺎﻳﭗ ﻛﻨﻴﺪ‪) :‬ﺑﻪ ﺟﺎﻱ ﻧﺎﻡ ﻓﺎﻳﻞ‪،‬‬
‫ﻋﻨﻮﺍﻧﻲ ﺭﺍ ﻛﻪ ﺑﺮﺍﻱ ﺫﺧﻴﺮﻩ ﻓﺎﻳﻞ ﻭﺍﺭﺩ ﻛﺮﺩﻳﺪ ﺩﺭﺝ ﻧﻤﺎﻳﻴﺪ‪(.‬‬
‫‪.php‬ﻧﺎﻡ ﻓﺎﻳﻞ‪http://localhost/‬‬

‫ﺷﻜﻞ ‪6-1‬‬

‫ﻧﻜﺘﻪ ﺍﮔﺮ ﺍﻳﻦ ﻓﺎﻳﻞ ﺭﺍ ﺑﺎ ﻧﺎﻡ ‪ index.php‬ﻳﺎ ‪ default.php‬ﺫﺧﻴﺮﻩ ﻛﻨﻴﺪ‪ ،‬ﻻﺯﻡ ﻧﻴﺴﺖ ﺩﺭ‬
‫ﻧﻮﺍﺭ ﺁﺩﺭﺱ ﻧﺎﻡ ﻓﺎﻳﻞ ﺭﺍ ﺫﻛﺮ ﻧﻤﺎﻳﻴﺪ‪ ،‬ﻓﻘﻂ ﻛﺎﻓﻲ ﺍﺳﺖ ﻋﺒﺎﺭﺕ ‪ http://localhost‬ﺭﺍ‬
‫ﺗﺎﻳﭗ ﻛﺮﺩﻩ ﻭ ﻛﻠﻴﺪ ‪ Enter‬ﺭﺍ ﻓﺸﺎﺭ ﺩﻫﻴﺪ‪.‬‬

‫ﺩﺭ ﺗﻤﺎﻡ ﺳــﺎﻳﺖﻫﺎ‪ ،‬ﺻﻔﺤﻪ ﺍﺻﻠﻲ )‪ (home page‬ﺳــﺎﻳﺖ ﺭﺍ ﺑﺎ ﻧﺎﻡ ‪ index‬ﻳﺎ ‪ default‬ﺫﺧﻴﺮﻩ‬
‫ﻣﻴﻜﻨﻨﺪ‪ ،‬ﻫﻨﮕﺎﻡ ﺩﺭﺝ ﺁﺩﺭﺱ ﻳﻚ ﺳــﺎﻳﺖ‪ ،‬ﻫﻤﻴﺸﻪ ﺻﻔﺤﻪﺍﻱ ﻛﻪ ﺩﺭ ﻣﺴﻴﺮ ﺍﺻﻠﻲ ﺳﺎﻳﺖ ﺑﺎ ﻧﺎﻡ‬
‫‪ index‬ﻳﺎ ‪ default‬ﺑﺎﺷــﺪ‪ ،‬ﺑﻪ ﻃﻮﺭ ﭘﻴﺶﻓﺮﺽ ﺑﺮﺍﻱ ﺍﺟﺮﺍ ﺍﻧﺘﺨﺎﺏ ﻣﻴﺸــﻮﺩ‪ ،‬ﺳــﺎﻳﺮ ﺻﻔﺤﺎﺕ ﺍﺯ‬
‫ﻃﺮﻳﻖ ﻫﻤﻴﻦ ﺻﻔﺤﻪ ﺑﺎ ﻳﻜﺪﻳﮕﺮ ﻣﺮﺗﺒﻂ ﻣﻴﺸﻮﻧﺪ‪.‬‬
‫ﺩﺳﺘﻮﺭ ﺍﺻﻠﻲ ﺑﺮﺍﻱ ﭼﺎپ ﺍﻃﻼﻋﺎﺕ ﺩﺭ ﺧﺮﻭﺟﻲ ﻫﺮ ﺯﺑﺎﻥ ‪ ،PHP‬ﺩﺳﺘﻮﺭ ‪ echo‬ﺍﺳﺖ‪ .‬ﺑﺮﻧﺎﻣﻪ‬
‫ﻗﺒﻞ ﺭﺍ ﺑﺎ ﺍﻳﻦ ﺩﺳﺘﻮﺭ ﻣﻲﺗﻮﺍﻧﻴﺪ ﺑﻪ ﺍﻳﻦ ﺻﻮﺭﺕ ﺑﻨﻮﻳﺴﻴﺪ‪:‬‬
‫‪<? Php‬‬
‫ﻭﺯﺍﺭﺕ ﻓﺮﻫﻨﮓ ﻭ ﺍﺭﺷﺎﺩ ﺍﺳﻼﻣﻰ‬
‫ﻧﺎﻡ ﺍﺳﺘﺎﻧﺪﺍﺭﺩ‪ :‬ﻃﺮﺍﺡ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫ﺷﻤﺎﺭﻩ‪ -15 / 4/ 2 :‬ﻑ‪ ،‬ﻫـ‬ ‫‪120‬‬

‫;)"!‪echo ("hello‬‬

‫>?‬

‫ﺍﮔﺮ ﺑﺨﻮﺍﻫﻴﺪ ﭼﻨﺪ ﻋﺒﺎﺭﺕ ﺭﺍ ﺗﻮﺳــﻂ ﻳﻚ ﺩﺳﺘﻮﺭ ‪ echo‬ﭼﺎپ ﻛﻨﻴﺪ‪ ،‬ﺑﺎﻳﺪ ﻫﺮ ﻋﺒﺎﺭﺕ ﺭﺍ ﺑﺎ‬
‫"‪ ".‬ﺍﺯ ﻋﺒﺎﺭﺕ ﺑﻌﺪﻱ ﺟﺪﺍ ﻛﻨﻴﺪ‪ .‬ﻣﺎﻧﻨﺪ)"‪echo ("hello!"." welcome‬‬

‫ﺣﺎﻝ ﺍﮔﺮ ﺑﺨﻮﺍﻫﻴﺪ ﻫﺮ ﻛﺪﺍﻡ ﺍﺯ ﻋﺒﺎﺭﺍﺕ ﺩﺭ ﻳﻚ ﺳﻄﺮ ﺟﺪﺍ ﭼﺎپ ﺷﻮﺩ ﺑﺎﻳﺪ ﺑﻴﻦ ﻫﺮ ﻋﺒﺎﺭﺕ‬
‫ﺍﺯ ">‪ "<br‬ﺍﺳﺘﻔﺎﺩﻩ ﻛﻨﻴﺪ‪.‬‬
‫)"‪echo ("hello!" . "<br>" . "welcome‬‬ ‫ﻣﺜﺎﻝ‪:‬‬

‫‪ 6-1-1‬ﺍﺳﺘﻔﺎﺩﻩ ﺍﺯ ﻛﺪﻫﺎﻱ ‪ PHP‬ﻭ ‪ HTML‬ﺩﺭ ﻛﻨﺎﺭ ﻳﻜﺪﻳﮕﺮ‬


‫ﻳﻜﻲ ﺍﺯ ﻗﺎﺑﻠﻴﺖﻫﺎﻱ ﺯﺑﺎﻥ ‪ PHP‬ﺍﻳﻦ ﺍﺳﺖ ﻛﻪ ﻣﻴﺘﻮﺍﻧﺪ ﺩﺭﻭﻥ ﻛﺪﻫﺎﻱ ‪ HTML‬ﺑﻪ ﻛﺎﺭ ﺑﺮﺩﻩ ﺷﻮﺩ‪.‬‬
‫>‪<html‬‬
‫>‪<head‬‬
‫>‪<title>first program</title‬‬
‫>‪</head‬‬
‫>‪<body‬‬

‫‪<?php‬‬
‫;)"!‪echo("hello‬‬
‫>?‬

‫>‪</body‬‬
‫>‪</html‬‬

‫ﻫﻤﺎﻧﻄﻮﺭ ﻛﻪ ﻣﻼﺣﻈﻪ ﻣﻲﻛﻨﻴﺪ‪ ،‬ﺑﺮﺍﻱ ﺩﺭﺝ ﻛﺪ ‪ PHP‬ﺩﺭﻭﻥ ‪ HTML‬ﻓﻘﻂ ﻛﺎﻓﻲ ﺍﺳﺖ ﺁﻥ ﺭﺍ‬
‫ﺩﺭﻭﻥ ﺗﮓ >‪ <body> … </body‬ﺑﻨﻮﻳﺴــﻴﺪ‪ ،‬ﺍﻣﺎ ﭘﺲ ﺍﺯ ﻧﻮﺷﺘﻦ ﻛﺪﻫﺎ‪ ،‬ﺑﺎﻳﺪ ﻓﺎﻳﻞ ﺑﺎ ﭘﺴﻮﻧﺪ ‪.php‬‬

‫ﺫﺧﻴﺮﻩ ﺷﻮﺩ ﻭ ﺩﺭﺻﻮﺭﺕ ﺫﺧﻴﺮﻩ ﻓﺎﻳﻞ ﺑﺎ ﭘﺴﻮﻧﺪ ‪ htm‬ﻳﺎ ‪ ،.html‬ﻛﺪﻫﺎﻱ ‪ PHP‬ﺍﺟﺮﺍ ﻧﺨﻮﺍﻫﻨﺪ ﺷﺪ‪.‬‬

‫‪ 6-1-2‬ﺩﺭﺝ ﺗﻮﺿﻴﺤﺎﺕ ﺩﺭﻭﻥ ﻛﺪﻫﺎﻱ ‪PHP‬‬


‫ﺗﻮﺿﻴﺤﺎﺕ ﺩﺭ ﺯﺑﺎﻥﻫﺎﻱ ﺑﺮﻧﺎﻣﻪﻧﻮﻳﺴــﻲ ﺑﻪ ﻣﻨﻈﻮﺭ ﺭﺍﻫﻨﻤﺎﻳﻲ ﺑﺮﺍﻱ ﺑﺮﻧﺎﻣﻪﻧﻮﻳﺲ ﻳﺎ ﻓﺮﺩﻱ‬
‫ﭘﻴﻤﺎﻧﻪ ﻣﻬﺎﺭﺗﻰ‪ :‬ﻃﺮﺍﺣﻰ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫‪121‬‬ ‫ﻭﺍﺣﺪ ﻛﺎﺭﺷﺸﻢ‪ :‬ﻛﺎﺭ ﺑﺎ ﻣﺘﻐﻴﺮﻫﺎ‪ ،‬ﻋﻤﻠﮕﺮﻫﺎ ﻭ ﺳﺎﺧﺘﺎﺭﻫﺎﻯ ﻛﻨﺘﺮﻟﻰ‬

‫ﺍﺳﺖ ﻛﻪ ﻣﻴﺨﻮﺍﻫﺪ ﺍﺯ ﻛﺪﻫﺎ ﺍﺳﺘﻔﺎﺩﻩ ﻛﻨﺪ‪ ،‬ﺩﺭ ‪ PHP‬ﻧﻴﺰ ﻣﺎﻧﻨﺪ ﺳﺎﻳﺮ ﺯﺑﺎﻥﻫﺎﻱ ﺑﺮﻧﺎﻣﻪﻧﻮﻳﺴﻲ‬
‫ﺩﺳــﺘﻮﺭﺍﻟﻌﻤﻞﻫﺎﻳﻲ ﺑﺮﺍﻱ ﺩﺭﺝ ﺗﻮﺿﻴﺤﺎﺕ ﻭﺟﻮﺩ ﺩﺍﺭﺩ‪ .‬ﺩﺭ‪ PHP‬ﺑﺮﺍﻱ ﺗﻮﺿﻴﺤﺎﺕ ﻳﻚ ﺧﻄﻲ‬
‫ﺍﺯ ﻋﻼﺋــﻢ ‪ //‬ﻭ ‪ #‬ﻭ ﺑــﺮﺍﻱ ﺩﺭﺝ ﺗﻮﺿﻴﺤﺎﺕ ﭼﻨﺪ ﺧﻄﻲ ﺍﺯ ﻋﻼﻣﺖ *‪ /‬ﺩﺭ ﺍﺑﺘﺪﺍ ﻭ ﺍﺯ ﻋﻼﻣﺖ‪*/‬‬

‫ﺩﺭ ﺍﻧﺘﻬﺎﻱ ﺗﻮﺿﻴﺤﺎﺕ ﺍﺳﺘﻔﺎﺩﻩ ﻣﻴﺸﻮﺩ‪.‬‬


‫>‪<html‬‬
‫>‪<head‬‬
‫>‪<title>first program</title‬‬
‫>‪</head‬‬
‫>‪<body‬‬

‫‪<?php‬‬
‫!‪/* Line number1: These 3 lines will not either‬‬
‫‪Line number2‬‬
‫‪Line number3: End of comment */‬‬

‫;)"!‪echo("hello‬‬

‫‪// This line will not be parsed in PHP‬‬

‫;)"!‪echo("welcome‬‬

‫‪# This line will not be parsed in PHP like the line above‬‬

‫>?‬

‫>‪</body‬‬
‫>‪</html‬‬
‫ﻭﺯﺍﺭﺕ ﻓﺮﻫﻨﮓ ﻭ ﺍﺭﺷﺎﺩ ﺍﺳﻼﻣﻰ‬
‫ﻧﺎﻡ ﺍﺳﺘﺎﻧﺪﺍﺭﺩ‪ :‬ﻃﺮﺍﺡ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫ﺷﻤﺎﺭﻩ‪ -15 / 4/ 2 :‬ﻑ‪ ،‬ﻫـ‬ ‫‪122‬‬

‫‪ 6-2‬ﻣﺘﻐﻴﺮﻫﺎ‬

‫ﻳﻜﻲ ﺍﺯ ﻣﻬﻤﺘﺮﻳﻦ ﺍﺑﺰﺍﺭﻫﺎﻳﻲ ﻛﻪ ﺑﺮﺍﻱ ﺗﺒﺪﻳﻞ ﺻﻔﺤﺎﺕ ﺳﺎﺩﻩ ﻭ ﺛﺎﺑﺖ ﺑﻪ ﺻﻔﺤﺎﺕ ﭘﻮﻳﺎ ﻻﺯﻡ‬
‫ﺍﺳﺖ‪ ،‬ﺍﻳﻦ ﺍﺳﺖ ﻛﻪ ﺳﺮﻭﻳﺲﺩﻫﻨﺪﻩ ﺑﺘﻮﺍﻧﺪ ﺍﻃﻼﻋﺎﺕ ﺭﺍ ﺍﺯ ﻛﺎﺭﺑﺮ ﺳﺮﻭﻳﺲﮔﻴﺮﻧﺪﻩ ﺩﺭﻳﺎﻓﺖ ﻛﻨﺪ‬
‫ﻭ ﺑﺎ ﺗﻮﺟﻪ ﺑﻪ ﺍﻃﻼﻋﺎﺕ ﭘﺎﺳــﺦ ﻣﻨﺎﺳﺒﻲ ﺑﻪ ﻛﺎﺭﺑﺮ ﺑﺪﻫﺪ‪ .‬ﻣﺘﻐﻴﺮﻫﺎ ﺍﺑﺰﺍﺭﻱ ﻫﺴﺘﻨﺪ ﻛﻪ ﺑﺎ ﺍﺳﺘﻔﺎﺩﻩ‬
‫ﺍﺯ ﺁﻧﻬﺎ ﻣﻴﺘﻮﺍﻥ ﺍﻃﻼﻋﺎﺕ ﺭﺍ ﺍﺯ ﻛﺎﺭﺑﺮ ﺩﺭﻳﺎﻓﺖ ﻛﺮﺩ‪ .‬ﺩﺭ ﻭﺍﻗﻊ ﻣﺘﻐﻴﺮﻫﺎ ﺑﻪ ﻣﻨﺰﻟﻪ ﻇﺮﻓﻬﺎﻳﻲ ﻫﺴﺘﻨﺪ‬
‫ﻛﻪ ﺍﻃﻼﻋﺎﺕ ﻛﺎﺭﺑﺮ ﺭﺍ ﺑﻪ ﻃﻮﺭ ﻣﻮﻗﺖ ﺫﺧﻴﺮﻩ ﻛﺮﺩﻩ ﻭ ﺩﺭ ﻣﻮﺍﻗﻊ ﻟﺰﻭﻡ ﺁﻧﺮﺍ ﺑﺮﺍﻱ ﺳﺮﻭﻳﺲﺩﻫﻨﺪﻩ‬
‫ﺍﺭﺳﺎﻝ ﻣﻴﻜﻨﻨﺪ‪.‬‬
‫ﺑﺮﺍﻱ ﺗﻌﺮﻳﻒ ﻣﺘﻐﻴﺮ ﺩﺭ ‪ PHP‬ﺑﺎﻳﺪ ﺍﺯ ﺣﺮﻑ ‪ $‬ﺍﺳﺘﻔﺎﺩﻩ ﻛﺮﺩ‪.‬‬
‫;‪$a‬‬ ‫ﻣﺜﺎﻝ‪:‬‬
‫;‪$b=10‬‬

‫ﻧﻮﻉ ﻣﺘﻐﻴﺮ ﺩﺭ ‪ PHP‬ﺍﻫﻤﻴﺘﻲ ﻧﺪﺍﺭﺩ ﻭ ﻻﺯﻡ ﻧﻴﺴﺖ ﻫﻨﮕﺎﻡ ﺗﻌﺮﻳﻒ ﻣﺘﻐﻴﺮ‪ ،‬ﻧﻮﻉ ﺁﻥ ﺫﻛﺮ ﺷﻮﺩ‪.‬‬
‫ﻣﺘﻐﻴﺮﻫﺎﻱ ‪ PHP‬ﻧﺴﺒﺖ ﺑﻪ ﺣﺮﻭﻑ ﻛﻮﭼﻚ ﻭ ﺑﺰﺭگ ﺣﺴﺎﺱ ﻫﺴﺘﻨﺪ‪.‬‬
‫ﺑــﻪ ﻋﻨﻮﺍﻥ ﻣﺜﺎﻝ ﻣﺘﻐﻴﺮﻫــﺎﻱ‪ a‬ﻭ ‪ A‬ﺑﺎ ﻳﻜﺪﻳﮕﺮ ﻣﺘﻔﺎﻭﺕ ﺑﻮﺩﻩ ﻭ ﺩﻭ ﻣﺘﻐﻴﺮ ﺟﺪﺍ ﻣﺤﺴــﻮﺏ‬
‫ﻣﻲﺷﻮﻧﺪ‪ .‬ﺩﺭ ﻧﺎﻣﮕﺬﺍﺭﻱ ﻣﺘﻐﻴﺮﻫﺎ ﻋﻼﻭﻩ ﺑﺮ ﺣﺮﻭﻑ ﻛﻮﭼﻚ ﻭ ﺑﺰﺭگ ﻣﻲﺗﻮﺍﻥ ﺍﺯ ﻋﻼﺋﻤﻲ ﻣﺎﻧﻨﺪ‬
‫"_" ﻳﺎ ﺧﻂ ﻓﺎﺻﻠﻪ ﭘﺎﻳﻴﻦ )‪ (Underline‬ﺍﺳﺘﻔﺎﺩﻩ ﻛﺮﺩ‪.‬‬
‫ﺍﺳﺘﻔﺎﺩﻩ ﺍﺯ ﺍﻋﺪﺍﺩ ﻧﻴﺰ ﺩﺭ ﻧﺎﻣﮕﺬﺍﺭﻱ ﻣﺘﻐﻴﺮﻫﺎﻱ‪ PHP‬ﻣﺠﺎﺯ ﺍﺳﺖ ﺩﺭ ﺻﻮﺭﺗﻲﻛﻪ ﺍﻭﻟﻴﻦ ﺣﺮﻑ‬
‫ﺍﺯ ﻣﺘﻐﻴﺮ ﻧﺒﺎﺷﺪ‪.‬‬
‫ﻣﺜﺎﻝ‪ :‬ﻣﺘﻐﻴﺮﻫﺎﻱ ﺑﺎ ﻧﺎﻡﻫﺎﻱ‪ $ab_1‬ﻭ‪ $_abc‬ﻣﺠﺎﺯ ﻫﺴﺘﻨﺪ‪.‬‬
‫ﻣﺜﺎﻝ‪ :‬ﻣﺘﻐﻴﺮﻫﺎﻱ ﺑﺎ ﻧﺎﻡﻫﺎﻱ ‪ $1ab‬ﻭ ‪ $1_a‬ﻏﻴﺮﻣﺠﺎﺯ ﻫﺴﺘﻨﺪ‪.‬‬

‫ﻧﻜﺘﻪ ﻧﺎﻣﮕــﺬﺍﺭﻱ ﻣﺘﻐﻴﺮﻫﺎ ﺑﻪﻃﻮﺭﻱ ﻛﻪ "_" )‪ (Underline‬ﺩﺭ ﺍﺑﺘﺪﺍﻱ ﺁﻥﻫﺎ ﺑﺎﺷــﺪ‪ ،‬ﻣﺠﺎﺯ‬
‫ﺍﺳــﺖ ﺍﻣﺎ ﺗﻮﺻﻴﻪ ﻣﻲﺷــﻮﺩ ﺍﺯ ﻧﺎﻣﮕﺬﺍﺭﻱ ﻣﺘﻐﻴﺮﻫﺎ ﺑﻪ ﺍﻳﻦ ﺷــﻴﻮﻩ ﺍﺳــﺘﻔﺎﺩﻩ ﻧﻜﻨﻴﺪ ﻭ‬
‫ﺣﺘﻲﺍﻻﻣﻜﺎﻥ ﺍﺯ "_" ﺩﺭ ﺍﺑﺘﺪﺍﻱ ﻧﺎﻡ ﻣﺘﻐﻴﺮﻫﺎ ﺍﺳﺘﻔﺎﺩﻩ ﻧﻜﻨﻴﺪ‪.‬‬
‫ﺩﺭ ﻣﺘﻐﻴﺮﻫﺎ ﻧﺸــﺎﻥ ﺩﺍﺩﻥ ﻧﻮﻉ ﺗﻮﺳــﻂ ﻣﻘﺪﺍﺭﺩﻫﻲ ﺗﻌﻴﻴﻦ ﻣﻲﺷﻮﺩ‪ .‬ﺩﺳﺘﻮﺭﺍﻟﻌﻤﻞ ﺗﻌﻴﻴﻦ ﻧﻮﻉ‬
‫ﺑﺮﺍﻱ ﻫﺮ ﻣﺘﻐﻴﺮ ﺑﻪﺻﻮﺭﺕ ﺯﻳﺮ ﺍﺳﺖ‪:‬‬
‫• ﺍﻋﺪﺍﺩ ﺻﺤﻴﺢ ﺑﻪﻃﻮﺭ ﺳﺎﺩﻩ ﻧﻮﺷﺘﻪ ﻣﻲﺷﻮﻧﺪ‪.‬‬
‫ﭘﻴﻤﺎﻧﻪ ﻣﻬﺎﺭﺗﻰ‪ :‬ﻃﺮﺍﺣﻰ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫‪123‬‬ ‫ﻭﺍﺣﺪ ﻛﺎﺭﺷﺸﻢ‪ :‬ﻛﺎﺭ ﺑﺎ ﻣﺘﻐﻴﺮﻫﺎ‪ ،‬ﻋﻤﻠﮕﺮﻫﺎ ﻭ ﺳﺎﺧﺘﺎﺭﻫﺎﻯ ﻛﻨﺘﺮﻟﻰ‬

‫;‪$a=15‬‬ ‫ﻣﺜﺎﻝ‪:‬‬
‫• ﺍﻋﺪﺍﺩ ﺍﻋﺸﺎﺭﻱ ﺗﻮﺳﻂ ﻋﻼﻣﺖ"‪ ".‬ﺗﻌﻴﻴﻦ ﻣﻲﺷﻮﻧﺪ‪.‬‬
‫ﻣﺜﺎﻝ‪:‬‬
‫;‪$b=7.0‬‬

‫;‪$C=1.295‬‬
‫• ﺍﻋﺪﺍﺩ ﻛﺴﺮﻱ ﺗﻮﺳﻂ ﻋﻼﻣﺖ "‪ "/‬ﺗﻌﻴﻴﻦ ﻣﻲﺷﻮﻧﺪ‪.‬‬
‫;‪$f=1/2‬‬ ‫ﻣﺜﺎﻝ‪:‬‬

‫‪0.5‬‬ ‫ﻧﻜﺘﻪ ﺍﻋﺪﺍﺩ ﻛﺴﺮﻱ ﺑﻪﺻﻮﺭﺕ ﺍﻋﺸﺎﺭﻱ ﺫﺧﻴﺮﻩ ﻣﻲﺷﻮﻧﺪ‪ ،‬ﺩﺭ ﻣﺜﺎﻝ ﻓﻮﻕ ﻣﺘﻐﻴﺮ‪ f‬ﺑﺎ‬
‫ﻣﻘﺪﺍﺭﺩﻫﻲ ﻣﻲﺷﻮﺩ‪.‬‬
‫ﺭﺷﺘﻪﻫﺎ ﺩﺭﻭﻥ ﻋﻼﻣﺖ " " ﻳﺎ ' ' ﺩﺭﺝ ﻣﻲﺷﻮﻧﺪ‪.‬‬
‫; "‪$Str = "Hello‬‬ ‫ﻣﺜﺎﻝ‪:‬‬
‫; "‪$H= "2914‬‬
‫; '‪$M = 'Welcome‬‬

‫ﺍﮔﺮ ﻳﻚ ﻋﺪﺩ ﺑﻴﻦ ﻋﻼﻣﺖ ” ” ﻗﺮﺍﺭ ﮔﻴﺮﺩ‪ ،‬ﻣﻘﺪﺍﺭ ﺭﺷﺘﻪﺍﻱ ﻣﺤﺴﻮﺏ ﻣﻲﺷﻮﺩ‪.‬‬
‫ﻣﺜﺎﻝ‪ :‬ﻣﻲﺧﻮﺍﻫﻴﻢ ﺍﺑﺘﺪﺍ ﺩﻭ ﻣﺘﻐﻴﺮ ﺭﺍ ﺑﺎ ﺍﻋﺪﺍﺩ ‪ 35‬ﻭ ‪ 79‬ﻣﻘﺪﺍﺭﺩﻫﻲ ﻛﻨﻴﻢ‪ ،‬ﺳﭙﺲ ﺩﺭ ﺧﺮﻭﺟﻲ‬
‫ﺣﺎﺻﻞﺟﻤﻊ ﺍﻳﻦ ﺩﻭ ﻋﺪﺩ ﺭﺍ ﭼﺎپ ﻛﻨﻴﻢ‪:‬‬
‫>‪<html‬‬
‫>‪<head‬‬
‫>‪<title>first program</title‬‬
‫>‪</head‬‬
‫>‪<body‬‬

‫‪<?php‬‬

‫;‪$num1 = 35‬‬
‫;‪$num2 = 79‬‬
‫ﻭﺯﺍﺭﺕ ﻓﺮﻫﻨﮓ ﻭ ﺍﺭﺷﺎﺩ ﺍﺳﻼﻣﻰ‬
‫ﻧﺎﻡ ﺍﺳﺘﺎﻧﺪﺍﺭﺩ‪ :‬ﻃﺮﺍﺡ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫ﺷﻤﺎﺭﻩ‪ -15 / 4/ 2 :‬ﻑ‪ ،‬ﻫـ‬ ‫‪124‬‬

‫;‪$sum = $num1 + $num2‬‬


‫;)" ‪echo("sum is:‬‬
‫;)"‪echo("$sum‬‬

‫>?‬

‫>‪</body‬‬
‫>‪</html‬‬
‫ﺷﻜﻞ ‪6-2‬‬
‫‪ 6-2-1‬ﺁﺭﺍﻳﻪﻫﺎ‬
‫ﻫﻤﺎﻥﻃــﻮﺭ ﻛﻪ ﻣﻲﺩﺍﻧﻴﺪ ﺁﺭﺍﻳﻪﻫﺎ ﺍﺯ ﻟﺤﺎﻅ ﺳــﺎﺧﺘﺎﺭﻱ ﭘﻴﭽﻴﺪﻩﺗﺮ ﺍﺯ ﺍﻧــﻮﺍﻉ ﺩﺍﺩﻩﻫﺎﻱ ﺩﻳﮕﺮ‬
‫ﻫﺴﺘﻨﺪ‪ ،‬ﺑﺮﺧﻼﻑ ﺍﻧﻮﺍﻉ ﺩﺍﺩﻩﻫﺎﻱ ﺭﺷﺘﻪﺍﻱ ﻭ ﻋﺪﺩﻱ ﻛﻪ ﻓﻘﻂ ﻣﻲﺗﻮﺍﻧﻨﺪ ﻳﻚ ﻣﻘﺪﺍﺭ ﺭﺍ ﺑﭙﺬﻳﺮﻧﺪ‪،‬‬
‫ﺁﺭﺍﻳﻪﻫﺎ ﻣﻲﺗﻮﺍﻧﻨﺪ ﺷﺎﻣﻞ ﻟﻴﺴﺘﻲ ﺍﺯ ﻣﻘﺎﺩﻳﺮ ﻣﺨﺘﻠﻒ ﺑﺎﺷﻨﺪ‪.‬‬
‫ﺩﺭ ﺯﺑﺎﻥ ‪ PHP‬ﺁﺭﺍﻳﻪﻫﺎ ﺭﺍ ﻣﻲﺗﻮﺍﻥ ﺑﻪ ﺩﻭ ﺩﺳــﺘﻪ ﺍﻧﺪﻳﺲﺩﺍﺭ‪ 1‬ﻭ ﺍﻧﺠﻤﻨﻲ‪2‬ﺗﻘﺴﻴﻢ ﻛﺮﺩ‪ ،‬ﺭﻭﺵ‬
‫ﺩﺳﺘﺮﺳﻲ ﺑﻪ ﻋﻨﺎﺻﺮ ﺩﺭ ﺍﻳﻦ ﺩﻭ ﺭﻭﺵ ﻣﺘﻔﺎﻭﺕ ﺍﺳﺖ‪.‬‬
‫ﺩﺭ ﺁﺭﺍﻳﻪﻫــﺎﻱ ﺍﻧﺪﻳﺲﺩﺍﺭ‪ ،‬ﺩﺳﺘﺮﺳــﻲ ﺑﻪ ﻫﺮﻳــﻚ ﺍﺯ ﻋﻨﺎﺻﺮ ﺗﻮﺳــﻂ ﺍﻧﺪﻳﺲ ﺁﻥ ﺻﻮﺭﺕ‬
‫ﻣﻲﮔﻴﺮﺩ‪.‬‬
‫ﺍﻭﻟﻴﻦ ﺍﻧﺪﻳﺲ ﺁﺭﺍﻳﻪ ﻧﻴﺰ ﻣﺎﻧﻨﺪ ﺯﺑﺎﻥ ﺟﺎﻭﺍ ﺍﺳﻜﺮﻳﭙﺖ »‪ «0‬ﺍﺳﺖ‪.‬‬
‫ﺳﺎﺧﺘﺎﺭ ﻛﻠﻲ ﺁﺭﺍﻳﻪ ﺍﻧﺪﻳﺲﺩﺍﺭ ﺑﻪﺻﻮﺭﺕ ﺯﻳﺮ ﺍﺳﺖ‪:‬‬
‫;“ﻣﻘﺪﺍﺭ ﻋﻨﺼﺮ ﺁﺭﺍﻳﻪ“= ] [ ﻧﺎﻡ ﻣﺘﻐﻴﺮ ﺁﺭﺍﻳﻪﺍﻱ ‪$‬‬ ‫ﻣﺜﺎﻝ‪:‬‬
‫;"‪$name[]="Maryam‬‬
‫;"‪$name[]="zahra‬‬
‫;"‪$name[]="Meisam‬‬

‫ﺭﻭﺵ ﺩﻭﻡ ﺑﺮﺍﻱ ﻣﻌﺮﻓﻲ ﺁﺭﺍﻳﻪﻫﺎ ﺍﺳﺘﻔﺎﺩﻩ ﺍﺯ ﺷﻨﺎﺳﻪ‪ array‬ﺍﺳﺖ‪.‬‬


‫)‪ ...‬ﻭ "ﻣﻘﺪﺍﺭ ﻋﻨﺼﺮ ﺩﻭﻡ" ﻭ "ﻣﻘﺪﺍﺭ ﻋﻨﺼﺮ ﺍﻭﻝ"( ‪ = array‬ﻧﺎﻡ ﻣﺘﻐﻴﺮ ﺁﺭﺍﻳﻪﺍﻱ‪$‬‬

‫ﻣﺜﺎﻝ‪:‬‬
‫;) "‪ "Meisam‬ﻭ "‪ "Zahra‬ﻭ "‪$name = array ("Maryam‬‬

‫‪1- Indexed‬‬
‫‪2- Association‬‬
‫ﭘﻴﻤﺎﻧﻪ ﻣﻬﺎﺭﺗﻰ‪ :‬ﻃﺮﺍﺣﻰ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫‪125‬‬ ‫ﻭﺍﺣﺪ ﻛﺎﺭﺷﺸﻢ‪ :‬ﻛﺎﺭ ﺑﺎ ﻣﺘﻐﻴﺮﻫﺎ‪ ،‬ﻋﻤﻠﮕﺮﻫﺎ ﻭ ﺳﺎﺧﺘﺎﺭﻫﺎﻯ ﻛﻨﺘﺮﻟﻰ‬

‫ﺣﺎﻝ ﻣﻲﺗﻮﺍﻧﻴﻢ ﺑﺎ ﺍﺳﺘﻔﺎﺩﻩ ﺍﺯ ﺩﺳﺘﻮﺭﺍﺕ ﺑﺮﻧﺎﻣﻪ ﺑﻪ ﻫﺮ ﻛﺪﺍﻡ ﺍﺯ ﻋﻨﺎﺻﺮ ﺁﺭﺍﻳﻪ ﺩﺳﺘﺮﺳﻲ ﺩﺍﺷﺘﻪ ﺑﺎﺷﻴﻢ‪.‬‬
‫ﺍﻧﺪﻳﺲ ﻋﻨﺎﺻﺮ ﺁﺭﺍﻳﻪ ‪ name‬ﺑﻪ ﺍﻳﻦ ﺻﻮﺭﺕ ﺑﺮﺍﻱ ‪ PHP‬ﺷﻨﺎﺳﺎﻳﻲ ﻣﻲﺷﻮﻧﺪ‪:‬‬
‫"‪$name [0] = "Maryam‬‬
‫"‪$name [1] = "Zahra‬‬
‫"‪$name [2] = "Meisam‬‬

‫>‪<html‬‬
‫>‪<head‬‬
‫>‪<title>first program</title‬‬
‫>‪</head‬‬
‫>‪<body‬‬

‫‪<?php‬‬
‫ﺷﻜﻞ ‪6-3‬‬
‫;)"‪$name= array("maryam","zahra","meisam‬‬

‫;)" ]‪echo(" $name[0] "." $name[1] "." $name[2‬‬

‫;)" ]‪echo("<br>"."<br>"." $name[0] "."<br>"." $name[1] "."<br>"." $name[2‬‬

‫>?‬

‫>‪</body‬‬
‫>‪</html‬‬

‫ﺗﻤﺮﻳﻦ‪:‬‬
‫ﻳﻚ ﺁﺭﺍﻳﻪ ﻋﺪﺩﻱ ﺑﺎ ‪ 4‬ﻋﻨﺼﺮ ﺗﻌﺮﻳﻒﻛﻨﻴﺪ‪ ،‬ﻋﻨﺎﺻﺮﺭﺍ ﺑﺎ ﺍﻋﺪﺍﺩ ‪ 9‬ﺗﺎ ‪ 5‬ﻣﻘﺪﺍﺭﺩﻫﻲﻛﺮﺩﻩ‪،‬‬
‫ﺳﭙﺲ ﺑﺎ ﺍﺳﺘﻔﺎﺩﻩ ﺍﺯ ﺍﻧﺪﻳﺲ ﺁﺭﺍﻳﻪﻫﺎ‪ ،‬ﺁﻥﻫﺎ ﺭﺍ ﺑﺎ ﻫﻢ ﺟﻤﻊ ﻛﺮﺩﻩ‪ ،‬ﻧﺘﻴﺠﻪ ﺣﺎﺻﻞ ﺟﻤﻊ ﺭﺍ‬
‫ﺩﺭ ﺧﺮﻭﺟﻲ ﻧﻤﺎﻳﺶ ﺩﻫﻴﺪ‪.‬‬
‫ﻭﺯﺍﺭﺕ ﻓﺮﻫﻨﮓ ﻭ ﺍﺭﺷﺎﺩ ﺍﺳﻼﻣﻰ‬
‫ﻧﺎﻡ ﺍﺳﺘﺎﻧﺪﺍﺭﺩ‪ :‬ﻃﺮﺍﺡ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫ﺷﻤﺎﺭﻩ‪ -15 / 4/ 2 :‬ﻑ‪ ،‬ﻫـ‬ ‫‪126‬‬

‫‪ 6-2-2‬ﻣﺘﻐﻴﺮﻫﺎﻱ ﺍﺯ ﭘﻴﺶ ﺗﻌﺮﻳﻒ ﺷﺪﻩ‬


‫ﻣﺘﻐﻴﺮﻫﺎﻱ ﺍﺯ ﭘﻴﺶ ﺗﻌﺮﻳﻒ ﺷــﺪﻩ ﻣﺘﻐﻴﺮﻫﺎﻳﻲ ﻫﺴــﺘﻨﺪ ﻛﻪ ﺩﺭ ﻳﻜﻲ ﺍﺯ ﺑﺮﻧﺎﻣﻪﻫﺎﻱ ﻛﺎﺭﺑﺮﺩﻱ‬
‫ﺳﺮﻭﻳﺲﺩﻫﻨﺪﻩ ﻭﺏ ﺍﺳﺘﻔﺎﺩﻩ ﺷﺪﻩﺍﻧﺪ ﻭ ﺑﺮﻧﺎﻣﻪﻧﻮﻳﺲ ﻣﺠﺎﺯ ﻧﻴﺴﺖ ﺍﺯ ﻧﺎﻡ ﺁﻥﻫﺎ ﺑﺮﺍﻱ ﻣﺘﻐﻴﺮﻫﺎﻱ‬
‫ﺧﻮﺩ ﺍﺳﺘﻔﺎﺩﻩ ﻛﻨﺪ‪.‬‬
‫ﻣﻬﻢﺗﺮﻳﻦ ﻣﺘﻐﻴﺮﻫﺎﻱ ﺍﺯ ﭘﻴﺶ ﺗﻌﺮﻳﻒ ﺷﺪﻩ ﻋﺒﺎﺭﺗﻨﺪ ﺍﺯ‪:‬‬
‫‪ :OSTYPE‬ﻧﻮﻉ ﺳﻴﺴﺘﻢﻋﺎﻣﻠﻲ ﺭﺍ ﻛﻪ ﺭﻭﻱ ﺳﺮﻭﻳﺲﺩﻫﻨﺪﻩ ﺩﺭ ﺣﺎﻝ ﺍﺟﺮﺍ ﺍﺳﺖ ﺩﺭ ﺑﺮ ﻣﻲﮔﻴﺮﺩ‪.‬‬
‫‪ :HOSTNAME‬ﻧﺎﻡ ﺳﺮﻭﻳﺲﺩﻫﻨﺪﻩ ﺭﺍ ﺩﺭ ﺑﺮ ﻣﻲﮔﻴﺮﺩ‪.‬‬
‫‪ :PHP-SELF‬ﻧﺎﻡ ﺻﻔﺤﻪ ﺟﺎﺭﻱ ﺭﺍ ﺩﺭ ﺑﺮ ﻣﻲﮔﻴﺮﺩ‪.‬‬

‫‪ 6-2-3‬ﺛﺎﺑﺖﻫﺎ‬
‫ﻫﻤﺎﻥﮔﻮﻧﻪ ﻛﻪ ﺍﺯ ﻧﺎﻡ ﺛﺎﺑﺖﻫﺎ ﭘﻴﺪﺍﺳــﺖ‪ ،‬ﻣﻘﺎﺩﻳﺮﻱ ﻫﺴﺘﻨﺪ ﻛﻪ ﺩﺭ ﻃﻮﻝ ﺍﺟﺮﺍﻱ ﺑﺮﻧﺎﻣﻪ ﺛﺎﺑﺖ‬
‫ﺑﻮﺩﻩ ﻭ ﻣﻘﺪﺍﺭ ﺁﻥﻫﺎ ﺗﻐﻴﻴﺮ ﻧﻤﻲﻛﻨﺪ‪.‬‬
‫ﺛﺎﺑﺖﻫﺎ ﺑﻪﻃﻮﺭ ﺳﺮﺍﺳﺮﻱ ﻭ ﺩﺭ ﻫﻤﻪ ﺟﺎﻱ ﺑﺮﻧﺎﻣﻪ ﻗﺎﺑﻞ ﺍﺳﺘﻔﺎﺩﻩ ﻭ ﺩﺭ ﺩﺳﺘﺮﺱ ﻫﺴﺘﻨﺪ‪.‬‬
‫ﺑﺮﺍﻱ ﺗﻌﺮﻳﻒ ﺛﺎﺑﺖﻫﺎ ﺍﺯ ﺗﺎﺑﻊ ‪ define‬ﺍﺳﺘﻔﺎﺩﻩ ﻣﻲﺷﻮﺩ ﻭ ﻧﻴﺎﺯ ﺑﻪ ﺩﺭﺝ ﻋﻼﻣﺖ ‪$‬ﻧﺪﺍﺭﻧﺪ‪.‬‬
‫ﻫﻨﮕﺎﻡ ﺍﺳــﺘﻔﺎﺩﻩ ﺍﺯ ﺛﺎﺑﺖﻫﺎ ﺩﺭ ﻃﻮﻝ ﺑﺮﻧﺎﻣﻪ ﻧﻴﺰ ﺑﻪ ﻋﻼﻣﺖ ‪$‬ﻧﻴﺎﺯ ﻧﺪﺍﺷﺘﻪ ﻭ ﺗﻨﻬﺎ ﻧﻮﺷﺘﻦ ﻧﺎﻡ‬
‫ﻣﺘﻐﻴﺮ ﻛﺎﻓﻲ ﺍﺳﺖ‪.‬‬
‫ﺳﺎﺧﺘﺎﺭ ﻛﻠﻲ ﺗﻌﺮﻳﻒ ﺛﺎﺑﺖﻫﺎ ﺑﻪﺻﻮﺭﺕ ﺯﻳﺮ ﺍﺳﺖ‪:‬‬
‫ﻣﺜﺎﻝ‪:‬‬
‫; )"ﻣﻘﺪﺍﺭ ﺛﺎﺑﺖ"‪",‬ﻧﺎﻡ ﺛﺎﺑﺖ"( ‪define‬‬

‫; )"‪define("PI","3.14‬‬

‫ﻣﺜــﺎﻝ ﺯﻳﺮ ﻋﺪﺩ ‪ 3/14‬ﺭﺍ ﺩﺭ ﻣﺘﻐﻴﺮﻱ ﺑﻪ ﻧﺎﻡ ‪ pi‬ﺑﻪﻃﻮﺭ ﺛﺎﺑﺖ ﻗﺮﺍﺭ ﻣﻲﺩﻫﺪ‪ ،‬ﺷــﻌﺎﻉ ﺩﺍﻳﺮﻩ ﺭﺍ‬
‫ﻣﺘﻐﻴﺮﻱ ﺻﺤﻴﺢ ﻭ ﺑﺎ ﻋﺪﺩ ‪ 5‬ﻣﻘﺪﺍﺭﺩﻫﻲ ﻣﻲﻛﻨﺪ‪ ،‬ﺳــﭙﺲ ﻣﺤﻴﻂ ﺩﺍﻳﺮﻩ ﺭﺍ ﻣﺤﺎﺳــﺒﻪ ﻛﺮﺩﻩ ﻭ ﺩﺭ‬
‫ﺧﺮﻭﺟﻲ ﻧﺸﺎﻥ ﻣﻲﺩﻫﺪ‪.‬‬
‫‪<?php‬‬

‫;)"‪define ("pi","3.14‬‬
‫;‪$r=5‬‬
‫ﭘﻴﻤﺎﻧﻪ ﻣﻬﺎﺭﺗﻰ‪ :‬ﻃﺮﺍﺣﻰ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫‪127‬‬ ‫ﻭﺍﺣﺪ ﻛﺎﺭﺷﺸﻢ‪ :‬ﻛﺎﺭ ﺑﺎ ﻣﺘﻐﻴﺮﻫﺎ‪ ،‬ﻋﻤﻠﮕﺮﻫﺎ ﻭ ﺳﺎﺧﺘﺎﺭﻫﺎﻯ ﻛﻨﺘﺮﻟﻰ‬

‫;"‪$e=pi*2*"$r‬‬

‫)"‪echo ("environment : "."$e‬‬

‫>?‬

‫ﺷﻜﻞ ‪6-4‬‬

‫‪ 6-3‬ﻋﻤﻠﮕﺮﻫﺎ‬

‫‪ 6-3-1‬ﻋﻤﻠﮕﺮﻫﺎﻱ ﻣﺤﺎﺳﺒﺎﺗﻲ‬
‫ﻋﻤﻠﮕﺮﻫﺎ ﺩﺭ ﺗﻤﺎﻡ ﺯﺑﺎﻥﻫﺎ ﺑﺮﺍﻱ ﺍﺟﺮﺍﻱ ﻣﺤﺎﺳﺒﺎﺕ ﺭﻭﻱ ﺩﺍﺩﻩﻫﺎ ﺑﻪﻛﺎﺭ ﻣﻲﺭﻭﻧﺪ‪.‬‬

‫ﺟﺪﻭﻝ ‪6-1‬‬
‫ﻣﺜﺎﻝ‬ ‫ﻋﻤﻠﮕﺮ‬
‫‪$a+$b‬‬ ‫ﺟﻤﻊ‬ ‫‪+‬‬

‫‪$a-$b‬‬ ‫ﺗﻔﺮﻳﻖ‬ ‫‪-‬‬

‫‪$a*$b‬‬ ‫ﺿﺮﺏ‬ ‫*‬

‫‪$a/$b‬‬ ‫ﺗﻘﺴﻴﻢ‬ ‫‪/‬‬

‫‪$a % $b‬‬ ‫ﺑﺎﻗﻴﻤﺎﻧﺪﻩ ﺗﻘﺴﻴﻢ‬ ‫‪%‬‬

‫‪ ++$a‬ﻳﺎ ‪$a++‬‬ ‫ﺍﻓﺰﺍﻳﺶ‬ ‫‪++‬‬

‫‪$a--‬ﻳﺎ ‪--$a‬‬ ‫ﻛﺎﻫﺶ‬ ‫‪--‬‬


‫ﻭﺯﺍﺭﺕ ﻓﺮﻫﻨﮓ ﻭ ﺍﺭﺷﺎﺩ ﺍﺳﻼﻣﻰ‬
‫ﻧﺎﻡ ﺍﺳﺘﺎﻧﺪﺍﺭﺩ‪ :‬ﻃﺮﺍﺡ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫ﺷﻤﺎﺭﻩ‪ -15 / 4/ 2 :‬ﻑ‪ ،‬ﻫـ‬ ‫‪128‬‬

‫‪ 6-3-2‬ﻋﻤﻠﮕﺮﻫﺎﻱ ﻣﻘﺎﻳﺴﻪﺍﻱ‬
‫ﺟﺪﻭﻝ ‪6-2‬‬
‫ﻣﺜﺎﻝ‬ ‫ﻋﻤﻠﮕﺮ‬
‫‪$a == $b‬‬ ‫ﺗﺴﺎﻭﻱ‬ ‫==‬

‫‪$a === $b‬‬ ‫ﺗﺴﺎﻭﻱ‬ ‫===‬

‫‪$a != $b‬‬ ‫ﻧﺎﻣﺴﺎﻭﻱ‬ ‫=!‬

‫‪$a <> $b‬‬ ‫ﻧﺎﻣﺴﺎﻭﻱ‬ ‫><‬

‫‪$a < $b‬‬ ‫ﻛﻮﭼﻚﺗﺮ ﺍﺯ‬ ‫<‬

‫‪$a > $b‬‬ ‫ﺑﺰﺭگﺗﺮ ﺍﺯ‬ ‫>‬

‫‪$a <= $b‬‬ ‫ﻛﻮﭼﻚﺗﺮ ﻳﺎ ﻣﺴﺎﻭﻱ‬ ‫=<‬

‫‪$a >= $b‬‬ ‫ﺑﺰﺭگﺗﺮ ﻳﺎ ﻣﺴﺎﻭﻱ‬ ‫=>‬

‫ﺑﺎ ﻫﻢ ﻣﺴــﺎﻭﻱ ﻫﺴــﺘﻨﺪ ﻳﺎ ﺧﻴﺮ‪،‬‬ ‫‪ $a‬ﻭ‪$b‬‬ ‫ﺍﮔﺮ ﺑﺨﻮﺍﻫﻴﺪ ﻣﺸــﺨﺺ ﻛﻨﻴﺪ ﻛﻪ ﺁﻳﺎ ﺩﻭ ﻣﺘﻐﻴﺮ‬
‫ﻣﻲﺗﻮﺍﻧﻴﺪ ﺍﺯ ﻋﻤﻠﮕﺮ == ﺍﺳﺘﻔﺎﺩﻩ ﻛﻨﻴﺪ‪.‬‬
‫;‪1. $a= 5‬‬ ‫ﻣﺜﺎﻝ ‪:1‬‬
‫;‪2. $b=5‬‬
‫;‪3.$c=10‬‬
‫;‪4. $a == $b‬‬
‫;‪5.$b==$c‬‬
‫ﺩﺭ ﺩﺳﺘﻮﺭ ﺳﻄﺮ ‪ 4‬ﭼﻮﻥ ‪ $a‬ﺑﺎ‪ $b‬ﺑﺮﺍﺑﺮ ﻫﺴﺘﻨﺪ ﻣﻘﺪﺍﺭ ‪ true‬ﺑﺮﮔﺮﺩﺍﻧﺪﻩ ﻣﻲﺷﻮﺩ‪.‬‬
‫ﺍﻣﺎ ﺩﺭ ﺳــﻄﺮ ‪ 5‬ﺑﻪ ﺩﻟﻴﻞ ﺍﻳﻦﻛﻪ ‪ $c‬ﻭ ‪ $b‬ﺑﺎ ﻫﻢ ﻣﺴــﺎﻭﻱ ﻧﻴﺴــﺘﻨﺪ ﻣﻘــﺪﺍﺭ‪ false‬ﺑﺮﮔﺮﺩﺍﻧﺪﻩ‬
‫ﻣﻲﺷﻮﺩ‪.‬‬
‫ﻣﺜﺎﻝ ‪:2‬‬
‫;‪$a = 3‬‬
‫;"‪$b = "3‬‬
‫;‪$a == $b‬‬
‫ﻧﻮﻉ ﺭﺷﺘﻪﺍﻱ ﺑﻪ ﻧﻮﻉ‬ ‫ﺩﺭ ﺩﺳــﺘﻮﺭ ﺳــﻮﻡ ﺍﺭﺯﺵ‪ true‬ﺑﺮﮔﺮﺩﺍﻧﺪﻩ ﻣﻲﺷﻮﺩ ﺯﻳﺮﺍ ﺍﺑﺘﺪﺍ ﻣﺘﻐﻴﺮ‪$b‬‬
‫ﻋﺪﺩﻱ ﺗﺒﺪﻳﻞ ﻣﻲﺷﻮﺩ ﻭ ﺳﭙﺲ ﻣﻘﺎﻳﺴﻪ ﺻﻮﺭﺕ ﻣﻲﮔﻴﺮﺩ‪.‬‬
‫ﭘﻴﻤﺎﻧﻪ ﻣﻬﺎﺭﺗﻰ‪ :‬ﻃﺮﺍﺣﻰ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫‪129‬‬ ‫ﻭﺍﺣﺪ ﻛﺎﺭﺷﺸﻢ‪ :‬ﻛﺎﺭ ﺑﺎ ﻣﺘﻐﻴﺮﻫﺎ‪ ،‬ﻋﻤﻠﮕﺮﻫﺎ ﻭ ﺳﺎﺧﺘﺎﺭﻫﺎﻯ ﻛﻨﺘﺮﻟﻰ‬

‫ﺍﮔﺮ ﺑﺨﻮﺍﻫﻴﻢ ﺗﺒﺪﻳﻞ ﻧﻮﻉ ﺻﻮﺭﺕ ﻧﮕﻴﺮﺩ ﻭ ﻣﻘﺎﻳﺴﻪ ﺑﺎ ﺍﻧﻮﺍﻉ ﻳﻜﺴﺎﻥ ﺻﻮﺭﺕ ﮔﻴﺮﺩ‪ ،‬ﺑﺎﻳﺪ‬
‫;‪$a= 7‬‬ ‫ﺍﺯ ﻋﻤﻠﮕﺮ === ﺍﺳﺘﻔﺎﺩﻩ ﻛﻨﻴﻢ‪:‬‬
‫;"‪$b = "7‬‬
‫;‪$a === $b‬‬
‫ﺭﺍ ﺑﺮﻣﻲﮔﺮﺩﺍﻧﺪ‪ ،‬ﺯﻳﺮﺍ ‪ $a‬ﻭ ‪ $b‬ﻫﻤﻨﻮﻉ ﻧﻴﺴﺘﻨﺪ‪.‬‬ ‫ﻣﻘﺪﺍﺭ‪false‬‬ ‫ﺩﺳﺘﻮﺭ ﺳﻮﻡ‬

‫‪ 6-3-3‬ﻋﻤﻠﮕﺮﻫﺎﻱ ﻣﻨﻄﻘﻲ‬
‫ﻋﻤﻠﮕﺮﻫــﺎﻱ ﻣﻨﻄﻘــﻲ ﺭﻭﻱ ﻋﺒﺎﺭﺍﺕ ﻣﻨﻄﻘﻲ ﻋﻤﻞ ﻣﻲﻛﻨﻨﺪ‪ .‬ﺑﻪ ﻋﻨــﻮﺍﻥ ﻣﺜﺎﻝ ﺍﮔﺮ ﺑﺨﻮﺍﻫﻴﻢ‬
‫ﺗﺸــﺨﻴﺺ ﺩﻫﻴﻢ ﻣﻘﺪﺍﺭ ﻣﺘﻐﻴﺮ‪ $a‬ﺑﻴﻦ ‪ 1‬ﻭ ‪ 100‬ﻗﺮﺍﺭ ﺩﺍﺭﺩ ﺑﺎﻳﺪ ﺍﺯ ﻋﻤﻠﮕﺮﻫﺎﻱ ﻣﻨﻄﻘﻲ ﺍﺳﺘﻔﺎﺩﻩ‬
‫ﻛﻨﻴﻢ‪ .‬ﺍﻳﻦ ﻋﻤﻠﮕﺮﻫﺎ ﺭﺍ ﺩﺭ ﺟﺪﻭﻝ ﺯﻳﺮ ﻣﺸﺎﻫﺪﻩ ﻣﻲﻧﻤﺎﻳﻴﺪ‪:‬‬
‫ﺟﺪﻭﻝ ‪6-3‬‬
‫ﻧﺘﻴﺠﻪ‬ ‫ﻣﺜﺎﻝ‬ ‫ﻧﺎﻡ‬ ‫ﻋﻤﻠﮕﺮ‬
‫ﺍﮔﺮ ‪ $b‬ﻧﺎﺩﺭﺳﺖ ﺑﺎﺷﺪ‪ ،‬ﻧﺘﻴﺠﻪ ﺩﺭﺳﺖ ﺍﺳﺖ ﻭ ﺑﺮﻋﻜﺲ‪.‬‬ ‫‪$b‬‬ ‫ﻧﻘﻴﺾ )‪(NOT‬‬ ‫|‬
‫ﺍﮔﺮ ‪ $a‬ﻭ ‪ $b‬ﻫﺮ ﺩﻭ ﺩﺭﺳﺖ ﺑﺎﺷﻨﺪ ﻧﺘﻴﺠﻪ ﺩﺭﺳﺖ ﺍﺳﺖ ﺩﺭ‬
‫‪ $a && $b‬ﻏﻴﺮ ﺍﻳﻦﺻﻮﺭﺕ ﻧﺎﺩﺭﺳﺖ ﺍﺳﺖ‪.‬‬ ‫ﻭ )‪(AND‬‬ ‫&&‬

‫ﺍﮔﺮ ﺣﺪﺍﻗﻞ ﻳﻜﻲ ﺍﺯ ﻣﺘﻐﻴﺮﻫﺎﻱ‪ a$‬ﻳﺎ ‪ b$‬ﺩﺭﺳﺖ ﺑﺎﺷﻨﺪ‪،‬‬


‫ﻧﺘﻴﺠﻪ ﺩﺭﺳﺖ ﺍﺳﺖ ﻭ ﺩﺭ ﻏﻴﺮ ﺍﻳﻦﺻﻮﺭﺕ ﻧﺎﺩﺭﺳﺖ ﺍﺳﺖ‪.‬‬ ‫‪$a || $b‬‬ ‫ﻳﺎ )||(‬ ‫||‬

‫‪ 6-3-4‬ﻋﻤﻠﮕﺮﻫﺎﻱ ﺗﺮﻛﻴﺒﻲ‬
‫ﺗﻌﺪﺍﺩﻱ ﺍﺯ ﻋﻤﻠﮕﺮﻫﺎ ﺩﺭ ‪ PHP‬ﻭﺟﻮﺩ ﺩﺍﺭﻧﺪ ﻛﻪ ﺗﺮﻛﻴﺒﻲ ﺍﺯ ﺩﻭ ﻋﻤﻠﮕﺮ ﺩﻳﮕﺮ ﻫﺴﺘﻨﺪ ﻭ ﺑﻪ ﻧﺎﻡ‬
‫ﻋﻤﻠﮕﺮﻫﺎﻱ ﺗﺮﻛﻴﺒﻲ ﺧﻮﺍﻧﺪﻩ ﻣﻲﺷﻮﻧﺪ‪ .‬ﺍﻳﻦ ﻋﻤﻠﮕﺮﻫﺎ ﺩﺭ ﺟﺪﻭﻝ ﺯﻳﺮ ﺗﻮﺿﻴﺢ ﺩﺍﺩﻩ ﺷﺪﻩﺍﻧﺪ‪:‬‬

‫ﺟﺪﻭﻝ ‪6-4‬‬
‫ﻋﺒﺎﺭﺕ ﻣﻌﺎﺩﻝ‬ ‫ﻣﺜﺎﻝ‬ ‫ﻧﺎﻡ‬ ‫ﻋﻤﻠﮕﺮ‬
‫‪$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‬‬ ‫ﺍﻧﺘﺴﺎﺏ ﺑﺎﻗﻴﻤﺎﻧﺪﻩ ﺗﻘﺴﻴﻢ‬ ‫=‪%‬‬


‫ﻭﺯﺍﺭﺕ ﻓﺮﻫﻨﮓ ﻭ ﺍﺭﺷﺎﺩ ﺍﺳﻼﻣﻰ‬
‫ﻧﺎﻡ ﺍﺳﺘﺎﻧﺪﺍﺭﺩ‪ :‬ﻃﺮﺍﺡ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫ﺷﻤﺎﺭﻩ‪ -15 / 4/ 2 :‬ﻑ‪ ،‬ﻫـ‬ ‫‪130‬‬

‫‪ 6-3-5‬ﻋﻤﻠﮕﺮﻫﺎﻱ ﺭﺷﺘﻪﺍﻱ‬
‫ﻳﻜﻲ ﺍﺯ ﭘﺮﺍﺳــﺘﻔﺎﺩﻩﺗﺮﻳﻦ ﻋﻤﻠﻴﺎﺗﻲ ﻛﻪ ﺭﻭﻱ ﺭﺷــﺘﻪﻫﺎ ﺍﻧﺠﺎﻡ ﻣﻲﮔﻴــﺮﺩ‪ ,‬ﻋﻤﻞ ﺍﻟﺤﺎﻕ ﻛﺮﺩﻥ‬
‫ﺭﺷﺘﻪﻫﺎﺳﺖ‪ .‬ﺑﻪ ﻋﻤﻞ ﭼﺴﺒﺎﻧﺪﻥ ﺩﻭ ﺭﺷﺘﻪ ﺑﻪ ﻳﻜﺪﻳﮕﺮ ﻋﻤﻞ ﺍﻟﺤﺎﻕ ﺭﺷﺘﻪﻫﺎ ﮔﻔﺘﻪ ﻣﻲﺷﻮﺩ‪.‬‬
‫ﺑﻪ ﻋﻨﻮﺍﻥ ﻣﺜﺎﻝ ﺩﻭ ﺭﺷﺘﻪ ﺯﻳﺮ ﺭﺍ ﺩﺭ ﻧﻈﺮ ﺑﮕﻴﺮﻳﺪ‪.‬‬
‫;"‪$a="web‬‬
‫;"‪$b="design‬‬

‫ﺑﺮﺍﻱ ﺍﻟﺤﺎﻕ ﺭﺷﺘﻪﻫﺎ ﺩﺭ ‪ PHP‬ﺍﺯ ﻋﻤﻠﮕﺮ ‪ .‬ﺍﺳﺘﻔﺎﺩﻩ ﻣﻲﺷﻮﺩ‪:‬‬


‫;‪$c=$a . $b‬‬

‫ﺧﻮﺍﻫﺪ ﺑﻮﺩ‪.‬‬ ‫ﺭﺷﺘﻪ‪”webdesign”، $c‬‬ ‫ﺑﻪ ﺍﻳﻦ ﺗﺮﺗﻴﺐ ﺣﺎﺻﻞ‬


‫ﺩﺭ ‪ PHP‬ﺩﻭ ﻧﻮﻉ ﺭﺷﺘﻪ ﻭﺟﻮﺩ ﺩﺍﺭﺩ‪:‬‬
‫ﺭﺷﺘﻪﻫﺎﻳﻲ ﻛﻪ ﺑﻴﻦ ﺩﻭ ﻋﻼﻣﺖ "" ﻗﺮﺍﺭ ﻣﻲﮔﻴﺮﻧﺪ‪ .‬ﻣﺜﻞ‪" webdesign":‬‬

‫ﺭﺷﺘﻪﻫﺎﻳﻲ ﻛﻪ ﺑﻴﻦ ﺩﻭ ﻋﻼﻣﺖ' ' ﻗﺮﺍﺭ ﻣﻲﮔﻴﺮﻧﺪ‪ .‬ﻣﺜﻞ‪' webdesign' :‬‬

‫ﺍﮔﺮ ﺭﺷــﺘﻪﺍﻱ ﺩﺭ ﻧﻘﻞ ﻗﻮﻝ ﺩﻭﺗﺎﻳﻲ ﻗﺮﺍﺭ ﮔﻴﺮﺩ‪ PHP ،‬ﺳﻌﻲ ﻣﻲﻛﻨﺪ ﺁﻥ ﺭﺍ ﻣﺜﻞ ﻳﻚ ﻋﺒﺎﺭﺕ‬
‫ﺍﺭﺯﻳﺎﺑﻲ ﻛﻨﺪ‪ ،‬ﻭﻟﻲ ﺍﮔﺮ ﺭﺷﺘﻪ ﺩﺭ ﻧﻘﻞ ﻗﻮﻝ ﻳﻜﺎﻧﻲ ﻗﺮﺍﺭ ﮔﻴﺮﺩ‪ ،‬ﺍﺭﺯﻳﺎﺑﻲ ﻧﻤﻲﺷﻮﺩ‪ .‬ﺑﺮﺍﻱ ﻣﺜﺎﻝ ﺑﻪ‬
‫ﺩﺳﺘﻮﺭﺍﺕ ﺯﻳﺮ ﻧﮕﺎﻩ ﻛﻨﻴﺪ‪:‬‬
‫;‪1.$a=10‬‬
‫;"‪2.echo "value of $a‬‬
‫;'‪3.echo 'value of $a‬‬
‫ﺩﺳــﺘﻮﺭ ﺳــﻄﺮ ﺍﻭﻝ ﻣﻘﺪﺍﺭ ‪ 10‬ﺭﺍ ﺩﺭ ﻣﺘﻐﻴﺮ ‪$a‬ﻗﺮﺍﺭ ﻣﻲﺩﻫﺪ‪ .‬ﺧﺮﻭﺟﻲ ﺩﺳــﺘﻮﺭ ﺳﻄﺮ ﺩﻭﻡ‬
‫ﺑﻪﺻﻮﺭﺕ ﺯﻳﺮ ﺍﺳﺖ‪:‬‬
‫‪Value of 10‬‬

‫ﺩﺭ ﺣﺎﻟﻲ ﻛﻪ ﺩﺳﺘﻮﺭ ﺳﻄﺮ ﺳﻮﻡ‪ ،‬ﺑﻪﺻﻮﺭﺕ ﺯﻳﺮ ﺍﺳﺖ‪:‬‬


‫‪Value of $a‬‬

‫‪ 6-4‬ﻛﺎﺭ ﺑﺎ ﺳﺎﺧﺘﺎﺭﻫﺎﻱ ﻛﻨﺘﺮﻟﻲ‬

‫ﺩﺭ ﻣﺒﺤﺚ ﺟﺎﻭﺍ ﺍﺳﻜﺮﻳﭙﺖ ﺑﺎ ﺳﺎﺧﺘﺎﺭﻫﺎﻱ ﻛﻨﺘﺮﻟﻲ ﺁﺷﻨﺎ ﺷﺪﻳﺪ‪ ،‬ﺩﺭ ﺍﻳﻦ ﻗﺴﻤﺖ ﺿﻤﻦ ﺑﻴﺎﻥ‬
‫ﻧﻜﺎﺕ ﻣﺨﺘﺼﺮﻱ ﺟﻬﺖ ﻳﺎﺩﺁﻭﺭﻱ ﻣﻄﺎﻟﺐ‪ ،‬ﺑﻪ ﺑﺮﺭﺳﻲ ﺳﺎﺧﺘﺎﺭ ﻫﺮ ﻛﺪﺍﻡ ﺍﺯ ﺩﺳﺘﻮﺭﺍﺕ ﺩﺭ ﺯﺑﺎﻥ‬
‫ﭘﻴﻤﺎﻧﻪ ﻣﻬﺎﺭﺗﻰ‪ :‬ﻃﺮﺍﺣﻰ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫‪131‬‬ ‫ﻭﺍﺣﺪ ﻛﺎﺭﺷﺸﻢ‪ :‬ﻛﺎﺭ ﺑﺎ ﻣﺘﻐﻴﺮﻫﺎ‪ ،‬ﻋﻤﻠﮕﺮﻫﺎ ﻭ ﺳﺎﺧﺘﺎﺭﻫﺎﻯ ﻛﻨﺘﺮﻟﻰ‬

‫‪ PHP‬ﻣﻲﭘﺮﺩﺍﺯﻳﻢ‪:‬‬

‫‪ 6-4-1‬ﺩﺳﺘﻮﺭ ‪if‬‬
‫ﻭﺍﺭﺩ ﺑﻠﻮﻙ ﻛﺪ ﻣﺮﺑﻮﻁ ﺑﻪ‬ ‫ﭘﺎﺳﺦ‪true‬‬ ‫ﻳﻚ ﻋﺒﺎﺭﺕ ﺭﺍ ﺑﺮﺭﺳــﻲ ﻣﻲﻛﻨﺪ‪ ،‬ﺩﺭ ﺻﻮﺭﺕ ﺩﺍﺷﺘﻦ‬
‫‪ if‬ﻣﻲﺷﻮﺩ‪ ،‬ﺩﺭ ﻏﻴﺮ ﺍﻳﻦﺻﻮﺭﺕ ﺍﺯ ﺑﻠﻮﻙ ‪ if‬ﻣﻲﮔﺬﺭﺩ‪.‬‬
‫ﺳﺎﺧﺘﺎﺭ ﺩﺳﺘﻮﺭ ‪ if‬ﺑﻪﺻﻮﺭﺕ ﺯﻳﺮ ﺍﺳﺖ‪:‬‬
‫ﺷﺮﻃﻲ(‪if‬‬ ‫)ﻋﺒﺎﺭﺕ‬
‫{‬

‫ﺩﺳﺘﻮﺭﺍﺕ ﺑﺪﻧﻪ‬ ‫ﺩﺭ ﭘﺮﺍﻧﺘﺰ ﻣﻘﺎﺑﻞ ﺩﺳﺘﻮﺭ‪ if‬ﺑﺎﻳﺪ ﻳﻚ ﻋﺒﺎﺭﺕ ﺷﺮﻃﻲ ﮔﺬﺍﺷﺘﻪ ﺷﻮﺩ‪.‬‬
‫}‬

‫ﻣﺜﺎﻝ‪:‬‬
‫>‪<html‬‬
‫>‪<head‬‬
‫>‪<title>Listing 5.1</title‬‬
‫>‪</head‬‬
‫>‪<body‬‬
‫‪<?php‬‬
‫;"‪$a = "hello‬‬
‫) "‪if ( $a == "hello‬‬
‫{‬
‫;"‪echo "hello... welcome‬‬
‫}‬

‫>?‬
‫>‪</body‬‬
‫>‪</html‬‬
‫ﺍﮔﺮ ﺑﺨﻮﺍﻫﻴﻢ ﺩﺳﺘﻮﺭ‪ if‬ﺭﺍ ﻃﻮﺭﻱ ﺑﻨﻮﻳﺴﻴﻢ ﻛﻪ ﺩﺭ ﺻﻮﺭﺕ ﺑﺮﻗﺮﺍﺭ ﻧﺒﻮﺩﻥ ﺷﺮﻁ ﺍﻭﻝ‪ ،‬ﺷﺮﻁ‬
‫ﺩﻭﻡ‪ ،‬ﺳــﻮﻡ ﻭ ‪ ...‬ﺑﺮﺭﺳﻲ ﺷﻮﺩ ﻭ ﺩﺭ ﺻﻮﺭﺕ ﺻﺤﻴﺢ ﺑﻮﺩﻥ ﻫﺮ ﻛﺪﺍﻡ ﻛﺪﻫﺎﻱ ﻣﺨﺼﻮﺹ ﺁﻥ‬
‫ﻭﺯﺍﺭﺕ ﻓﺮﻫﻨﮓ ﻭ ﺍﺭﺷﺎﺩ ﺍﺳﻼﻣﻰ‬
‫ﻧﺎﻡ ﺍﺳﺘﺎﻧﺪﺍﺭﺩ‪ :‬ﻃﺮﺍﺡ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫ﺷﻤﺎﺭﻩ‪ -15 / 4/ 2 :‬ﻑ‪ ،‬ﻫـ‬ ‫‪132‬‬

‫ﺍﺟﺮﺍ ﺷﻮﺩ ﻣﻲﺗﻮﺍﻧﻴﻢ ﺍﺯ ﺩﺳﺘﻮﺭ‪ elseif‬ﻭ ‪ else‬ﺍﺳﺘﻔﺎﺩﻩ ﻛﻨﻴﻢ‪.‬‬


‫ﺳﺎﺧﺘﺎﺭ ﻛﻠﻲ ﺍﻳﻦ ﺩﺳﺘﻮﺭﺍﺕ ﻋﺒﺎﺭﺗﻨﺪ ﺍﺯ‪:‬‬
‫ﺍﻭﻝ(‪if‬‬ ‫)ﺷﺮﻁ‬
‫{‬
‫ﺩﺳﺘﻮﺭﺍﺕ ﺑﺪﻧﻪ‬
‫}‬
‫ﺩﻭﻡ(‪elseif‬‬ ‫)ﺷﺮﻁ‬
‫{‬
‫ﺩﺳﺘﻮﺭﺍﺕ ﺑﺪﻧﻪ‬
‫}‬
‫ﺳﻮﻡ(‪elseif‬‬ ‫)ﺷﺮﻁ‬
‫{‬
‫ﺩﺳﺘﻮﺭﺍﺕ ﺑﺪﻧﻪ‬
‫{‬
‫‪.........‬‬

‫‪else‬‬
‫}‬
‫ﺩﺳﺘﻮﺭﺍﺕ ﺑﺪﻧﻪ‬
‫}‬
‫ﺩﺭ ﺳــﺎﺧﺘﺎﺭ ﻓﻮﻕ ﺍﺑﺘﺪﺍ ﺷﺮﻁ ﻣﻘﺎﺑﻞ‪ if‬ﭼﻚ ﻣﻲﺷﻮﺩ‪ ،‬ﺍﮔﺮ ﺩﺭﺳﺖ ﺑﻮﺩ‪ ،‬ﺩﺳﺘﻮﺭﺍﺕ ﺑﺪﻧﻪ ﺁﻥ‬
‫ﺍﺟﺮﺍ ﻣﻲﺷﻮﺩ‪ ،‬ﺍﻣﺎ ﺍﮔﺮ ﻧﺎﺩﺭﺳﺖ ﺑﻮﺩ ﺷﺮﻁ ﻣﻘﺎﺑﻞ ‪ elseif‬ﺍﻭﻝ ﺍﺟﺮﺍ ﻣﻲﺷﻮﺩ‪ ،‬ﺩﺭ ﺻﻮﺭﺕ ﺻﺤﻴﺢ‬
‫ﺑﻮﺩﻥ‪ ،‬ﻛﺪﻫﺎﻱ ﺁﻥ ﺍﺟﺮﺍ ﺷــﺪﻩ‪ ،‬ﺍﻣﺎ ﺍﮔﺮ ﺻﺤﻴﺢ ﻧﺒﺎﺷــﺪ‪ ،‬ﺩﺳــﺘﻮﺭﺍﺕ‪ elseif‬ﺑﻌﺪﻱ ﺭﺍ ﺑﺮﺭﺳــﻲ‬
‫ﻣﻲﻛﻨﺪ ﻭ ﺍﻳﻦ ﻋﻤﻞ ﺑﻪ ﺗﻌﺪﺍﺩ ﺩﺳﺘﻮﺭﺍﺕ‪ elseif‬ﺍﺩﺍﻣﻪ ﭘﻴﺪﺍ ﻣﻲﻛﻨﺪ ﻭ ﺩﺭ ﻧﻬﺎﻳﺖ ﺍﮔﺮ ﻫﻴﭻﻛﺪﺍﻡ ﺍﺯ‬
‫ﺷﺮﻁﻫﺎﻱ ‪ if‬ﻭ‪ elseif‬ﺻﺤﻴﺢ ﻧﺒﻮﺩﻧﺪ‪ ،‬ﺩﺳﺘﻮﺭﺍﺕ ﻣﺮﺑﻮﻁ ﺑﻪ ﺑﺪﻧﻪ ‪ else‬ﺍﺟﺮﺍ ﻣﻲﮔﺮﺩﺩ‪.‬‬
‫ﻣﺜﺎﻝ‪:‬‬
‫>‪<html‬‬
‫>‪<head‬‬
‫>‪<title>Listing 5.3</title‬‬
‫>‪</head‬‬
‫ﭘﻴﻤﺎﻧﻪ ﻣﻬﺎﺭﺗﻰ‪ :‬ﻃﺮﺍﺣﻰ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫‪133‬‬ ‫ﻭﺍﺣﺪ ﻛﺎﺭﺷﺸﻢ‪ :‬ﻛﺎﺭ ﺑﺎ ﻣﺘﻐﻴﺮﻫﺎ‪ ،‬ﻋﻤﻠﮕﺮﻫﺎ ﻭ ﺳﺎﺧﺘﺎﺭﻫﺎﻯ ﻛﻨﺘﺮﻟﻰ‬

‫>‪<body‬‬
‫‪<?php‬‬
‫;"‪$mood = "sad‬‬
‫) "‪if ( $mood == "happy‬‬
‫{‬
‫;"‪echo "Hooray, I'm in a good mood‬‬
‫}‬
‫) "‪elseif ( $mood =="sad‬‬
‫{‬
‫;"!‪echo " I'm in a bad mood‬‬
‫}‬
‫‪else‬‬
‫{‬
‫;"‪echo "Neither happy nor sad but $mood‬‬
‫}‬
‫>?‬
‫>‪</body‬‬
‫>‪</html‬‬

‫ﭘﺲ ﺍﺯ ﺍﺟﺮﺍﻱ ﻛﺪ ﻣﻮﺭﺩ ﻧﻈﺮ ﻣﺸــﺎﻫﺪﻩ ﻣﻲﻛﻨﻴﺪ ﻛﻪ ﺑﻪ ﺩﻟﻴﻞ ﺩﺭﺳــﺖ ﺑﻮﺩﻥ ﺷــﺮﻁ ﻣﻘﺎﺑﻞ‬
‫‪ ،elseif‬ﺩﺳﺘﻮﺭﺍﺕ ﻣﺮﺑﻮﻁ ﺑﻪ ﺍﻳﻦ ﺑﻠﻮﻙ ﺍﺟﺮﺍ ﻣﻲﺷﻮﺩ‪.‬‬

‫ﺷﻜﻞ‪6-5‬‬
‫ﻭﺯﺍﺭﺕ ﻓﺮﻫﻨﮓ ﻭ ﺍﺭﺷﺎﺩ ﺍﺳﻼﻣﻰ‬
‫ﻧﺎﻡ ﺍﺳﺘﺎﻧﺪﺍﺭﺩ‪ :‬ﻃﺮﺍﺡ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫ﺷﻤﺎﺭﻩ‪ -15 / 4/ 2 :‬ﻑ‪ ،‬ﻫـ‬ ‫‪134‬‬

‫ﺗﻤﺮﻳﻦ‪:‬‬
‫ﺩﺳﺘﻮﺭﺍﺕ ﻓﻮﻕ ﺭﺍ ﻃﻮﺭﻱ ﺗﻐﻴﻴﺮ ﺩﻫﻴﺪ ﻛﻪ ﺑﻠﻮﻙ ﻣﺮﺑﻮﻁ ﺑﻪ ‪ else‬ﺍﺟﺮﺍ ﺷﻮﺩ‪.‬‬

‫‪ 6-4-2‬ﺩﺳﺘﻮﺭ‪Switch‬‬
‫ﺳﺎﺧﺘﺎﺭ ﻛﻠﻲ ﺍﻳﻦ ﺩﺳﺘﻮﺭ ﻣﺎﻧﻨﺪ ﺟﺎﻭﺍ ﺍﺳﻜﺮﻳﭙﺖ ﺍﺳﺖ‪:‬‬
‫‪Switch‬‬ ‫)ﻧﺎﻡ ﻣﺘﻐﻴﺮ(‬
‫{‬

‫‪Case‬‬ ‫‪:‬ﻣﻘﺪﺍﺭ ﺍﻭﻝ‬


‫ﺩﺳﺘﻮﺭﺍﺕ ﺑﺪﻧﻪ‬
‫;‪break‬‬

‫ﺩﻭﻡ‪Case‬‬ ‫‪:‬ﻣﻘﺪﺍﺭ‬
‫ﺩﺳﺘﻮﺭﺍﺕ ﺑﺪﻧﻪ‬
‫;‪break‬‬
‫‪.........‬‬

‫‪default‬‬

‫ﺩﺳﺘﻮﺭﺍﺕ ﺑﺪﻧﻪ‬
‫}‬

‫ﺩﺭ ﺳــﺎﺧﺘﺎﺭ‪ Switch‬ﻣﻘﺪﺍﺭ ﻣﺘﻐﻴﺮ ﻣﻘﺎﺑﻞ ‪ Switch‬ﺑﺎ ﻣﻘﺎﺩﻳﺮ ﻣﻘﺎﺑﻞ ‪Case‬ﻫﺎ ﻣﻘﺎﻳﺴﻪ ﻣﻲﺷﻮﺩ‪،‬‬
‫ﺍﮔﺮ ﺑﺎ ﻣﻘﺪﺍﺭ ﻣﻘﺎﺑﻞ ‪ Case‬ﺍﻭﻝ ﺑﺮﺍﺑﺮ ﺑﺎﺷﺪ‪ ،‬ﺩﺳﺘﻮﺭﺍﺕ ﺑﺪﻧﻪ ﺁﻥ ﺍﺟﺮﺍ ﻣﻲﺷﻮﺩ ﻭ ﺳﭙﺲ ﺍﺯ ﺑﻠﻮﻙ‬
‫‪ Switch‬ﺧﺎﺭﺝ ﻣﻲﺷــﻮﺩ‪ ،‬ﺍﮔﺮ ﺑﺮﺍﺑﺮ ﺑﺎ ﻣﻘﺪﺍﺭ ﻣﻘﺎﺑﻞ ‪ Case‬ﺩﻭﻡ ﺑﺎﺷــﺪ‪ ،‬ﺩﺳﺘﻮﺭﺍﺕ ﻣﺮﺑﻮﻁ ﺑﻪ ﺁﻥ‬
‫ﺍﺟﺮﺍ ﺷﺪﻩ ﻭ ﺍﺯ ﺑﻠﻮﻙ ‪ Switch‬ﺧﺎﺭﺝ ﻣﻲﺷﻮﺩ‪.‬‬
‫ﺑــﻪ ﻫﻤﻴﻦ ﺗﺮﺗﻴﺐ ﭼﻚ ﻛﺮﺩﻥ ﻣﻘﺎﺩﻳﺮ ﺍﺩﺍﻣﻪ ﭘﻴﺪﺍ ﻣﻲﻛﻨﺪ ﻭ ﺍﮔﺮ ﻫﻴﭻﻛﺪﺍﻡ ﺍﺯ ﻣﻘﺎﺩﻳﺮ ﺑﺮﺍﺑﺮ ﺑﺎ‬
‫ﻣﺘﻐﻴﺮ ﻣﻘﺎﺑﻞ‪ Switch‬ﻧﺒﻮﺩﻧﺪ‪ ،‬ﺩﺳﺘﻮﺭ ﺑﺪﻧﻪ‪ default‬ﺍﺟﺮﺍ ﻣﻲﺷﻮﺩ‪.‬‬
‫ﻣﺜﺎﻝ ﻗﺒﻞ ﻣﺮﺑﻮﻁ ﺑﻪ ﺷﺮﻁ‪ if … elseif‬ﺭﺍ ﺑﺎ ﺍﺳﺘﻔﺎﺩﻩ ﺍﺯ ﺩﺳﺘﻮﺭ ‪ Switch‬ﺑﺎﺯﻧﻮﻳﺴﻲ ﻣﻲﻛﻨﻴﻢ‪:‬‬
‫>‪<html‬‬
‫>‪<head‬‬
‫>‪<title>Listing 5.4</title‬‬
‫ﭘﻴﻤﺎﻧﻪ ﻣﻬﺎﺭﺗﻰ‪ :‬ﻃﺮﺍﺣﻰ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫‪135‬‬ ‫ﻭﺍﺣﺪ ﻛﺎﺭﺷﺸﻢ‪ :‬ﻛﺎﺭ ﺑﺎ ﻣﺘﻐﻴﺮﻫﺎ‪ ،‬ﻋﻤﻠﮕﺮﻫﺎ ﻭ ﺳﺎﺧﺘﺎﺭﻫﺎﻯ ﻛﻨﺘﺮﻟﻰ‬

‫>‪</head‬‬
‫>‪<body‬‬
‫‪<?php‬‬
‫;"‪$mood = "sad‬‬
‫) ‪switch ( $mood‬‬
‫{‬
‫‪case "happy":‬‬
‫;"‪echo "Hooray, I'm in a good mood‬‬
‫;‪break‬‬
‫‪case "sad":‬‬
‫;"!‪echo "I'm in a bad mood‬‬
‫;‪break‬‬
‫‪default:‬‬
‫;"‪echo "Neither happy nor sad but $mood‬‬
‫}‬
‫>?‬
‫>‪</body‬‬
‫>‪</html‬‬

‫ﭘﺲ ﺍﺯ ﺍﺟﺮﺍﻱ ﻛﺪ ﻓﻮﻕ ﺧﺮﻭﺟﻲ ﺍﻳﻦ ﺑﺮﻧﺎﻣﻪ ﻧﻴﺰ ﻣﺸﺎﺑﻪ ﺷﻜﻞ ‪ 5-5‬ﺧﻮﺍﻫﺪ ﺑﻮﺩ‪.‬‬

‫‪ 6-5‬ﺣﻠﻘﻪﻫﺎ‬

‫ﺣﻠﻘﻪﻫﺎ ﻳﻜﻲ ﺩﻳﮕﺮ ﺍﺯ ﺍﻣﻜﺎﻧﺎﺕ ﺯﺑﺎﻥﻫﺎﻱ ﺑﺮﻧﺎﻣﻪﻧﻮﻳﺴﻲ ﻫﺴﺘﻨﺪ ﻛﻪ ﺍﺟﺎﺯﻩ ﻣﻲﺩﻫﻨﺪ ﺩﺳﺘﻮﺭ‬
‫ﻳﺎ ﺩﺳﺘﻮﺭﺍﺗﻲ ﭼﻨﺪ ﺑﺎﺭ ﺍﺟﺮﺍ ﺷﻮﻧﺪ‪.‬‬

‫‪ 6-5-1‬ﺩﺳﺘﻮﺭ ‪for‬‬
‫ﺳﺎﺧﺘﺎﺭ ﻛﻠﻲ ﺩﺳﺘﻮﺭ ‪ for‬ﺑﻪﺻﻮﺭﺕ ﺯﻳﺮ ﺍﺳﺖ‪:‬‬
‫)ﻣﻴﺰﺍﻥ ﺍﻓﺰﺍﻳﺶ ﻳﺎ ﻛﺎﻫﺶ ﻣﺘﻐﻴﺮ ; ﻋﺒﺎﺭﺕ ﺷﺮﻃﻲ ; ﻣﻘﺪﺍﺭ ﺍﻭﻟﻴﻪ ﺣﻠﻘﻪ( ‪for‬‬
‫{‬
‫ﺑﺪﻧﻪ ﺣﻠﻘﻪ‬
‫}‬
‫ﻭﺯﺍﺭﺕ ﻓﺮﻫﻨﮓ ﻭ ﺍﺭﺷﺎﺩ ﺍﺳﻼﻣﻰ‬
‫ﻧﺎﻡ ﺍﺳﺘﺎﻧﺪﺍﺭﺩ‪ :‬ﻃﺮﺍﺡ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫ﺷﻤﺎﺭﻩ‪ -15 / 4/ 2 :‬ﻑ‪ ،‬ﻫـ‬ ‫‪136‬‬

‫ﺳﺎﺧﺘﺎﺭ ﺣﻠﻘﻪﻫﺎ ﺩﺭ ‪ PHP‬ﻧﻴﺰ ﺷﺒﺎﻫﺖ ﺯﻳﺎﺩﻱ ﺑﻪ ﺟﺎﻭﺍ ﺍﺳﻜﺮﻳﭙﺖ ﺩﺍﺭﺩ‪.‬‬


‫ﻣﻘﺪﺍﺭ ﺍﻭﻟﻴﻪ ﺣﻠﻘﻪ‪ ،‬ﻣﻘﺪﺍﺭﻱ ﺍﺳــﺖ ﻛﻪ ﺑﺮﺍﻱ ﻣﺘﻐﻴﺮ ﺷــﻤﺎﺭﻧﺪﻩ ﺗﻌــﺪﺍﺩ ﺩﻓﻌﺎﺕ ﺍﺟﺮﺍ ﺗﻌﻴﻴﻦ‬
‫ﻣﻲﺷﻮﺩ‪.‬‬
‫ﻋﺒﺎﺭﺕ ﺩﻭﻡ‪ ،‬ﻋﺒﺎﺭﺕ ﻛﻨﺘﺮﻟﻲ ﺍﺳﺖ ﻛﻪ ﺁﺧﺮﻳﻦ ﺑﺎﺭ ﺍﺟﺮﺍﻱ ﺣﻠﻘﻪ ﺭﺍ ﻣﻌﻴﻦ ﻣﻲﻛﻨﺪ‪.‬‬
‫ﻋﺒﺎﺭﺕ ﺳﻮﻡ ﻣﻘﺪﺍﺭ ﺍﺿﺎﻓﻪ ﻳﺎ ﻛﻢ ﺷﺪﻥ ﻣﺘﻐﻴﺮ ﺍﻭﻟﻴﻪ ﺩﺭ ﻫﺮ ﺑﺎﺭ ﺍﺟﺮﺍﻱ ﺣﻠﻘﻪ ﺭﺍ ﺗﻌﻴﻴﻦ ﻣﻲﻛﻨﺪ‪.‬‬
‫ﻣﺜﺎﻝ‪ :‬ﻣﻲﺧﻮﺍﻫﻴﻢ ﺍﻋﺪﺍﺩ ‪ 1‬ﺗﺎ ‪ 10‬ﺭﺍ ﺩﺭ ﺧﺮﻭﺟﻲ ﭼﺎپ ﻛﻨﻴﻢ‪.‬‬
‫>‪<html‬‬
‫>‪<head‬‬
‫>‪<title>for</title‬‬
‫>‪</head‬‬
‫>‪<body‬‬
‫‪<?php‬‬
‫) ‪for ( $i=1; $i<=10; $i++‬‬
‫{‬
‫;">‪echo"$i"."<br‬‬
‫}‬
‫>?‬
‫>‪</body‬‬
‫>‪</html‬‬
‫ﺷﻜﻞ‪6-6‬‬

‫ﺗﻤﺮﻳﻦ‪:‬‬
‫ﺑﺎ ﺍﺳــﺘﻔﺎﺩﻩ ﺍﺯ ﺣﻠﻘﻪ ‪ for‬ﺑﺮﻧﺎﻣﻪﺍﻱ ﺑﻨﻮﻳﺴﻴﺪ ﻛﻪ ﺍﻋﺪﺍﺩ ‪ 100‬ﺗﺎ ‪ 50‬ﺭﺍ ﺑﻪﺻﻮﺭﺕ ﻧﺰﻭﻟﻲ‬
‫ﺩﺭ ﺧﺮﻭﺟﻲ ﭼﺎپ ﻛﻨﺪ‪.‬‬

‫ﺩﺭ ﺳــﺎﺧﺘﺎﺭ ﺣﻠﻘــﻪ ‪ for‬ﻣﺎﻧﻨﺪ ﺟﺎﻭﺍ ﺍﺳــﻜﺮﻳﭙﺖ ﻣﻲﺗﻮﺍﻥ ﻣﻘﺪﺍﺭ ﺍﻭﻟﻴــﻪ ﺣﻠﻘﻪ ﺭﺍ ﻗﺒﻞ ﺍﺯ ‪for‬‬

‫ﻣﻘﺪﺍﺭﺩﻫــﻲ ﻛﺮﺩ ﻭ ﺍﺯ ﻧﻮﺷــﺘﻦ ﺁﻥ ﻣﻘﺎﺑﻞ ‪ for‬ﺻﺮﻑﻧﻈﺮ ﻛﺮﺩ‪ ،‬ﻫﻢﭼﻨﻴــﻦ ﺍﺯ ﻣﻴﺰﺍﻥ ﺍﻓﺰﺍﻳﺶ ﻳﺎ‬
‫ﻛﺎﻫﺶ ﻣﺘﻐﻴﺮ ﻛﻪ ﻣﻌﻤﻮﻻً ﺩﺭ ﭘﺮﺍﻧﺘﺰ ﻣﻘﺎﺑﻞ ﺩﺳﺘﻮﺭ ‪ for‬ﺍﻧﺠﺎﻡ ﻣﻲﺷﻮﺩ ﻣﻲﺗﻮﺍﻥ ﺻﺮﻑﻧﻈﺮ ﻛﺮﺩﻩ‬
‫ﻭ ﻋﻤﻞ ﻛﺎﻫﺶ ﻳﺎ ﺍﻓﺰﺍﻳﺶ ﻣﺘﻐﻴﺮ ﺭﺍ ﺩﺭﻭﻥ ﺣﻠﻘﻪ ‪ for‬ﺍﻧﺠﺎﻡ ﺩﺍﺩ‪.‬‬
‫ﭘﻴﻤﺎﻧﻪ ﻣﻬﺎﺭﺗﻰ‪ :‬ﻃﺮﺍﺣﻰ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫‪137‬‬ ‫ﻭﺍﺣﺪ ﻛﺎﺭﺷﺸﻢ‪ :‬ﻛﺎﺭ ﺑﺎ ﻣﺘﻐﻴﺮﻫﺎ‪ ،‬ﻋﻤﻠﮕﺮﻫﺎ ﻭ ﺳﺎﺧﺘﺎﺭﻫﺎﻯ ﻛﻨﺘﺮﻟﻰ‬

‫ﻣﺜﺎﻝ‪:‬‬
‫>‪<html‬‬
‫>‪<head‬‬
‫>‪<title>for</title‬‬
‫>‪</head‬‬
‫>‪<body‬‬
‫‪<?php‬‬
‫;‪$i=1‬‬
‫) ;‪for ( ; $i<=10‬‬
‫{‬
‫;">‪echo "$i"."<br‬‬
‫;‪$i++‬‬
‫}‬
‫>?‬
‫>‪</body‬‬
‫>‪</html‬‬
‫‪ 6-5-2‬ﺣﻠﻘﻪ ‪while‬‬
‫ﺳﺎﺧﺘﺎﺭ ﻛﻠﻲ ﺍﻳﻦ ﺣﻠﻘﻪ ﺑﻪﺻﻮﺭﺕ ﺯﻳﺮ ﺍﺳﺖ‪:‬‬
‫‪while‬‬ ‫)ﺷﺮﻁ ﺧﺎﺗﻤﻪ(‬
‫{‬
‫ﺩﺳﺘﻮﺭﺍﺕ ﺑﺪﻧﻪ‬
‫}‬

‫ﻣﺜﺎﻝ‪ :‬ﻣﻲﺧﻮﺍﻫﻴﻢ ﺗﻮﺳﻂ ﺩﺳﺘﻮﺭ ‪ while‬ﻣﻀﺎﺭﺏ ‪ 2‬ﻣﺮﺑﻮﻁ ﺑﻪ ﺍﻋﺪﺍﺩ ﻛﻮﭼﻚﺗﺮ ﺍﺯ ‪ 15‬ﺭﺍ‬


‫ﺩﺭ ﺧﺮﻭﺟﻲ ﻧﻤﺎﻳﺶ ﺩﻫﻴﻢ‪.‬‬
‫>‪<html‬‬
‫>‪<head‬‬
‫>‪<title>while</title‬‬
‫>‪</head‬‬
‫>‪<body‬‬
‫ﻭﺯﺍﺭﺕ ﻓﺮﻫﻨﮓ ﻭ ﺍﺭﺷﺎﺩ ﺍﺳﻼﻣﻰ‬
‫ﻧﺎﻡ ﺍﺳﺘﺎﻧﺪﺍﺭﺩ‪ :‬ﻃﺮﺍﺡ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫ﺷﻤﺎﺭﻩ‪ -15 / 4/ 2 :‬ﻑ‪ ،‬ﻫـ‬ ‫‪138‬‬

‫‪<?php‬‬
‫;‪$i = 1‬‬
‫) ‪while ( $i <= 15‬‬
‫{‬
‫;">‪echo "i = "."$i is : ".($i*2)."<br‬‬
‫;‪$i=$i+1‬‬
‫}‬
‫>?‬
‫>‪</body‬‬
‫>‪</html‬‬
‫ﺷﻜﻞ ‪6-7‬‬

‫‪ 6-5-3‬ﺣﻠﻘﻪ ‪do … while‬‬


‫ﺳﺎﺧﺘﺎﺭ ﺍﻳﻦ ﺣﻠﻘﻪ ﻧﻴﺰ ﻛﺎﻣ ً‬
‫ﻼ ﺷﺒﻴﻪ ﺑﻪ ‪ do … while‬ﺩﺭ ﺟﺎﻭﺍ ﺍﺳﻜﺮﻳﭙﺖ ﺍﺳﺖ‪.‬‬
‫ﺍﻳﻦ ﺣﻠﻘﻪ ﺩﺭ ﺗﻤﺎﻡ ﺯﺑﺎﻥﻫﺎ ﺣﺪﺍﻗﻞ ﻳﻚﺑﺎﺭ ﺍﺟﺮﺍ ﻣﻲﺷــﻮﺩ‪ ،‬ﭼﻮﻥ ﺷــﺮﻁ ﺑﺮﺭﺳــﻲ ﺍﺩﺍﻣﻪ ﺩﺭ‬
‫ﺍﻧﺘﻬﺎﻱ ﺣﻠﻘﻪ ﻧﻮﺷﺘﻪ ﺷﺪﻩ ﺍﺳﺖ‪.‬‬
‫{ ‪do‬‬

‫ﺩﺳﺘﻮﺭﺍﺕ ﺑﺪﻧﻪ‬
‫}‬

‫ﺷﺮﻃﻲ(‪while‬‬ ‫; )ﻋﺒﺎﺭﺕ‬
‫ﻣﺜﺎﻝ‪:‬‬
‫>‪<html‬‬
‫>‪<head‬‬
‫>‪<title>do...while</title‬‬
‫>‪</head‬‬
‫>‪<body‬‬
‫‪<?php‬‬
‫;‪$n1 = 8‬‬
‫‪do‬‬
‫ﭘﻴﻤﺎﻧﻪ ﻣﻬﺎﺭﺗﻰ‪ :‬ﻃﺮﺍﺣﻰ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫‪139‬‬ ‫ﻭﺍﺣﺪ ﻛﺎﺭﺷﺸﻢ‪ :‬ﻛﺎﺭ ﺑﺎ ﻣﺘﻐﻴﺮﻫﺎ‪ ،‬ﻋﻤﻠﮕﺮﻫﺎ ﻭ ﺳﺎﺧﺘﺎﺭﻫﺎﻯ ﻛﻨﺘﺮﻟﻰ‬

‫{‬
‫;">‪print "number: "." $n1"."<br‬‬
‫;‪$n1++‬‬
‫}‬
‫;) ‪while ( $n1 > 10 && $n1 < 15‬‬
‫>?‬
‫>‪</body‬‬
‫>‪</html‬‬

‫ﺷﻜﻞ ‪6-8‬‬

‫ﺩﺭ ﻛﺪ ﻗﺒﻠﻲ ﻣﺘﻐﻴﺮ ﺍﻭﻟﻴﻪ ‪ n1‬ﺑﺎ ﻋﺪﺩ ‪ 8‬ﻣﻘﺪﺍﺭﺩﻫﻲ ﺍﻭﻟﻴﻪ ﺷــﺪﻩ ﺍﺳــﺖ ﻭ ﺩﺭ ﻋﺒﺎﺭﺕ ﺷﺮﻃﻲ‬
‫ﻣﻘﺎﺑﻞ ‪ ،while‬ﺑﺮﺭﺳﻲ ﺷﺪﻩ ﺍﺳﺖ ﻛﻪ ﺗﺎ ﺯﻣﺎﻧﻲ ﻛﻪ ﻣﺘﻐﻴﺮ ‪ n1‬ﺑﺰﺭگﺗﺮ ﺍﺯ ‪ 10‬ﻭ ﻛﻮﭼﻚﺗﺮ ﺍﺯ ‪15‬‬
‫ﺍﺳﺖ ﺣﻠﻘﻪ ﺍﺩﺍﻣﻪ ﻳﺎﺑﺪ‪ ،‬ﺍﻣﺎ ﺩﺭ ﺷﻜﻞ ‪ 6-8‬ﻣﻼﺣﻈﻪ ﻣﻲﻛﻨﻴﺪ ﺑﺎ ﺍﻳﻦﻛﻪ ﺍﻳﻦ ﺷﺮﻁ ﻫﺮﮔﺰ ﺻﺤﻴﺢ‬
‫ﻧﻴﺴﺖ‪ ،‬ﻳﻚﺑﺎﺭ ﺍﺟﺮﺍ ﺷﺪﻩ ﺍﺳﺖ‪.‬‬

‫‪ 6-6‬ﺩﺳﺘﻮﺭﺍﺕ ‪ break‬ﻭ ‪continue‬‬

‫ﻋﻤﻠﻜﺮﺩ ﺍﻳﻦ ﺩﺳﺘﻮﺭﺍﺕ ﻣﺎﻧﻨﺪ ﺟﺎﻭﺍ ﺍﺳﻜﺮﻳﭙﺖ ﺍﺳﺖ‪.‬‬


‫ﺩﺳﺘﻮﺭ ‪ break‬ﺑﺮﺍﻱ ﺧﺮﻭﺟﻲ ﺍﺯ ﺑﻠﻮﻙ ﺟﺎﺭﻱ ﻭ ﺍﺩﺍﻣﻪ ﺑﺮﻧﺎﻣﻪ ﺍﺳﺘﻔﺎﺩﻩ ﻣﻲﺷﻮﺩ‪.‬‬
‫ﺩﺳــﺘﻮﺭ ‪ continue‬ﺑــﺮﺍﻱ ﺧﺮﻭﺝ ﺍﺯ ﺣﻠﻘﻪ ﺑﺎ ﭘﺎﺭﺍﻣﺘﺮ ﻓﻌﻠﻲ ﻭ ﺍﺩﺍﻣــﻪ ﺑﻠﻮﻙ ﺑﺎ ﻣﻘﺪﺍﺭ ﭘﺎﺭﺍﻣﺘﺮ‬
‫ﺑﻌﺪﻱ ﺍﻧﺠﺎﻡ ﻣﻲﺷﻮﺩ‪.‬‬
‫ﻭﺯﺍﺭﺕ ﻓﺮﻫﻨﮓ ﻭ ﺍﺭﺷﺎﺩ ﺍﺳﻼﻣﻰ‬
‫ﻧﺎﻡ ﺍﺳﺘﺎﻧﺪﺍﺭﺩ‪ :‬ﻃﺮﺍﺡ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫ﺷﻤﺎﺭﻩ‪ -15 / 4/ 2 :‬ﻑ‪ ،‬ﻫـ‬ ‫‪140‬‬

‫‪ 6-7‬ﻣﺘﻐﻴﺮﻫﺎﻱ ﺳﺮﺍﺳﺮﻱ‬

‫ﺍﮔﺮ ﺩﺭ ﺑﺮﻧﺎﻣﻪ ﺑﺨﻮﺍﻫﻴﺪ ﻳﻚ ﻣﺘﻐﻴﺮ ﺭﺍ ﺑﻴﻦ ﺗﻤﺎﻡ ﻗﺴــﻤﺖﻫﺎ ﺑﻪﻃﻮﺭ ﻳﻜﺴــﺎﻥ ﺍﺳﺘﻔﺎﺩﻩ ﻛﻨﻴﺪ‪،‬‬
‫ﻣﻲﺗﻮﺍﻧﻴﺪ ﺁﻥ ﺭﺍ ﺍﺯ ﻧﻮﻉ ﺳﺮﺍﺳﺮﻱ )‪ (global‬ﺗﻌﺮﻳﻒ ﻧﻤﺎﻳﻴﺪ‪:‬‬
‫ﺷﻴﻮﻩ ﺗﻌﺮﻳﻒ ﻳﻚ ﻣﺘﻐﻴﺮ ﺳﺮﺍﺳﺮﻱ ﺑﻪ ﻓﺮﻡﻫﺎﻱ ﺯﻳﺮ ﺍﺳﺖ‪:‬‬
‫; ﻧﺎﻡ ﻣﺘﻐﻴﺮ‪$global‬‬

‫; ﻣﻘﺪﺍﺭ ﻣﺘﻐﻴﺮ = ﻧﺎﻡ ﻣﺘﻐﻴﺮ‪$global‬‬


‫ﭘﻴﻤﺎﻧﻪ ﻣﻬﺎﺭﺗﻰ‪ :‬ﻃﺮﺍﺣﻰ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫‪141‬‬ ‫ﻭﺍﺣﺪ ﻛﺎﺭﺷﺸﻢ‪ :‬ﻛﺎﺭ ﺑﺎ ﻣﺘﻐﻴﺮﻫﺎ‪ ،‬ﻋﻤﻠﮕﺮﻫﺎ ﻭ ﺳﺎﺧﺘﺎﺭﻫﺎﻯ ﻛﻨﺘﺮﻟﻰ‬

‫ﻭﺍژﻩﻧﺎﻣﻪ‬

‫‪Association‬‬ ‫ﺍﻧﺠﻤﻦ‬
‫‪Break‬‬ ‫ﺷﻜﺴﺘﻦ‬
‫‪Continue‬‬ ‫ﺍﺩﺍﻣﻪ ﺩﺍﺩﻥ‬
‫‪Default‬‬ ‫ﭘﻴﺶﻓﺮﺽ‬
‫‪Define‬‬ ‫ﻣﻘﺪﺍﺭﺩﻫﻲ‬
‫‪HomePage‬‬ ‫ﺻﻔﺤﻪ ﺧﺎﻧﮕﻲ‬
‫‪Indexed‬‬ ‫ﺩﺍﺭﺍﻯ ﺯﻳﺮﻧﻮﻳﺲ‬

‫ﺧﻼﺻﻪ ﻣﻄﺎﻟﺐ‬

‫• ﺑﺮﺍﻱ ﺍﻳﺠﺎﺩ ﻓﺎﻳﻞﻫﺎﻱ ‪ PHP‬ﻧﻴﺎﺯﻱ ﺑﻪ ﻣﺤﻴﻂ ﻣﺨﺼﻮﺹ ﺍﻳﻦ ﺯﺑﺎﻥ ﻧﻴﺴﺖ‪ ،‬ﺣﺘﻲ ﺩﺭ ﻣﺤﻴﻂ‬
‫ﻣﺘﻨﻲ ﺳﺎﺩﻩ ‪ Notepad‬ﻧﻴﺰ ﻣﻲﺗﻮﺍﻥ ﻛﺪﻫﺎﻱ ﺁﻥ ﺭﺍ ﻧﻮﺷﺖ‪.‬‬
‫• ﻓﺎﻳﻞ‪ HomePage‬ﺻﻔﺤﺎﺕ ﺳﺎﻳﺖ ﺑﺎﻳﺪ ﺑﺎ ﻧﺎﻡ ‪ index‬ﻳﺎ ‪ default‬ﺫﺧﻴﺮﻩ ﺷﻮﺩ‪.‬‬
‫• ﻣﻬﻢﺗﺮﻳﻦ ﺍﺑﺰﺍﺭ ﺗﻮﻟﻴﺪ ﺻﻔﺤﺎﺕ ﭘﻮﻳﺎ ﺍﺳﺘﻔﺎﺩﻩ ﺍﺯ ﻣﺘﻐﻴﺮﻫﺎﺳﺖ‪.‬‬
‫• ﻣﺘﻐﻴﺮﻫﺎﻱ ‪ PHP‬ﻫﻤﻪ ﻣﻨﻈﻮﺭﻩ ﻫﺴــﺘﻨﺪ ﻭ ﻧﻴﺎﺯﻱ ﻧﻴﺴﺖ ﻫﻨﮕﺎﻡ ﻣﻌﺮﻓﻲ‪ ،‬ﻧﻮﻉ ﻣﺘﻐﻴﺮ ﺗﻌﻴﻴﻦ‬
‫ﺷﻮﺩ‪.‬‬
‫• ﻫﻨﮕﺎﻡ ﻣﻘﺪﺍﺭﺩﻫﻲ ﻣﺘﻐﻴﺮﻫﺎﻱ‪ ، PHP‬ﻧﻮﻉ ﺁﻥﻫﺎ ﺑﻪﻃﻮﺭ ﺿﻤﻨﻲ ﺗﻌﻴﻴﻦ ﻣﻲﺷﻮﺩ‪.‬‬
‫• ﺁﺭﺍﻳﻪﻫﺎﻱ ‪ PHP‬ﺩﺍﺭﺍﻱ ﺩﻭ ﻧﻮﻉ ﺍﺻﻠﻲ ﺍﻧﺪﻳﺲﺩﺍﺭ ﻭ ﺍﻧﺠﻤﻨﻲ ﻫﺴﺘﻨﺪ‪.‬‬
‫• ﺍﻧﺪﻳﺲ ﻣﻘﺎﺩﻳﺮ ﺁﺭﺍﻳﻪﻫﺎﻱ‪ PHP‬ﺍﺯ ﺻﻔﺮ ﺷﺮﻭﻉ ﻣﻲﺷﻮﺩ ﻳﻌﻨﻲ ﺍﻧﺪﻳﺲ ﺍﻭﻟﻴﻦ ﻋﻨﺼﺮ ﺁﺭﺍﻳﻪ‬
‫ﺻﻔﺮ ﻭ ﺍﻧﺪﻳﺲ ﺁﺧﺮﻳﻦ ﻋﻨﺼﺮ ﺁﺭﺍﻳﻪ ‪ n-1‬ﺍﺳﺖ‪.‬‬
‫• ﺩﺭ ‪ PHP‬ﻣﺘﻐﻴﺮﻫﺎﻳﻲ ﻭﺟﻮﺩ ﺩﺍﺭﻧﺪ ﻛﻪ ﺑﻪﻃﻮﺭ ﺍﺯ ﭘﻴﺶ ﺗﻌﺮﻳﻒ ﺷﺪﻩ ﺑﺮﺍﻱ ﺍﻫﺪﺍﻑ ﺧﺎﺻﻲ‬
‫ﺩﺭ ﻧﻈﺮ ﮔﺮﻓﺘﻪ ﺷــﺪﻩﺍﻧﺪ‪ ،‬ﺩﺭ ﻧﺎﻣﮕﺬﺍﺭﻱ ﻣﺘﻐﻴﺮﻫﺎ ﺑﺎﻳﺪ ﺩﻗﺖ ﻛﻨﻴﺪ‪ ،‬ﻣﺘﻐﻴﺮﻱ ﻛﻪ ﺗﻌﺮﻳﻒ ﻣﻲﻛﻨﻴﺪ‪،‬‬
‫ﻫﻢﻧﺎﻡ ﺑﺎ ﻣﺘﻐﻴﺮﻫﺎﻱ ﺍﺯ ﭘﻴﺶ ﺗﻌﺮﻳﻒ ﺷﺪﻩ ﻧﺒﺎﺷﺪ‪.‬‬
‫• ﻋﻤﻠﮕﺮﻫﺎﻱ ﺯﺑﺎﻥ ‪ PHP‬ﻧﻴﺰ ﻣﺎﻧﻨﺪ ﺳﺎﻳﺮ ﺯﺑﺎﻥﻫﺎ ﺑﺮﺍﻱ ﺍﻧﺠﺎﻡ ﻋﻤﻠﻴﺎﺕ ﻣﺤﺎﺳﺒﺎﺗﻲ‪ ،‬ﻣﻘﺎﻳﺴﻪﺍﻱ‪،‬‬
‫ﻣﻨﻄﻘﻲ ﻭ ‪ ...‬ﺑﻪﻛﺎﺭ ﻣﻲﺭﻭﻧﺪ‪.‬‬
‫• ﺩﺭ ﺗﻤﺎﻡ ﺯﺑﺎﻥﻫﺎﻱ ﺑﺮﻧﺎﻣﻪﻧﻮﻳﺴــﻲ ﺍﺯ ﺟﻤﻠﻪ‪ ، PHP‬ﺑــﺮﺍﻱ ﻛﻨﺘﺮﻝ ﺭﻭﻧﺪ ﺍﺟﺮﺍﻱ ﺑﺮﻧﺎﻣﻪ ﺍﺯ‬
‫ﻭﺯﺍﺭﺕ ﻓﺮﻫﻨﮓ ﻭ ﺍﺭﺷﺎﺩ ﺍﺳﻼﻣﻰ‬
‫ﻧﺎﻡ ﺍﺳﺘﺎﻧﺪﺍﺭﺩ‪ :‬ﻃﺮﺍﺡ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫ﺷﻤﺎﺭﻩ‪ -15 / 4/ 2 :‬ﻑ‪ ،‬ﻫـ‬ ‫‪142‬‬

‫ﻭ‬ ‫ﺩﺳــﺘﻮﺭﺍﺕ‪if … elseif ، if‬‬


‫ﺳــﺎﺧﺘﺎﺭﻫﺎﻱ ﻛﻨﺘﺮﻟﻲ ﺍﺳﺘﻔﺎﺩﻩ ﻣﻲﺷــﻮﺩ ﻛﻪ ﻣﻬﻢﺗﺮﻳﻦ ﺁﻥﻫﺎ‬
‫‪ Switch … Case‬ﻫﺴﺘﻨﺪ‪.‬‬
‫• ﺍﮔﺮ ﺩﺭ ﻧﻈﺮ ﺩﺍﺷــﺘﻪ ﺑﺎﺷﻴﻢ ﻳﻚ ﻳﺎ ﭼﻨﺪ ﺩﺳﺘﻮﺭﺍﻟﻌﻤﻞ ﺭﺍ ﭼﻨﺪ ﺑﺎﺭ ﺍﺟﺮﺍ ﻛﻨﻴﻢ ﺑﻬﺘﺮ ﺍﺳﺖ ﺍﺯ‬
‫ﺩﺳﺘﻮﺭﺍﺕ ﺣﻠﻘﻪ ﺍﺳﺘﻔﺎﺩﻩ ﻧﻤﺎﻳﻴﻢ‪.‬‬
‫• ﻣﻬﻢﺗﺮﻳﻦ ﺩﺳﺘﻮﺭﺍﺕ ﺣﻠﻘﻪ ﻋﺒﺎﺭﺗﻨﺪ ﺍﺯ‪ while ،for :‬ﻭ ‪do … while‬‬
‫ﭘﻴﻤﺎﻧﻪ ﻣﻬﺎﺭﺗﻰ‪ :‬ﻃﺮﺍﺣﻰ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫‪143‬‬ ‫ﻭﺍﺣﺪ ﻛﺎﺭﺷﺸﻢ‪ :‬ﻛﺎﺭ ﺑﺎ ﻣﺘﻐﻴﺮﻫﺎ‪ ،‬ﻋﻤﻠﮕﺮﻫﺎ ﻭ ﺳﺎﺧﺘﺎﺭﻫﺎﻯ ﻛﻨﺘﺮﻟﻰ‬

‫ﺁﺯﻣﻮﻥ ﻧﻈﺮﻱ‬

‫‪ - 1‬ﻛﺪﻫﺎﻱ ‪ PHP‬ﺭﺍ ﻗﺒﻞ ﺍﺯ ﺍﺟﺮﺍ ﺩﺭﻭﻥ ﻛﺪﺍﻡﻳﻚ ﺍﺯ ﻣﺤﻴﻂﻫﺎﻱ ﺯﻳﺮ ﻧﻤﻲﺗﻮﺍﻥ ﻧﻮﺷﺖ؟‬
‫ﺏ‪Notepad -‬‬ ‫ﺍﻟﻒ‪Word -‬‬

‫ﺩ‪Internet Explorer -‬‬ ‫ﺝ‪Dreamweaver -‬‬

‫‪ - 2‬ﺩﺳﺘﻮﺭﺍﺕ ‪ PHP‬ﺑﺎ ﻛﺪﺍﻡﻳﻚ ﺍﺯ ﻋﻼﺋﻢ ﺯﻳﺮ ﺷﺮﻭﻉ ﻣﻲﺷﻮﻧﺪ؟‬


‫ﺩ‪<php> -‬‬ ‫ﺝ‪<?php>-‬‬ ‫ﺏ‪<php-‬‬ ‫ﺍﻟﻒ‪<?php -‬‬

‫‪ - 3‬ﻛﺪﺍﻡﻳﻚ ﺍﺯ ﺍﺳﺎﻣﻲ ﺯﻳﺮ ﻧﻤﻲﺗﻮﺍﻧﺪ ﺑﻪﻃﻮﺭ ﺧﻮﺩﻛﺎﺭ ﺑﻪ ﻋﻨﻮﺍﻥ ﺻﻔﺤﻪ ﺧﺎﻧﮕﻲ ﺗﻨﻈﻴﻢ ﺷﻮﺩ؟‬
‫ﺩ‪index.txt-‬‬ ‫ﺝ‪default.php-‬‬ ‫ﺏ‪index.php -‬‬ ‫ﺍﻟﻒ‪index.htm -‬‬

‫‪ - 4‬ﺑﺮﺍﻱ ﺩﺭﺝ ﺗﻮﺿﻴﺤﺎﺕ ﺩﺭ ‪ PHP‬ﺍﺯ ﭼﻪ ﻋﻼﺋﻤﻲ ﺍﺳﺘﻔﺎﺩﻩ ﻣﻲﺷﻮﺩ؟‬


‫‪ */-4‬ﻭ *‪/‬‬ ‫‪$ -3‬‬ ‫‪# -2‬‬ ‫‪// -1‬‬
‫‪“ -5‬‬

‫ﺏ‪ 1 -‬ﻭ ‪ 2‬ﻭ ‪4‬‬ ‫ﺍﻟﻒ‪ 1 -‬ﻭ ‪ 2‬ﻭ ‪3‬‬


‫ﺩ‪ 1 -‬ﻭ ‪ 2‬ﻭ ‪4‬‬ ‫ﺝ‪ 1 -‬ﻭ ‪ 2‬ﻭ ‪ 3‬ﻭ ‪ 4‬ﻭ ‪5‬‬
‫‪ - 5‬ﺑﺮﺍﻱ ﺗﻌﻴﻴﻦ ﻳﻚ ﻣﺘﻐﻴﺮ ﺑﺎﻳﺪ ﺍﺯ ﭼﻪ ﻋﻼﻣﺘﻲ ﺍﺳﺘﻔﺎﺩﻩ ﺷﻮﺩ؟‬
‫ﺩ‪(Underline) -‬‬ ‫ﺝ‪@ -‬‬ ‫ﺏ‪$ -‬‬ ‫ﺍﻟﻒ‪# -‬‬

‫‪ - 6‬ﻛﺪﺍﻡﻳﻚ ﺍﺯ ﮔﺰﻳﻨﻪﻫﺎﻱ ﺯﻳﺮ ﻳﻚ ﻣﺘﻐﻴﺮ ﺍﻋﺸﺎﺭﻱ ﺭﺍ ﺑﻪﻃﻮﺭ ﺻﺤﻴﺢ ﻣﻌﺮﻓﻲ ﻣﻲﻛﻨﺪ؟‬


‫ﺩ‪float a = 1/5-‬‬ ‫ﺝ‪$a = 5.0-‬‬ ‫ﺏ‪$a = 5; -‬‬ ‫ﺍﻟﻒ‪float a; -‬‬

‫‪ - 7‬ﺍﮔﺮ ﻳﻚ ﻣﺘﻐﻴﺮ ﺑﻪﺻﻮﺭﺕ ;‪ $m = 1/4‬ﻣﻌﺮﻓﻲ ﺷﻮﺩ‪ ،‬ﺫﺧﻴﺮﻩ ﺁﻥ ﺑﻪ ﭼﻪ ﺷﻜﻞ ﺻﻮﺭﺕ ﻣﻲﮔﻴﺮﺩ؟‬


‫ﺩ‪1/4-‬‬ ‫ﺝ‪14-‬‬ ‫ﺏ‪1.4 -‬‬ ‫ﺍﻟﻒ‪0.25 -‬‬

‫‪ - 8‬ﺩﺭ ﺁﺭﺍﻳــﻪ ) "‪ $day = array ("first"، "Second"،"third‬ﻣﺘﻐﻴــﺮ]‪ day [1‬ﭼــﻪ ﻣﻘﺪﺍﺭﻱ ﺭﺍ‬
‫ﺑﺮﻣﻲﮔﺮﺩﺍﻧﺪ؟‬
‫ﺏ‪second -‬‬ ‫ﺍﻟﻒ‪first -‬‬

‫ﺩ‪ second -‬ﻭ ‪first‬‬ ‫ﺝ‪third -‬‬

‫‪ - 9‬ﻛﺪﺍﻡﻳﻚ ﺍﺯ ﻣﺘﻐﻴﺮﻫﺎﻱ ﺯﻳﺮ ﻧﺎﻡ ﺻﻔﺤﻪ ﺟﺎﺭﻱ ﺭﺍ ﺑﺮﻣﻲﮔﺮﺩﺍﻧﺪ؟‬


‫ﺏ‪PHP_OSTYPE-‬‬ ‫ﺍﻟﻒ‪PHP_CURRENTPAGE-‬‬

‫ﺩ‪PHP_NAME -‬‬ ‫ﺝ‪PHP_SELF -‬‬


‫ﻭﺯﺍﺭﺕ ﻓﺮﻫﻨﮓ ﻭ ﺍﺭﺷﺎﺩ ﺍﺳﻼﻣﻰ‬
‫ﻧﺎﻡ ﺍﺳﺘﺎﻧﺪﺍﺭﺩ‪ :‬ﻃﺮﺍﺡ ﺻﻔﺤﺎﺕ ﻭﺏ)ﭘﻴﺸﺮﻓﺘﻪ(‬
‫ﺷﻤﺎﺭﻩ‪ -15 / 4/ 2 :‬ﻑ‪ ،‬ﻫـ‬ ‫‪144‬‬

‫‪ - 10‬ﺑﺮﺍﻱ ﺗﻌﺮﻳﻒ ﺛﺎﺑﺖﻫﺎ ﺍﺯ ﭼﻪ ﺗﺎﺑﻌﻲ ﺍﺳﺘﻔﺎﺩﻩ ﻣﻲﺷﻮﺩ؟‬


‫ﺩ‪define-‬‬ ‫ﺝ‪array -‬‬ ‫ﺏ‪global -‬‬ ‫ﺍﻟﻒ‪const -‬‬

‫‪ - 11‬ﺍﮔﺮ ﺑﺨﻮﺍﻫﻴﻢ ﻳﻚ ﺑﺮﻧﺎﻣﻪ ﺭﺍ ﺑﺎ ﺳــﺎﺧﺘﺎﺭ ‪ if‬ﭘﻴﺎﺩﻩ ﻛﻨﻴﻢ ﺩﺭ ﺣﺎﻟﺘﻲﻛﻪ ‪ 5‬ﺷــﺮﻁ ﻣﺨﺘﻠﻒ‬


‫ﻭﺟﻮﺩ ﺩﺍﺷﺘﻪ ﺑﺎﺷﺪ‪ ،‬ﻣﺠﻤﻮﻉ ﺗﻌﺪﺍﺩ ‪ elseif ،if‬ﻭ ‪ else‬ﺩﺭ ﻛﺪ ﺑﺮﻧﺎﻣﻪ ﭼﻨﺪ ﻋﺪﺩ ﺧﻮﺍﻫﺪ ﺑﻮﺩ؟‬
‫ﺩ‪6 -‬‬ ‫ﺝ‪5 -‬‬ ‫ﺏ‪4 -‬‬ ‫ﺍﻟﻒ‪3 -‬‬
‫‪ - 12‬ﺩﺭ ﺩﺳﺘﻮﺭ ‪ for‬ﻧﻮﺷﺘﻦ ﻛﺪﺍﻡﻳﻚ ﺍﺯ ﺍﺟﺰﺍ ﺩﺭ ﻣﻘﺎﺑﻞ ‪ for‬ﺍﺟﺒﺎﺭﻱ ﺍﺳﺖ؟‬
‫ﺏ‪ -‬ﻋﺒﺎﺭﺕ ﺷﺮﻃﻲ‬ ‫ﺍﻟﻒ‪ -‬ﻣﻴﺰﺍﻥ ﺍﻓﺰﺍﻳﺶ ﻳﺎ ﻛﺎﻫﺶ ﻣﺘﻐﻴﺮ‬
‫ﺩ‪ -‬ﺍﻟﻒ ﻭ ﺝ‬ ‫ﺝ‪ -‬ﻣﻘﺪﺍﺭ ﺍﻭﻟﻴﻪ ﺣﻠﻘﻪ‬
‫‪ - 13‬ﺑﺮﺍﻱ ﺗﻌﺮﻳﻒ ﻳﻚ ﻣﺘﻐﻴﺮ ﺳﺮﺍﺳــﺮﻱ ﺑﺎﻳﺪ ﺍﺯ ﻛﺪﺍﻡ ﮔﺰﻳﻨﻪ ﻗﺒﻞ ﺍﺯ ﻧﺎﻡ ﻣﺘﻐﻴﺮ ﺍﺳــﺘﻔﺎﺩﻩ‬
‫ﺷﻮﺩ؟‬
‫ﺩ‪const-‬‬ ‫ﺝ‪define -‬‬ ‫ﺏ‪Local-‬‬ ‫ﺍﻟﻒ‪global -‬‬

‫ﺁﺯﻣﻮﻥ ﻋﻤﻠﻲ‬

‫‪ - 1‬ﻳﻚ ﺁﺭﺍﻳﻪ ﺑﺮﺍﻱ ﻣﻌﺮﻓﻲ ﺭﻭﺯﻫﺎﻱ ﻫﻔﺘﻪ ﺑﻨﻮﻳﺴﻴﺪ‪.‬‬


‫‪ - 2‬ﺩﺭ ﺳــﺆﺍﻝ ﺍﻭﻝ ﺑﺪﻭﻥ ﺍﺳــﺘﻔﺎﺩﻩ ﺍﺯ ﺣﻠﻘﻪﻫﺎ‪ ،‬ﻫﺮ ﻛﺪﺍﻡ ﺍﺯ ﻋﻨﺎﺻﺮ ﺁﺭﺍﻳﻪ ﺭﺍ ﺩﺭ ﻳﻚ ﺳﻄﺮ‬
‫ﺟﺪﺍ ﻧﻤﺎﻳﺶ ﺩﻫﻴﺪ‪.‬‬
‫‪ - 3‬ﺳﺆﺍﻝ ﺩﻭﻡ ﺭﺍ ﺑﺎ ﺍﺳﺘﻔﺎﺩﻩ ﺍﺯ ﺣﻠﻘﻪ ‪ for‬ﻭ‪ while‬ﭘﻴﺎﺩﻩﺳﺎﺯﻱ ﻛﻨﻴﺪ‪.‬‬
‫‪ - 4‬ﺍﻋﺪﺍﺩ ﻛﻮﭼﻚﺗﺮ ﺍﺯ ‪ 200‬ﺭﺍ ﻛﻪ ﻣﻀﺮﺏ ‪ 5‬ﻫﺴﺘﻨﺪ ﺩﺭ ﺧﺮﻭﺟﻲ ﻧﻤﺎﻳﺶ ﺩﻫﻴﺪ‪.‬‬
‫‪ - 5‬ﻣﺤﻴﻂ ﻭ ﻣﺴﺎﺣﺖ ﻳﻚ ﻣﺮﺑﻊ ﺑﺎ ﻗﻄﺮ ‪ 20‬ﺭﺍ ﻣﺤﺎﺳﺒﻪ ﻭ ﭼﺎپ ﻛﻨﻴﺪ‪.‬‬

You might also like