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.
- Object – NEXT LESSON
- Arrays – NEXT LESSON
- NULL – NEXT LESSON
- 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
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!
I hope you enjoyed the article. If I was able to help you please consider a tip for the content.
One Time Tip