Is this if statement? - php

I am using CodeIgniter.
I set $config['global_xss_filtering'] = FALSE in a config file.
Then I find this code in system/core/Input.php:
$this->_enable_xss= (config_item('global_xss_filtering') === TRUE);
What actually this code it doing? It doesn't look like a ternary statement. It seems to me is
$this->_enable_xss= (FALSE === TRUE);
In this case $this->_enable_xss returns FALSE?

This expands out to:
// If global_xss_filtering is a boolean TRUE (by strict comparison)
if (config_item('global_xss_filtering') === TRUE) {
// Set _enable_xss to TRUE
$this->_enable_xss = TRUE;
}
// Otherwise set it FALSE
else $this->_enable_xss = FALSE;
The part in () (config_item('global_xss_filtering') === TRUE) is a boolean comparison which will return TRUE or FALSE. That value is stored in $this->_enable_xss.
So in your case, you are correct that you're evaluating
$this->_enable_xss= (FALSE === TRUE);
... which sets $this->_enable_xss to FALSE.

each comparison operator returns a boolean.
Yours checks if you got true left and right.
So, yes, var_dump(true === false);//bool(false)

is there more code around the statement? I would say your assessment is valid. Looking at this forum http://codeigniter.com/forums/viewthread/160281/#771216 it looks like it's just setting the _enable_xss based on the config value so you can control the setting. Why they need to do a comparison is beyond me, seems unnecessary.

Related

What does while ($variable) mean in PHP?

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) {
}

Return integer 0

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

Type juggling - Ressource to boolean

I stumbled upon this bit of code (written by someone else):
$result = do_stuff(); //returns false on failure, and mysql resource on success
return $result !== false;
If I get it right, it casts $result to a boolean and returns it.
How could we justify the use of $result !== false instead of (bool)$result? Is the former one more efficient?
I'm not sure what you mean by justify. Casting to bool will probably, depending on the result of do_stuff(), do the same, but is in fact something else and implies something else as well.
Example:
If you have 0 as returnvalue, casting to bool will make your function return false, but 0 !== false will return true. This is probably not a usecase you have here according to your comment, but it is implied.
So what you are saying here is "return false if $result is exactly false, otherwise always return true", while casting to bool will return false on any value of $result that is 'false-ish'.
My justification would be that this is clearer: you are implying that the result is either really false, or you actually consider it true.

Successfully parsed SimpleXMLElement comparison to 'false' returning 'true'

I got a very awkward and specific issue with a simplexml evaluation.
The code:
$simplexml = simplexml_load_string($xmlstring);
var_dump($simplexml);
var_dump($simplexml == false); //this comparison
var_dump($simplexml) returns the actual structure of my simplexml but the comparison returns 'true' for this specific simplexml, which I can't show the structure because of my contract.
I'm sure that's very specifc issue 'cause I tried other XML strings and the comparison returns 'false'.
$simplexml = simplexml_load_string('<a><b>test</b></a>');
var_dump($simplexml); //returns the actual structure
var_dump($simplexml == false); //returns false
I solved the problem using the '===' operator, but I'm not satisfied with just making it work. I want to understand why the '==' operator returns true.
I read about the two operators and the SimpleXMLElement and on my sight it should return 'false' for both operators.
What are the possible reasons for a comparison between a succesfully parsed SimpleXMLElement and the boolean 'false' to return 'true'?
I this this is a better way to do it using boolean casting (bool)
$simplexml = simplexml_load_string('<a><b>test</b></a>');
var_dump($simplexml); //returns the actual structure
var_dump((bool) $simplexml); // Retuns true
var_dump((bool) $simplexml == false); //returns false
var_dump((bool) $simplexml === false); //returns false
Demo : http://codepad.viper-7.com/xZtuNG
=== compares values and type… except with objects, where === is only true if both operands are actually the same object! For objects, == compares both value (of every attribute) and type, which is what === does for every other type.
EDIT 1
See Latest Bug on Something similar https://bugs.php.net/bug.php?id=54547
var_dump($simplexml == false); //returns false
This is expected behavior and it is explained by data comparison via "loose" data typing. In PHP, NULL, zero, and boolean FALSE are considered "Falsy" values; everything else is considered "Truthy." Inside the parentheses, PHP performs an evaluation of the expression. In this case, PHP evaluates a comparison of the named variable OBJECT and the boolean FALSE. They are not the same, so the return value from the comparison is FALSE and this is what *var_dump()* prints.
You can use this to your advantage in the if() statement. Example:
$simplexml = SimpleXML_Load_String('<a><b>test</b></a>');
if ($simplexml) { /* process the object */ }
else { /* process the failure to load the XML */ }
Have a look here:
http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting
It says that SimpleXML objects created from empty tags evaluate to false. Maybe that's what's going on?

Testing if either of two condtions are true

I'm trying to test if either of two variables are true using the code below, but I the code always returns the true conditions even when the variable is blank. Have I done this correctly or is it possible the variables are always true?
Thanks in advance for your help.
<?php
if (($gogo_team_member_twitter !== true) or ($gogo_team_member_facebook !== true)) {
echo('class="amb-with-socal"');
}
else echo('class="amb-without-socal"');
?>
If you need "Testing if either of two condtions are true" then your condition should look like:
if ($gogo_team_member_twitter === true || $gogo_team_member_facebook === true)
or just
if ($gogo_team_member_twitter || $gogo_team_member_facebook)
if you don't need strict comparison
You have the right idea, but you're checking that the variables are not true. Surely you want to check if either is true?
Also, try to use || and && rather than or and and, as they have a higher precedence.
I would just write
if ($gogo_team_member_twitter || $gogo_team_member_facebook)
In addition when dealing with negatives like this you can use "and"
if ($gogo_team_member_twitter !== true && $gogo_team_member_facebook !== true)
Well, you are checking if any of the 2 variables is not exactly equal to true.
If the variable is blank, it is not true, and so the condition is met.

Categories