Data-types in PHP - integer, string, double and bool
Topics

Hi!
I'm Karsten and I want to help people learn German.
Buy me a coffee if you feel I did well.

Data-types in PHP - integer, string, double and bool

data-types in php
There's a button below (sneak peek) to peek at the code.

There are several data-types in programming languages. The most important are:

  1. string
  2. integer
  3. double
  4. boolean

There are many more, but let's look at these first. PHP is very simple in this respect. Some think it is a curse, and some think it's a blessing: When you declare a variable, its type is determined by the context. Explicit type definition is not required.

If we echo with "+" we will get the sum: 12
If we echo with "." we will get the concatenated string: 10sugar2

Look at this:

Sometimes I am an integer.
and sometimes I am a string.

(Again, you will need the sneak-peak button at the bottom of the page.) What are we doing here? The expression ($variable1 . $variable2) is being sent to the function called gettype: gettype($variable1 . $variable2). Then we echo it to the html document by echo(gettype($variable1 . $variable2)). By putting it in-between php-tags we are telling the interpreter to evaluate the expression and it then echoes the type: <?php echo(gettype($variable1 . $variable2))?>
Now we will try something else and change the values of the variables.

The sum is 3.5.
I can be an integer.
I can be a double.

An integer is a whole number. It can be either positive or negative, not to forget the "0". The double-precision floating-point format is a computer number format which occupies 8 bytes (64 bits) in computer memory and represents a wide, dynamic range of values by using a floating point (a very long explanation can be found on wikipedia). The boolean type can take two values, either true of false, but it is represented not only by "true" and "false", but also by the number 1, the string "1", the number 0 or "0" and an empty string (""), true, false and also the strings "true" and "false". You need a boolean to tell if an expression is true or not, like "2.5 + 1 = 3.5".

True is represented (echoed to this document) as 1.
False is represented as "" (meaning nothing). The operator "==" checks if one value equals another. This can get quite philosophical.
So gettype(($variable1+$variable2 == 9)) is boolean.

1 + 2.5 = 9 is false.

We are using an if-construct. If the expression in-between the round brackets is true the statement in-between the next curly brackets is executed, if not (if the expression is evaluated to false), the code in the curly brackets following the word "else" is executed. You can also omit the "else", then if the expression is false, nothing is executed.

Do you see what PHP is doing here?

10

The interpreter is moving from left to right, it "sees" that variable1 is to be added to the following and the following variable2 is then set to be 9 and then the two numbers are added:

See, variable2 is really 9.

This is the most common mistake that programmers make. One wants to check the truth of something, and uses the operator "=". This doesn't check but sets a value:

1 + 2.5 = 9 is true.


>> continue
submit to reddit





Sneak peek of the server side code