Boolean Logic for PHP
Basics of PHP with Security Considerations - Part 5
Foreword: In this part of the series I teach you a way of reasoning called Boolean Logic.
By: Chrysanthus Date Published: 30 Aug 2018
Introduction
The Problem
Assuming that I am tall and you are tall. If someone ask you, if both of us are tall, you would say, Yes (true). If he asks if both of us are short, you would say, No (false). If you are short and I am tall, and he asks you if either you or me is tall, your answer would be Yes (true). If he asks if both you and me are tall, you would not have an answer. You might go on to say that the last question should not be asked or that the question does not have an answer. Well, I want you to know as from today that under certain circumstances the question should be asked.
In biology, a person is either tall or short. It is “environmental” conditions that make the person to have a medium height. One scientist, George Boole defined a set of answers or rules for this kind of questions. We learn this rules in this part of the series. These rules are used in computing, programming, electronics and telecommunications, today. In fact, without these rules, you will not have a computer; you will not also have programming.
True or False
The simple human language statement is either true or false in itself. If I say, “I am tall”, that is either true or false. If I say, “you are tall”, that is either true or false. If I am tall and you are short, and the question is asked, if both you and me are tall, in Boolean Logic, an answer of true or false must be given. Which of these two should be given? Boole did not really answer this question. He simply came up with a set of rules for us to follow. The good news is that, when you follow these rules in their right context, you do not have any ambiguity. Thanks to these rules, we have computers and programming, today. I will give you the rules. The rules cannot really be explained; you just accept them. The rules are under three headings: AND, OR and NOT.
AND
The question can be asked, if both you AND me are tall. My height and your height are combined by AND. These are the rules to follow:
false AND false = false
false AND true = false
true AND false = false
true AND true = true
Now, let tall be true and short be false. This means that if I am short AND you are short, you and me are short. If I am short AND you are tall, you and me are short; that is Boolean answer, which you have to accept. If I am tall AND you are short, both you and me are short. If I am tall AND you are tall, you and me are tall. All these are Boolean rules that you have to accept.
OR
The question can be asked, if you OR me is tall. My height and your height are combined by OR. These are the rules to follow:
false OR false = false
false OR true = true
true OR false = true
true OR true = true
Again, let tall be true and short be false. This means that if I am short OR you are short, you or me is short. If I am short OR you are tall, you or me is tall. If I am tall OR you are short, you OR me is tall. If I am tall OR you are tall, you or me is tall. All these are Boolean rules that you have to accept.
NOT
Now, in Boolean logic, only two states (possible answers) exist. That is, if you are NOT tall, you are short; if you are NOT short you are tall; nothing else. These are the rules to follow:
NOT false = true
NOT true = false
Assume that you have a string (spring), you can extend (pull). While the string is in its natural state, if I say, “NOT short” you would extend it; that is the interpretation. While the string is extended, if I say, “NOT long” you would allow it to contract; that is the interpretation.
You have to memorize all the above rules under their different categories.
More than two Variables
In PHP AND, OR and NOT are each called an operator. For the NOT operator, you need only one variable to have an answer. For AND or OR, you can have more than two variables. The above cases show two variables. You can have three variables for AND; something like:
false AND false AND false = false
false AND false AND true = false
These are two lines. There are actually nine lines when the variables are three. When the variables are four, there are 16 lines.
You should consult some other text to know the pattern when the number of variables is higher than 2. It will not be difficult to identify the patterns. The pattern for AND and the pattern for OR are different.
True or false is actually a value and not a variable. I have referred to them as variables in this article, for simplicity.
I hope you now know what Boolean values (true or false) mean. We carry on in the next part of the series.
Chrys
Related Links
Basics of PHP with Security ConsiderationsWhite Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links