Hướng dẫn control structures in php

Tác giả: Dương Nguyễn Phú Cường

Ngày đăng: Hồi xưa đó

What is a control structure?

Code execution can be grouped into categories as shown below

  • Sequential – this one involves executing all the codes in the order in which they have been written.
  • Decision – this one involves making a choice given a number of options. The code executed depends on the value of the condition.

A control structure is a block of code that decides the execution path of a program depending on the value of the set condition. Let’s now look at some of the control structures that PHP supports.

If… then... else is the simplest control structure. It evaluates the conditions using Boolean logic When to use if… then… else

  • You have a block of code that should be executed only if a certain condition is true
  • You have two options, and you have to select one.
  • If… then… else if… is used when you have to select more than two options and you have to select one or more

Syntax The syntax for if… then… else is;

HERE,

  • if [condition is true]” is the control structure
  • block one” is the code to be executed if the condition is true
  • {…else…} is the fallback if the condition is false
  • block two” is the block of code executed if the condition is false

How it works The flow chart shown below illustrates how the if then… else control structure works

Let’s see this in action The code below uses “if… then… else” to determine the larger value between two numbers.

Output:

21 is greater than 7

PHP Switch Case

Switch… case is similar to the if then… else control structure. It only executes a single block of code depending on the value of the condition. If no condition has been met then the default block of code is executed. It has the following basic syntax.

HERE,

  • “switch[…]{…}” is the control structure block code
  • “case value: case…” are the blocks of code to be executed depending on the value of the condition
  • “default:” is the block of code to be executed when no value matches with the condition

How it works The flow chart shown below illustrates how the switch control structure works

Practical example The code below uses the switch control structure to display a message depending on the day of the week.

Output:

ladies night, take her out for dinner

Summary

  • Control structures are used to control the execution of the program
  • The if then... else is when you have more than route block of code to execute depending on the value of the condition
  • Switch… case is used to when you have a number of block codes, and you only have to execute one of them depending on the value of the set case.
  1. Home
  2. PHP Căn bản
  3. Bài 8: Cấu trúc điều kiện trong php

PHP Căn bản | Lập Trình PHP | 04/10/2015 | 2819 |

Các cấu trúc điều khiển [Control Structures] căn bản mà hầu hết các ngôn ngữ lập trình đều có. Trong lập trình cấu trúc điều khiển được dùng để kiếm soát luồng chạy của chương trình. Phần này chúng ta sẽ tìm hiểu các cấu trúc điều khiển if, if...else, if..elseif và switch.

I. Cấu trúc rẽ nhánh if

Cấu trúc if là một trong những cấu trúc quan trọng trong nhiều ngôn ngữ lập trình, trong đó có ngôn ngữ PHP. Nó cho phép người lập trình điều khiển các đoạn mã.

Cú pháp 1:

if [biểu thức điều kiện] {
    Lệnh khi đúng;
}

Ý nghĩa:

Nếu biểu thức điều kiện là đúng [true] thì sẽ thực hiện lệnh trong dấu { } trái lại thì không thực hiện.

Ví dụ:

$a = 9;
if [$a > 8] {
    echo $a,' lớn hơn 8';
}

Cú pháp 2:

if [biểu thức điều khiện]{
     Lệnh khi đúng;
}else{
     Lệnh khi sai;
}

Ý nghĩa:

Nếu biểu thức điều kiện là đúng [true] thì sẽ thực hiện "Lệnh khi đúng" trái lại thì thực hiện "Lệnh khi sai".

Ví dụ:

$a = 9;
if [$a > 8] {
    echo $a,' lớn hơn 8';
}else{
    echo $a,' nhỏ hơn hoặc bằng 8';
}

II. Toán tử ?:

III. Cấu trúc chọn lựa switch

    Các Tin Khác:

  • Một số hàm sắp xếp dành cho mảng trong php - 10/03/2016
  • Bài 3: Xây dựng trang với web HTML 5 - 28/01/2016
  • Bài 9: Cấu trúc lặp - 28/01/2016
  • Bài 6: Tổng quan lập trình PHP - 28/01/2016
  • Bài 1 - Tổng quan về lập trình Web - 19/01/2016
  • Bài 2: Xây dựng trang web với HTML - 07/01/2016
  • Bài 7: Hướng dẫn cài đặt wampserver trên localhost - 05/01/2016
  • Bài 4: Sử dụng CSS trong trang web - 04/10/2015

Mới Đăng

Tags

Lên đầu trang, back to top, scroll to top, jquery, css, bootstrap, thiết kế giao diện, theme, thiết kế web, framework, wampserver, máy chủ web, localhost, php, mysql, apache, định dạng tiền, phân cách phần ngàn, javascript định dạng tiền, format currency

Chủ Đề