SlideShare ist ein Scribd-Unternehmen logo
1 von 9
CHUYÊN ĐỀ 6
CẤU TRÚC LỆNH VÀ HÀM
THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 1
NỘI DUNG
Các cấu trúc lệnh rẽ nhánh
Cấu trúc lặp
THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 2
6.2. CÚ PHÁP CƠ BẢN
6.2.3. CÁC CẤU TRÚC LỆNH TRONG PHP
 Điều kiện if
 Điều khiển switch
 Vòng lặp for
 Vòng lặp while
 Vòng lặp do.. While
 Vòng lặp foreach
 Từ khóa break, continue
THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 3
CẤU TRÚC LỆNH IF
if (condition)
{
statement[s] if true
}
else (condition)
{
statement[s] if false
}
THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 4
Ví dụ:
$x = 5;
if ($x < 4)
echo “$x is less than 4”;
else
print ‘$x isn’t less than 4’;
$x isn’t less than 4
CẤU TRÚC LỆNH SWITCH
switch (expression) {
case label :
statementlist
break;
case label :
statementlist
break;
...
default :
statementlist
}
THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 5
$menu = 3;
switch ($menu){
case 1:
echo "You picked one";
break;
case 2:
echo "You picked two";
break;
case 3:
echo "You picked three";
case 4:
echo "You picked four";
break;
default:
echo "You picked another option";
}
CẤU TRÚC LẶP FOR
for ([initial expression]; [condition]; [update expression])
{
statement[s] inside loop
}
THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 6
 Ví dụ:
print “<select>”;
for ($i = 1; $i <= 12; $i++) {
print “<option>$i</option>”;
}
print “</select>”;
CẤU TRÚC LẶP WHILE, DO…WHILE
while (expression)
{
statements
}
THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 7
do
{
statements
}while (expression);
Ví dụ:
$i = 1; $j = 9;
while ($i <= 10) {
$temp = $i * $j;
print “$j * $i = $temp<br>";
$i++;
}
CẤU TRÚC LẶP FOREACH
foreach (array as variable)
{
statements
}
THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 8
Ví dụ:
$meal = array('breakfast' => 'Walnut Bun',
'lunch' => 'Cashew Nuts and White Mushrooms',
'dinner' => 'Eggplant with Chili Sauce');
print "<table border=‘1’>n";
foreach ($meal as $key => $value) {
print "<tr><td>$key</td><td>$value</td></tr>n";
}
print '</table>';
BÀI TẬP THỰC HÀNH
THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 9

Weitere ähnliche Inhalte

Empfohlen

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 

Empfohlen (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

Chuyên đề 6. Cấu trúc lệnh.pptx

  • 1. CHUYÊN ĐỀ 6 CẤU TRÚC LỆNH VÀ HÀM THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 1
  • 2. NỘI DUNG Các cấu trúc lệnh rẽ nhánh Cấu trúc lặp THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 2
  • 3. 6.2. CÚ PHÁP CƠ BẢN 6.2.3. CÁC CẤU TRÚC LỆNH TRONG PHP  Điều kiện if  Điều khiển switch  Vòng lặp for  Vòng lặp while  Vòng lặp do.. While  Vòng lặp foreach  Từ khóa break, continue THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 3
  • 4. CẤU TRÚC LỆNH IF if (condition) { statement[s] if true } else (condition) { statement[s] if false } THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 4 Ví dụ: $x = 5; if ($x < 4) echo “$x is less than 4”; else print ‘$x isn’t less than 4’; $x isn’t less than 4
  • 5. CẤU TRÚC LỆNH SWITCH switch (expression) { case label : statementlist break; case label : statementlist break; ... default : statementlist } THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 5 $menu = 3; switch ($menu){ case 1: echo "You picked one"; break; case 2: echo "You picked two"; break; case 3: echo "You picked three"; case 4: echo "You picked four"; break; default: echo "You picked another option"; }
  • 6. CẤU TRÚC LẶP FOR for ([initial expression]; [condition]; [update expression]) { statement[s] inside loop } THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 6  Ví dụ: print “<select>”; for ($i = 1; $i <= 12; $i++) { print “<option>$i</option>”; } print “</select>”;
  • 7. CẤU TRÚC LẶP WHILE, DO…WHILE while (expression) { statements } THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 7 do { statements }while (expression); Ví dụ: $i = 1; $j = 9; while ($i <= 10) { $temp = $i * $j; print “$j * $i = $temp<br>"; $i++; }
  • 8. CẤU TRÚC LẶP FOREACH foreach (array as variable) { statements } THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 8 Ví dụ: $meal = array('breakfast' => 'Walnut Bun', 'lunch' => 'Cashew Nuts and White Mushrooms', 'dinner' => 'Eggplant with Chili Sauce'); print "<table border=‘1’>n"; foreach ($meal as $key => $value) { print "<tr><td>$key</td><td>$value</td></tr>n"; } print '</table>';
  • 9. BÀI TẬP THỰC HÀNH THS. PHAN THỊ GẤM - KHOA KỸ THUẬT CÔNG NGHỆ, TRƯỜNG ĐH HÀ TĨNH 9