Operators and Data-types in PHP
Topics

Operators and Data-types in PHP

function php
There's a button below (sneak peek) to peek at the code.

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.

value == ===
$a = "" false false
$b = "" false false
$c = "" false false
$d = "1" true true
$e = "1" true false
$f = "42" true false
$g = "0" false false
$h = "-1" true false
$i = "1" true false
$j = "0" false false
$k = "-1" true false
$l = "php" true false
$m = "true" true false
$n = "false" true false
$o = "" false false
Are you meditating on this table right now? This will cross your path more than once, believe me.

>> continue
submit to reddit





Sneak peek of the server side code