So far in this PHP Tutorial Series we have taken a good look at all of the basics. Some of the topics we looked at include what PHP is is, and what the history of PHP has been. Moving on from this we covered the beginnings of variables and strings, two of the most common concepts you’ll run across when programming in PHP. We then got funky with integers, floating point values, arrays, and more. Do you see a pattern here? That’s right, we’re hammering home the basic types so that when we get to more advanced topics, we won’t even have to think about these things. They will just be second nature. This leads us to today’s episode of working the Booleans and php Constants. They’re definitely not the most exciting thing in the world, but we’ll try to keep it fun, and they are a vital component of programming so let’s review them now!
Booleans are a really basic thing, they are a just like binary. This is to say you either have a 1 or a zero, a true or a false value. Booleans can either be true or false, that’s it. If it is true, this does not means the string true or the number 1, it is simply a truthy value. They are really useful in programming since we can make use of them to perform various tests. We’ll be making use of this technique all the time to decide which conditional branch to take. Booleans can be a little finicky so we should look at a few examples.
<?php $true = true; $false = false; echo $true.'<br>'; echo $false.'<br>'; echo is_bool($true).'<br>'; echo is_bool($false).'<br>'; var_dump($true).'<br>'; var_dump($false).'<br>'; ?>
1 1 1 boolean true boolean false
There are a few points to consider here. First up, notice that we assigned the boolean values using lowercase. Some will say you should always use uppercase, however for the
lazy efficient among us, why create the need to hold down the shift or caps lock key if the end result of
false is the same? 🙂 Now the next thing we do is to echo out these boolean values to the screen. Do not be confused! When using the echo command in PHP, the language will always try to convert whatever it is you are echoing into a
string. So when you echo out a
true boolean, PHP decides that this should be represented by 1. Ok, makes sense, right? Now, when PHP echoes out a
false value, the same type juggling takes place however instead of outputting a 0 like you think it might, it simply outputs nothing. Strange Behavior indeed.
To be sure of what you are dealing with, you can make use of the
is_bool() function to check if the variable contains a boolean. You may also want to make use of the
var_dump() function since it outputs both the type and the value for you. Types are a bit confusing, but the more you work with them, the easier they become to manage.
It’s nice to be able to make use of variables in programming to be able to do all kinds of interesting things. There are times when it is needed that we need to store a value in a variable and keep it that way. You can almost think of a constant that way. It is a technique to assign a value to a non changing variable, and use it anywhere in the program. Maybe variable isn’t the best name for this placeholder, and you know what, you’re right – that is why we call them constants! Constants need to use ALL UPPERCASE and they must be assigned a value using the define() function. You can not simply use the assignment = operator like you can with variables. Let’s test it out.
<?php define('TAX_RATE', 'Way too high.'); echo TAX_RATE; define('TAX_RATE', 7.8); echo TAX_RATE; ?>
Way too high. ( ! ) Notice: Constant TAX_RATE already defined in C:wampwwwphpconsoleindex.php(61) : eval()'d code on line 5 Way too high.
So some strange things happened. First off, we defined the constant tax rate as a string which is way too high. It echoed out to the browser just fine. Moving through the script however, we realized that the actual tax rate would be a number value so we try to redefine it. Guess what? You can’t do that! Once a constant is defined, it is set until the end of the script life cycle.
Now there is a bit more to constants so let’s examine. Check this out.
<?php echo __LINE__.'<br>'; echo __FILE__.'<br>'; echo __DIR__.'<br>'; ?>
1 C:wampwwwphpconsoleindex.php(61) : eval()'d code C:wampwwwphpconsole
Now wait just a second! We didn’t define anything to those constants, yet look at the data they output when we echo them to the browser! You are witnessing magic my friend, yes magic. Actually, PHP has a bunch of predefined constants that you can make use in your scripts right away. As we can see by the example here, __LINE__ provides the line number which is currently executing. __FILE__ provides the path and filename of the currently running script. __DIR__ gives us the directory where the currently executing file lives. These are all very handy, and the fun doesn’t end there. There are also some more advanced constants to make use of like __FUNCTCION__, __CLASS__, __TRAIT__, __METHOD__, and __NAMESPACE__ but we’ll get to those in due time as they are a bit more advanced. For now, as long as we understand the concept of what a constant is and how we can use it, we’ll be good to go.
The Action Packed Boolean and Constants Summary
Like we mentioned at the outset, booleans and constants might not set the world on fire with their operation, but they are key concepts to understand when programming. Booleans are especially key since pretty soon we’ll start looking at conditional statements. It is by looking at a boolean value that we determine which actions should take place in our code. Once we’re rocking and rolling with if statements, if else statements, if else if else statements, switch statements, and so on, booleans will be the very thing that determines how all of these things work. So while it is true that booleans seem boring with their on or off true or false nature, we need to have them down cold before moving onward!