Operators and Data-types in PHP
Topics

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


<div>
<h1>Operators and Data-types in PHP</h1>
<img src="../images_July_2015/hello.png" width="600" height="287" alt="function php"/>
<h6>There's a button below (sneak peek) to peek at the code.</h6>

<?php
$a = "";
$b = null;
$c = false;
$d = true;
$e = 1;
$f = 42;
$g = 0;
$h = -1;
$i = "1";
$j = "0";
$k = "-1";
$l = "php";
$m = "true";
$n = "false";



function the_truth ($variable)
{
$truth="true";

if ($variable == true)
{
return $truth;
}
else
{
$truth = "false";
return $truth;
}
}

function the_real_truth ($variable)
{
$truth="true";

if ($variable === true)
{
return $truth;
}
else
{
$truth = "false";
return $truth;
}
}


?>




<p>
What are we doing here? We have written two functions which check if
the expression is true. The function "the_truth" only checks the values.
Checking with "the_truth" only, the function identifies "1" and 1 and true
and "true" as true. The function "the_real_truth" also checks if the data
types are boolean. If the types are not boolean and true ($d) they will
in the case of "the_real_truth"
always return false. As you can see, you can not only pass a value to a
function, but you can also receive a value. What does this function do?
The function the_truth checks only superficially if the value that has been
passed to it is true by using this construction which executes code
only when the condition is met, i.e. when $variable is true:
if ($variable == true). This is the
if-construction we talked about. The operator used is "==". It doesn't
check the type of the variable, it only checks the content.
In this fashion you can "echo a function" if a function returns a value
like this one. We have a variable in the function called $truth set per
default to the string "true" by stating '$truth="true";'. If the passed variable
isn't true then $truth will be set to the string "false" ($truth = "false";).
This string is returned to where the function was called:
by "return $truth;". Once the interpreter enters the return command it leaves
the function. So if the condition is not true the second return command of the
function is executed after $truth has been set to "false". If the condition
is true the function executes the first return command and leaves everything
else behind, leaving the default value in the variable ("true"). Depending
then on the path the code takes (either true of false) the return value of $truth
will be either "true" or "false". ($truth is a string data-type. You need to keep string and boolean
apart.) The function the_real_truth looks as if it does the same as the
function the_truth but its operator is not "==", but "===" which
includes data-types in the check. This is one very important thing
to remember in order to avoid programming frustration and a desperate
search for mistakes in the code.
</p>


<table>
<tr>
<th>value</th>
<th>==</th>
<th>===</th>
</tr>
<tr>
<td><?php echo ('$a = "' .$a. '"') ?></td>
<td><?php echo(the_truth ($a)) ?></td>
<td><?php echo(the_real_truth ($a)) ?></td>
</tr>
<tr>
<td><?php echo ('$b = "' .$b. '"') ?></td>
<td><?php echo(the_truth ($b)) ?></td>
<td><?php echo(the_real_truth ($b)) ?></td>
</tr>
<tr>
<td><?php echo ('$c = "' .$c. '"') ?></td>
<td><?php echo(the_truth ($c)) ?></td>
<td><?php echo(the_real_truth ($c)) ?></td>
</tr>
<tr>
<td><?php echo ('$d = "' .$d. '"') ?></td>
<td><?php echo(the_truth ($d)) ?></td>
<td><?php echo(the_real_truth ($d)) ?></td>
</tr>
<tr>
<td><?php echo ('$e = "' .$e. '"') ?></td>
<td><?php echo(the_truth ($e)) ?></td>
<td><?php echo(the_real_truth ($e)) ?></td>
</tr>
<tr>
<td><?php echo ('$f = "' .$f. '"') ?></td>
<td><?php echo(the_truth ($f)) ?></td>
<td><?php echo(the_real_truth ($f)) ?></td>
</tr>
<tr>
<td><?php echo ('$g = "' .$g. '"') ?></td>
<td><?php echo(the_truth ($g)) ?></td>
<td><?php echo(the_real_truth ($g)) ?></td>
</tr>
<tr>
<td><?php echo ('$h = "' .$h. '"') ?></td>
<td><?php echo(the_truth ($h)) ?></td>
<td><?php echo(the_real_truth ($h)) ?></td>
</tr>
<tr>
<td><?php echo ('$i = "' .$i. '"') ?></td>
<td><?php echo(the_truth ($i)) ?></td>
<td><?php echo(the_real_truth ($i)) ?></td>
</tr>
<tr>
<td><?php echo ('$j = "' .$j. '"') ?></td>
<td><?php echo(the_truth ($j)) ?></td>
<td><?php echo(the_real_truth ($j)) ?></td>
</tr>
<tr>
<td><?php echo ('$k = "' .$k. '"') ?></td>
<td><?php echo(the_truth ($k)) ?></td>
<td><?php echo(the_real_truth ($k)) ?></td>
</tr>
<tr>
<td><?php echo ('$l = "' .$l. '"') ?></td>
<td><?php echo(the_truth ($l)) ?></td>
<td><?php echo(the_real_truth ($l)) ?></td>
</tr>
<tr>
<td><?php echo ('$m = "' .$m. '"') ?></td>
<td><?php echo(the_truth ($m)) ?></td>
<td><?php echo(the_real_truth ($m)) ?></td>
</tr>
<tr>
<td><?php echo ('$n = "' .$n. '"') ?></td>
<td><?php echo(the_truth ($n)) ?></td>
<td><?php echo(the_real_truth ($n)) ?></td>
</tr>
<tr>
<td><?php echo ('$o = "' .$o. '"')//not defined ?></td>
<td><?php echo(the_truth ($o)) ?></td>
<td><?php echo(the_real_truth ($o)) ?></td>
</tr>
</table>


<div>
Are you meditating on this table right now? This
will cross your path more than once, believe me.
</div>





</br>

<a href="#">
&gt;&gt; continue</a>

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


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

</div>

Sneak peek of the server side code