Efficiency differences between switch and ifelse in php and analysis of its applicability _ PHP

Source: Internet
Author: User
Tags loop case
This article mainly introduces the differences in efficiency and applicability between switch and ifelse in php, and analyzes the differences in efficiency between switch and ifelse in the form of an instance in the case of variables and constants, it is very practical. if you need it, you can refer to the examples in this article to analyze the efficiency differences and applicability of the switch and ifelse in php. Share it with you for your reference. The specific analysis is as follows:

Both methods in PHP are used to determine whether the value meets the conditions. if the value Meets/does not meet the conditions, different actions are performed.

No matter what language programs are written, the code running efficiency will be taken into account. After reading some materials, switch and ifelse have different efficiency advantages in different 'environment.

1. when the determined value is a constant (fixed value), the switch operation efficiency is higher than the ifelse operation efficiency;

$ Changliang = 3; // The variable judgment value is constant switch ($ changliang) {case 1: echo 'constant value is 1'; break; // jump out of loop case 2: echo 'constant value: 2'; break; case 3: echo 'constant value: 3'; break ;}

2. when the determined value is a variable, ifelse runs more efficiently than switch. ifelse implements the final judgment policy and starts to judge from the first condition until the last else, therefore, it is advantageous to learn to use a switch;

$ A = $ _ GET ['A']; // get the value after passing the value through GET; if ($ a = 1) {echo 'variable a value is 1';} elseif ($ a = 2) {echo 'variable a value is 2';} elseif ($ a = 3) {echo 'the value of variable a is 3';} else {echo 'the value of variable a is unknown ';}

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.