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

--><!-- Nothing works without HTML -->


<div>
<h1>Data-types in PHP - integer, string, double and bool</h1>

<img src="../images_July_2015/code.jpg" width="799" height="519" alt="data-types in php" />


<h6>There's a button below (sneak peek) to peek at the code.</h6>




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


</p>


<ol style="margin-left:40px">
<li>string</li>
<li>integer</li>
<li>double</li>
<li>boolean</li>
</ol>


<p>
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.
</p>



<?php
$variable1 = "10sugar";
$variable2 = 2;
?>

<p>
If we echo with "+" we will get the sum: <?php echo($variable1+$variable2);?>
</br>
If we echo with "." we will get the concatenated string: <?php echo($variable1.$variable2);?>
</p>

<p>Look at this:
</br>
</br>
Sometimes I am an <?php echo(gettype($variable1 + $variable2))?>.
</br>
and sometimes I am a <?php echo(gettype($variable1 . $variable2))?>.
</br>
</br>
(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:
&lt;?php echo(gettype($variable1 . $variable2))?&gt;
</br>
Now we will try something else and change the values of the variables.
</p>



<?php
$variable1 = 1;
$variable2 = 2.5;
?>


<p>
The sum is <?php echo($variable1+$variable2); ?>.
</br>
I can be an <?php echo(gettype($variable1)); ?>.
</br>
I can be a <?php echo(gettype($variable2)); ?>.
</p>



<p>
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 <a href="https://en.wikipedia.org/wiki/Double-precision_floating-point_format">wikipedia</a>).
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".
</p>




<?php
$variable1 = 1;
$variable2 = 2.5;
?>

<p>True is represented (echoed to this document) as <?php echo($variable1+$variable2 == 3.5); ?>.
</br>
False is represented as "<?php echo($variable1+$variable2 == 9); ?>" (meaning nothing). The operator "==" checks if
one value equals another. This can get quite philosophical.
</br>
So gettype(($variable1+$variable2 == 9)) is <?php echo (gettype(($variable1+$variable2 == 9))); ?>.
</p>


<?php
$variable1 = 1;
$variable2 = 2.5;

if ($variable1+$variable2 == 9)

{
echo ('1 + 2.5 = 9 is true.');
}
else echo ('1 + 2.5 = 9 is false.');
?>

<p>
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.
</p>

<p>Do you see what PHP is doing here?</p>


<?php echo(($variable1+$variable2 = 9));?>

<p>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:</p>

<p>See, variable2 is really <?php echo($variable2);?>.</p>




<p>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:</p>


<?php
$variable1 = 1;
$variable2 = 2.5;

if ($variable1+$variable2 = 9)

{
echo ('1 + 2.5 = 9 is true.');
}
else echo ('1 + 2.5 = 9 is false.');






?>









</br>
</br>
</br>





<a href="http://www.primitivecode.com/index.php?topic=-PHP-function-in+php-">
&gt;&gt; continue</a>

<?php $Weblog->share ();?>


<br/>
<br/>
<br/>

</div>

Sneak peek of the server side code