Hey everybody I want to let you know that I have undertaken the grueling task of getting the heck away from WordPress. I was so sick of the problems and updates I had to do all the time. I am now using my ezbloo system and I am integrating all my old posts into the new system. It sucks, but in the end, I will save bundles of time. I needed to keep things simple and that is why I created ezbloo. I'll have more on this later for you guys after I am done with the total integration of my old posts here. So if you are looking for a post and need it faster, shoot me an email and I will make it a priority. [email protected]

Understanding PHP7 data types will help you put things together. There are 8 different data types in PHP. They are all used differently.  We will cover the first 4 types in this lesson and the second half in the next lesson. Here are files from the last lesson. Here are some quick examples of PHP7 data types and how they are used.

PHP7 Data Types

These are the different data types in PHP7.
  1. Integers
  2. Floats
  3. Strings
  4. Boolean
  5. Object - NEXT LESSON
  6. Arrays - NEXT LESSON
  7. NULL - NEXT LESSON
  8. Resource - NEXT LESSON

Integers In PHP7

Integers are whole numbers. They can also include negative numbers. Integers cannot include decimal points. You do not have to include quotes when setting a variable to an integer. Here is an example. <?php $var=2; echo $var; ?> If you tried to do that with a word you would get an error. Here is an example of the wrong thing to do.
<?php $var=two; echo $var; ?>
Here is an example of the warning you will get. Warning: Use of undefined constant two - assumed 'two' (this will throw an Error in a future version of PHP) in /opt/lampp/htdocs/mywebsite/datatypes.php on line 2 two

Using var_dump To Define A Variable

In PHP7 you can use var_dump($var); to get the type and value of a variable.  So from our example above we can use this.
<?php $var=2; var_dump($var); ?>
This will return "int(2)".

How Would You Use Integers in PHP7?

You would use "PHP Integers" when you are doing some kind of math in PHP7. If you are looking for greater than, less than, adding, multiplying or dividing you would want to use integers. Remember that integers are whole numbers not decimals! use-php-integers-when-doing-math

Floats In PHP7

Floats are numbers too but they contain decimal points. They like integers do not get quotes like strings. Here is an example of a float.
<?php $var=2.5; var_dump($var); ?>
This is the result. float(2.5)

When Would I Use A Float In PHP7?

You would use a float in PHP7 when you are doing math as well. You may have a float returning from a database query and need to do something with it. This will come in handy when you are making calculations for checkout processes. float-is-a-number with a decimal in it in PHP7

Strings in PHP7

If you have been following along in our lessons we have used several strings. A string in PHP can be encapsulated with single or double quotes. A PHP string is made up of several letters, numbers and even code like HTML, javaScript and CSS. Here is an example of a string.
<?php $var="I Love PHP7" var_dump($var);?>
This is the result. When you use var_dump it returns the value and also the number of characters that are in a string. string(11) "I love PHP7"

Using Strings With Code in PHP7

You can use strings to hold code in PHP. We have done this a few times already in our lessons. Here is an example.
<?php $string='
<h3>PHP7 Is Awesome</h3>';
echo $string; ?>

When Would I Use String in PHP7?

You will use strings all the time in PHP7. They are used everywhere and practically on every page. Creating a string is simple and easy to use, that may be why we use them so often. string in php7 are used all the time

Booleans in PHP7

A Boolean value will return TRUE or FALSE. If an integer is zero 0 it is considered FALSE in PHP7. If a string has nothing in it then it is considered FALSE in PHP7. Negative numbers are considered TRUE to Boolean. Here are some examples.

Integer Boolean False

<?php $var=0; var_dump((bool) $var); ?> Result would be: bool(false).

Integer Boolean True

Notice we are using a negative 2 "-2". Even though it is negative number it is still TRUE to Boolean.
<?php $var=-2; var_dump((bool) $var); ?>
Result would be: bool(true)

String Boolean False

<?php $var=""; var_dump((bool) $var); ?>
Result would be: bool(false).

How We Would Use Booleans

We use booleans in PHP to see if something is true or false. So if we need to see if a variable has a value or not we can use the var_dump((bool) $var); to see if there is anything in it or not.

booleans-in-php7
Here are the complete files for this lesson.