In the previous sections, we worked with strings that were "in quotes" and numbers that were not. Now you get to find out why.
Different kinds of data are stored in different kinds of variables. A string like "
Hello world" is very different from the number 32, which is very different from a binary True/False, which is very different from an array containing multiple variables. These types of variables are called "datatypes." In PHP, the left side of a variable assignment always looks the same:
It's what comes on the right side of the "=" sign that distinguishes the datatype. While PHP doesn't do nearly as much as other programming languages to distinguish between datatypes, you will eventually run into logic problems if you don't understand their differences. Here are the most common datatypes. Pay attention to the right side of the equations.
Strange things can happen if you don't pay attention to datatypes, especially when you mix them carelessly. For example, what might happen when you try to perform calculations or comparisons on strings and numbers at the same time? Study the code below and try to imagine what will happen if you run it, then paste it into your sample page and study the results. Are they what you expected?
Notice that some of the combinations make sense and others don't, all depending on whether we used quotation marks when declaring the variable (i.e. depending on the datatype we used). PHP can be very forgiving ... but the fact that it's forgiving can lead to mistakes. This is one reason why more experienced developers often prefer a stricter language like Python that simply throws errors when you try to do something nonsensical like adding a string to an integer.