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.