Sorry for bad english , used Google.translate
There is a code that returns a value to a int, if set . Otherwise it returns false
if (isset($this->variable))
return intval ($this->variable);
else
return false;
On the receiving side condition
if ($return_value) {
// Here code
}
The problem is that if the returned value is 0, this is false, and the code is executed . But as the value of 0 is also important to me . If returned as a string , it is still treated as false.
define ('false', 'value') does not work.
Introduced his constant for this , but you have to rewrite a bunch of code for additional testing
(if($return_value! == my_false_constant)
That is not quite satisfied.
What options are there to solve this problem ?
if ($return_value !== false) {
}
Using !== (or ===) instead of just != or == also tests the type of the value.
Use strict comparison with ===.
See: http://us3.php.net/manual/en/types.comparisons.php
if(1 === true) //returns FALSE
This will work:
(if($return_value !== false){
// do work
}
Comparisons:
== means same value
=== means same value AND same type
! == means not (same value)
!== means not (same value and same type)
SO:
0 == false //is true
0 === false //is false
Simply ! == does not equal !==, as is not valid PHP code
Related
My code looks like this:
$muted = 'true'; //Note this is only for testing
if ($muted == 'false' || $muted == '' || $do_not_text == '' || $do_not_text =='false'){
//do this first thing
}
else{
//do something else
}
I can't get my else to run. What syntax am I messing up?
Related to this, I'm storing a value in my database (that will be what's called to set $muted in my real code) that's either 'true', 'false', or ''. What data type should I be storing these as? Currently, I'm using VARCHAR, but I suspect this is all part of the problem.
$do_not_text == '' evaluates to true. Why? Because $do_not_text is not defined which is a falsy value. You are comparing it to an empty string which also equates to a falsy value. So that comparison is true causing the first if statement to be evaluated as true.
I'm not sure why you're using strings for what should be boolean values.
$muted = true; // no need for quotes
You might also consider using the === operator when comparing boolean values.
if ($muted === false || // etc...
What data type should I be storing these as?
Boolean values in MySQL are typically stored as 1 or 0.
field_name TINYINT(1) UNSIGNED NOT NULL DEFAULT 0
Store them as TINYINT with length of 1 because its only 1 and 0, and 0 as the default value.
Then you can make $muted = boolval($db_muted_val); if you want, or use $db_muted_val as is, because 1 is true and 0 is false.
if ($db_muted_val) {
// do this first thing
} else {
// do something else
}
I am new to PHP and am currently constructing a do/while loop from a tutorial. I would understand if the whole condition was ($variable == true) or ($variable == false), however in the tutorial the while condition is simply while($variable). Could anyone explain this to me?
Here is the tutorial code.
<?php
$loopCond = false;
do {
echo "<p>The loop ran even though the loop condition is false.</p>";
} while ($loopCond);
echo "<p>Now the loop is done running.</p>";
?>
All such conditional statements, including while and if, are evaluating the given expression against true. If the expression results in true, the statement executes the action. If it results in false, it won't.
$var == true is an expression which compares $var to true. The result of this expression is either true or false. The important point to understand here is expressions. Expressions are things which return values. Try var_dump($var == true) or var_dump(4 > 6). It shows you that the expressions return a boolean value. Here:
if ($var == true)
first $var is compared to true, which yields either the value true or false, which is then evaluated by if whether it's true or false, which then prompts if to execute the following statement or not.
In other words: it's redundant.
if ($var)
This simply causes if to evaluate whether $var is true or false and then execute the following statement. The == true is essentially already "built in".
The following statements are all essentially equivalent:
if ($var)
if ($var == true)
if (($var == true) == true)
if ((($var == true)) == true) == true)
...
A boolean value true or false should not be used with a redundant $c == true as the result is the same as $c: true or false
$driving = true;
while ($driving) {
while ($driving == true) { // ugly
while (! $driving) { // while not driving.
while ($driving == false) { // ugly
$drinking = ! $driving;
if ($driving && $drinking) {
Hence also use adjectives for boolean variables.
A condition is met, if the value or statement in it is considered as true.
The code $variable == true is a statement that looks whether the value of the variable is true and if it is, yields true - Or false if it is not.
However, as this means, that $variable itself can only ultimately be true or false, you don't even need the statement, as its return value will also be one of those two.
Therefore $variable is exactly the same as $variable == true.
I hope this made it clear.
The semantic of while/do-while is
while(<boolean expression>) {
// do your stuff
}
A boolean expression is anything that evaluates to true or false. So, if $loopCount is true, then $loopCount == true is checked on every loop and evalutes to true. But you could also write $LoopCount as condition, since it also evaluates to true.
This is very handy for using other data types, e.g. integers.
$count = 0;
while ($count < 10) {
$count = $count +1;
}
Here $count < 10 is a boolean expression that evaluates to true as long as $count is not higher then 9.
A while loop runs as long as the condition is met, in other words, as long as the boolean expression you provide evaluates to true.
You can also just use a variable, e.g. $loopCount when that variable evaluates to a boolean or a constant (even the constant value true).
Like Padarom said: Therefore $variable is exactly the same as $variable == true.
In your case: The while-do loop determines if redo the loop-body after the first run. Means the loop-body is executed exactly one time regardless what value $variable has. After the first run, the while($variable) checks if the expression is true. If so, the loop-body is executed second time and so forth.
Check PHP reference for do-while loops here. PHP.net do-while reference
while ($loopCond) and while ($loopCond == true) is the same thing. It checks the "trueness" of whatever you put in the brackets.
If I ask a question "does sun set in the west ? " what would be your answer, definitely YES OR TRUE. Same as compiler always look for statement value. Take a look
$condition = true;
if($condition == true )
// above will return TRUE; in short $condition == true will replaced by true at runtime. But if we place true directly which is $condition value or can say we place $condition instead true thus statement become shorten and look like...
if($condition) {
}
I am trying to compare the following: if the value is not an array or the value does not equal false, return.
if (is_array($value) != true || $value != false) return;
This, and any other variation I am trying doesnt seem to work. However, when I compare these individually in their own if statements they return the correct results.
Any help would be greatly appreciated!
You originally said "if it's not an array or not false". You're thinking backwards. You want "if the value is an array or false continue, else return".
So:
// This says: "if it's not (an array or false)"
if(!(is_array($value) || $value === FALSE)) return;
Using De Morgan's laws, we can convert this to
// This says: "if it's not an array and not false"
if(!is_array($value) && $value !== FALSE) return;
I would write it like this:
if (!is_array($value) || $value !== false)
Many things in php can be == false, but only boolean false can be === false.
Check out boolean type manual page and comparison operators manual page
Is there a diference between this comparisons ?
What is the diference between ! and === FALSE ?
if (!class_exists($class)) {
require($class.'.php');
}
if (class_exists($class) === FALSE) {
require($class.'.php');
}
In this case, no.
Some people think it's good programming style to explicitly show that they're comparing to a boolean. Personally... I don't like it, but I guess the more verbose form is more obvious, as the ! operator isnt the mose visible thing when smashed between a parenthesis and other vertically'ish characters.
Yes both are the different things:
php automatically considers 0 as "false" and 1 as "true" so when ever you use function response directly inside the if condition at that this both makes a difference.
consider a function, if executed properly at that it returns int number. it may be 0 too.
But if function did not match requirement at that it is returning false.
So at this time function returning value 0 is success. event though the result is zero. At this if you check this in if condition like
$return = someFunction();
if($return){
//code if ture
}
so if $return is 0 your if code will not be executed even your function execution was correct so in that case you should check like
$return = someFunction();
if($return !== FALSE){
//code if ture
}
=== and !== are used to check the response exactly match return type also.
if('0' === 0)
will return false
but
if('0' == 0)
will return true...
Hope your idea is clear now.
check this out:
if('0' == 0){
echo 'Hi, I will be in screen :)';
}
if('0' === 0){
echo 'I will not be in screen :(';
}
In the code below when $start_limit and $end_limit are FALSE then A should be run. Instead B is occurring. I've tested that both variables are FALSE with var_dump.
I am using is_null because $start_limit is occasionally set to 0 and I want a condition where 0 counts as TRUE.
if (is_null($start_limit) && is_null($end_limit)) {
A
} else {
B
}
Any suggestions as to how to get A to run when both variables are FALSE would be very much appreciated .
Just use coercion-to-boolean. !0 and !false both evaluate to true.
if (!$start_limit && !$end_limit) {
// A
} else {
// B
}
http://ideone.com/aBbWJ
I think you want to use === instead of is_null. False is not null, but 0 !== false. The triple equality check is the type of exact matching you are looking for.
perhaps
if( false === $start_limit && !$end_limit ) {
// there are no limits, set the course for the heart of the sun!
}
else {
B
}