1. 15
    Create a Boolean variable in PHP
    3m 28s

Create a Boolean variable in PHP

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

Booleans toggle variables to true or false, depending on the outcome of the expression. You can also negate values, which reverses the polarity of the PHP variable.

Instructor: [0:00] If you aren't yet familiar with Booleans, they're basically a way to toggle one or zero to determine if a variable is true or false. We won't be using our constants anymore. Let's go ahead and remove them. Let's create a new variable named hasPosts.

[0:18] You can name Booleans whatever you want, but it's a great convention to prefix them with either an is or a has. This solves documents as a variable as a Boolean, which can be helpful in this loosely typed language. Let's set this equal to true.

[0:35] Before we continue any further, let's learn about another very frequently used PHP function called var_dump. This function dumps the output of a variable to the screen and it differs from other commands because it dumps out more verbose information about a specific variable or expression.

[0:55] Let's open a PHP tag and run var_dump and we'll pass in hasPosts. Then we will close up this PHP tag.

[1:08] When we refresh the screen, we will see that not only the value of the variable is outputted, but also the data type. This can be extremely useful when debugging your code.

[1:19] Also, note how I didn't use a short echo tag here. This is because var_dump automatically echoes out whatever you pass to it.

[1:28] Let's change this true to false. Of course, we will see the update as expected.

[1:34] Now, let's do something a bit different and add an exclamation to the front. This reverses the polarity of this variable essentially making false true and true false.

[1:45] This can be useful when you want to return the opposite value of what is passed to it. You can also pass an expression to a variable.

[1:55] Let's update this from false to numPosts and title. Using a double ampersand is the equivalent of the AND operand, which means if both sides of the operand are true, it will return true.

[2:12] Remembering what we learned before, if these data types are different such as string or integer, they're automatically cast as Booleans when this expression is evaluated.

[2:23] When one of these values is false, then, of course, the entire statement is false since both expressions must be true to return a truthy result.

[2:34] Similarly, we can pass in a double pipe instead of the ampersand, which stands for OR. If either one or the other of these values is true, the entire expression is true.

[2:46] We can also negate the entire expression by wrapping it in parentheses and then negating the expression. This will first execute the numPost or title expression, which would be true. Then it negates it, which makes the entire expression false.

[3:04] For now, let's set hasPost equal to numPost is greater than zero. Note how I also left spaces between the comparison operator just to make things much easier to read. This means that whenever we have post greater than zero, the evaluation is true.

egghead
egghead
~ 4 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today