PHP uses binary to save user status instances, binary user status

Source: Internet
Author: User

PHP uses binary to save user status instances, binary user status

Preface

User State storage is a common requirement. Generally, you can create multiple fields in the database table to store the user State, for example, to save whether the user is bound with a mobile phone number and QQ, you need two fields (mobile and qq) to save the settings.

If you use binary to save the user status, you only need one field (status) to save multiple user statuses.

Of course, the use of binary to save also has some disadvantages, such as not storing detailed information, can only be used to determine the status, and the readability in the database is also relatively poor. The following is a simple Demo.

Code

<? Php/*** use binary to save the user status * // first define the four user statuses define ('mobile', 1); // bind the MOBILE phone number to define ('email ', 2); // bind define ('wechat', 4) to the mailbox; // bind define ('qq', 8 ); // bind QQ // simulate User class User {public $ user_name = ""; public $ status = 0; // used to save User status function _ construct ($ user_name, $ status) {$ this-> user_name = $ user_name; $ this-> status = $ status ;}// new test User $ test_user = new User ("test_user ", 0); // first determine whether the user is bound with the mobile phone number if ($ test_user-> statu S & MOBILE) = MOBILE) echo "first: this user has been bound to a MOBILE phone number. The user status is :". $ test_user-> status. "</br>"; else echo "first: This user is not bound with a mobile phone number. The user status is :". $ test_user-> status. "</br>"; // then the user binds the MOBILE phone number and QQ $ test_user-> status = MOBILE | WECHAT | QQ; // determine whether the user is bound with the MOBILE phone number if ($ test_user-> status & MOBILE) = MOBILE) echo "second: the user has been bound to the MOBILE phone number. The user status is: ". $ test_user-> status. "</br>"; else echo "second: This user is not bound with a mobile phone number. The user status is :". $ test_user-> status. "< /Br> "; // check whether the user has bound the EMAIL address if ($ test_user-> status & EMAIL) = EMAIL) echo" third: the user has already bound the EMAIL address, the user status is :". $ test_user-> status. "</br>"; else echo "third: the user is not bound to an email address. The user status is :". $ test_user-> status. "</br>"; // then the user unbinds the mobile phone number $ test_user-> status = ($ test_user-> status &(~ MOBILE); // determine whether the user is bound with a MOBILE phone number again. if ($ test_user-> status & MOBILE) = MOBILE) echo "fourth: the user is bound with a MOBILE phone number, the user status is :". $ test_user-> status. "</br>"; else echo "fourth: This user is not bound with a mobile phone number. The user status is :". $ test_user-> status. "</br> ";

The above example of saving the user status using binary in PHP is all the content shared by the editor. I hope to give you a reference and support for the help house.

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.