Home > Backend Development > PHP Tutorial > Details of php++ and logical operations php decryption band of brothers php details of democracy

Details of php++ and logical operations php decryption band of brothers php details of democracy

WBOY
Release: 2016-07-29 08:53:11
Original
1246 people have browsed it

Everyone knows the general technology, and then the details determine the height. Today, let’s talk about some details of php’s ++ and logical operators:

++ We all know that a variable is incremented by 1, but ++ points Before ++ and after ++;

$a=5;$b=0;

$a++ //a=6;This is for sure

$b=$a++;//What is this $b equal to? ? $b=5, because this is assigned first and then $a++;

$b=++$a;//What is $b equal to at this time? This is $b equal to 6, because ++ is executed first and then assigned;

Similarly, we can get some equations:

$a++*4=20;

++$a*4=24;

These details are Sometimes it will cause bugs in our program, and it is not easy to report errors.

Here comes a question about the combination of ++ and logical operators:

if($a>0||++$b>0){

echo $b; What is $b equal to? $b is equal to 0; because the execution rule of || logic is that as long as the first one is established, the second one will not be executed

}

if($a++>5||$b>0){

//Will you come in? The answer is no, because it is compared first, and then $a is +1

}

It is rare that I am more elegant today, so I will pay attention to the details of switch:

function test(variable){

switch(variable) {
case 1:echo 'The variable of the function is 1;break;

case 2:echo 'The variable of the function is 2;break;

case 3:echo 'The variable of the function is 3break;

default:echo 'Function variable is not in the value range';

}

}

test(1)==>There is no doubt that case 1 will be executed:

test('1')==>This will execute that ? The result is that case1 is executed in the same way; this shows that if the variable type behind the case is different from the parameter type passed in, it will automatically change the type to match the parameter passed in by switch as much as possible. Some people may have questions here. Why is it that the number type behind the case has changed, rather than the parameter type? How to prove it?

Don’t worry, let’s prove it now:

test(true);==>Which one will this execute? ===》The answer is that case 1 will be executed; because 1 will be directly converted to boll type, matching the parameters of switch as much as possible. The little friend who loves to use his brain said again, true will also get 1 after conversion, which cannot prove that it is case. The subsequent parameters are transferred. To prove this is very simple, just modify the test function.

function test(variable){

switch(variable){
case '12345':echo 'The variable of the function is 1;break;

case 2:echo 'The variable of the function is 2;break;

case 3:echo 'The variable of the function is 3break;

default:echo 'The function variable is not in the value range';

}

}

Look, this proves that it is impossible to match '12345' when converted to true , but the '12345' conversion can match true.

This is the end of the proof. I hope it can give some help to friends who don’t have a solid foundation. In fact, not only php, js, ruby, and asp.net are all the same, but the languages ​​have many things in common, and they all know everything.

The above introduces the details of php++ and logical operations, including php, details. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template