How is the difference between false and 0 in php?

Source: Internet
Author: User
In PHP, when the variable is 0, the variable is "equal to" false "at the same time. how can we distinguish between 0 and false? This is useful in some condition statements. This article will give an example. First, check the code. the function of this code is to find whether a character string is opened with a certain word.

In PHP, when the variable is 0, the variable is "equal to" false "at the same time. how can we distinguish between 0 and false? This is useful in some condition statements. This article will give an example.

First, check the code. the function of this code is to find whether a string starts with a word.

The instance code is as follows:

  1. $ Title = "Hello world .";
  2. $ Keyword = "you ";
  3. If (strpos ($ title, $ keyword) = 0 ){
  4. Echo "correct ";
  5. } Else {
  6. Echo "error ";
  7. }

Output: correct

It seems that the code is correct? Why is the result incorrect? View The Help Manual. when the strpos () function finds a word in a string, if the word exists, the index position of the word is returned. otherwise, false is returned. then, the code is modified as follows.

The instance code is as follows:

  1. If (strpos ($ title, $ keyword) = 0 ){
  2. Echo "correct ";
  3. } Else if (strpos ($ title, $ keyword) = false ){
  4. Echo "error ";
  5. }

Output: correct

Why is it wrong? In PHP, when the variable is 0, the variable is "equal to" false "at the same time. how can we distinguish between 0 and false? In fact, it is very easy to modify the code:

The instance code is as follows:

  1. If (strpos ($ title, $ keyword) === 0 ){
  2. Echo "correct ";
  3. } Else if (strpos ($ title, $ keyword) === false ){
  4. Echo "error ";
  5. }

Output: Error

The instance code is as follows:

  1. /*
  2. * Test boolean
  3. * 0 false
  4. */
  5. $ Num = 0;
  6. $ BTest1 = false;
  7. $ BTest2 = true;
  8. $ StrTest2 = 'false ';
  9. If ($ num = $ bTest1)
  10. {
  11. Echo ('digits 0 and false are equal '); // Display
  12. Echo ("
    ");
  13. }
  14. If ($ bTest1)
  15. {
  16. Echo ('never executed
    '); // Not displayed
  17. }
  18. If (1)
  19. {
  20. Echo ('will it be executed,
    '); // Execute
  21. }
  22. If ($ bTest2)
  23. {
  24. Echo ('I am the boss, I want to execute
    '); // Execute
  25. }
  26. Else {
  27. Echo ('none of them belong to me.
    ');
  28. }
  29. Echo (false = 0); // 1 indicates equal
  30. Echo (true = 1); // Display 1 to indicate equal
  31. Function testReturn ()
  32. {
  33. Echo ('aaaaa ');
  34. Return;
  35. Return 'bbbbbb ';
  36. Echo ('cccccc ');
  37. }
  38. // Return indicates that the return of the function is not executed until it is executed, and exit is the launch program.
  39. Echo testReturn (); // call this function to output 'AAA' 'bbbbbbb'
  40. ?>

In many cases, false is equal to 0. when we want to return a value containing 0, for example, we need to pay attention to the number query. we can use ===to determine whether it is completely equal,

PHP has a gettype () function to obtain the variable type. you can use the ===operator (after reading it, there are three equal signs ). the difference between the = operator and the = operator is that the operator compares the values and types of data at the same time.

When different variable types are involved in the termination condition, use = and! = It is very important to check the strong type of operators.

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.