PHP Weird IF statement result on boolean? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So i have this code:
echo $argi[2][$iterate2][$iterate];
if ($argi[2][$iterate2][$iterate]==TRUE){
echo " It's true ";
}
And the result is:
true It's true
or
false It's true
Do you know why?
I tried to change IF to var=TRUE, var===TRUE, var=="true", var=1
None of these worked. I haven't got that kind of problem earlier, it's really weird...
P.S. If statement is inside two foreach loops. I don't know if that matters somehow...
EDIT
I changed echo to var_dump, and the result is:
string(5) "false" It's true
EDIT 2
Here's the part of the script:
$iterate2=0;
foreach ($UpdateData as $key => $value) {
$sql.="UPDATE `addtmptable` SET ";
$sql.="$key = CASE ";
$iterate=0;
foreach ($value as $val) {
$sql.="WHEN user = '$UpdateDataU[$iterate]' THEN $val ";
var_dump($argi[2][$iterate2][$iterate]);
if ($argi[2][$iterate2][$iterate]==true){
echo " It's true ";
}
$iterate++;
}
$sql.=";";
$iterate2++;
}
I stopped on this, i want to fire the inner foreach if the statement is true

You are testing a string against a boolean value.
The only strings that evaluate to false are "" and "0".
if (strtolower($argi[2][$iterate2][$iterate]) != "false") {
echo " It's true ";
}

A string with any value will always evaluate to boolean value true. Change your comparison to $argi[2][$iterate2][$iterate] == "TRUE" instead.

Related

how to create "if" by values of an array elements in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
i have an php array with Unknown number of elements (1 to 6 elements). the elements are boolean (false or true)
i want to write an if block that do some statements if all of array element are true and do some other statement if at least one of element value is false. can you help me?
Try something like this =)
$myarray = [true,true,true,true,true];
// no true elements
if (!in_array(true, $myarray)) {
echo 'no true elements';
}
// no false elements
if (!in_array(false, $myarray)) {
echo 'no false elements';
}
// at least one false element
if (in_array(false, $myarray)) {
echo 'at least one false element';
}

I am unable to understand below php Function code - [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How it will print hello - If hello is true, then the function must print hello, but if the function doesn’t receive hello or hello is false the function must print bye.
<?php
function showMessage($hello=false){
echo ($hello)?'hello':'bye';
}
?>
So if you don't want any condition you can add default value bye to para,eter. And simply echo it
<?php
function showMessage($hello="bye"){
echo $hello;
}
?>
Basically ($hello)?'hello':'bye'; is the shorthand for:
if ($hello == true) {
echo 'hello';
} else {
echo 'bye';
}
Reference: http://php.net/manual/en/control-structures.if.php
You are using ternary operator inside function, which will check the type of variable true or false. By default $hello variable type will be false.
So below code will be check if variable type is true then prine 'hello' else ternary operator will be print 'bye'.
It is same as like below
if($hello==true){
echo 'hello';
}else{
echo 'bye';
}
The reason why showMessage('abc') now prints 'hello' is because the ($hello) will evaluate to true as a non-empty string.
I guess what you are looking for is the type comparison operator ===. It will check whether the argument passed is actually a boolean value.
function showMessage($hello=false) {
echo ($hello === true)?'hello':'bye';
}

PHP Check if var is 0 or 1 and then print accordingly [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Im getting an integer from my database that is either 0 or 1. With this information i want to change a string to say Esea. This is my code which isn't working:
<?php
$esea = '';
if (!empty($final_data['esea'])) {
$esea = 'Esea'
}
?>
Then this is where i print it:
<p><?php echo $esea ?></p>
$myvalue= '';
if (!empty($final_data['esea'])) {
$myvalue= 'Foo'
} else {
$myvalue= 'Bar'
}
echo $myvalue;
If the output is Foo then $final_data['esea'] was one of:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
See the empty() documentation.
If the output is Bar then $final_data['esea'] was none of the above values.
Also see the comments here and here.
Also note that the above code can be rewritten to:
$myvalue= '';
if (empty($final_data['esea'])) {
$myvalue= 'Bar'
} else {
$myvalue= 'Foo'
}
echo $myvalue;
Which is functionally exactly the same. The only difference is emtpy(...) v.s. !empty(...) (and ofcourse the logic in the if/else swapped).

Check if atleast of the variables have a value in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two variables in php/magento as in below
$currentA = $advert->getA();
$currentB = $advert->getB();
I want to make sure that atleast one of these have a value....Basically a validation to make sure atleast one of these have a value. Am I doing it correct?
$currentA = $advert->getA();
$currentB = $advert->getB();
if (!($currentA != '' || $currentB !== '')) {
echo "do something";
}
It is more complicated than that. Like SQL fields, php variables may also be NULL and generate warning when accessed for data.
So use empty(var) because that tests for all of the possible empty conditions and doesn't give warnings if the variable has been declared without a value.
if (!(empty($currentA) || empty($currentB))) {
echo "do something";
}
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float) "
0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
You wouldn't need the ! because if either of these have a value, it will return true, and the ! operator checks if this condition is false, so it will work opposite of when it's supposed to. You should try
if ($currentA || $currentB) {
echo "do something";
}

Error on line that doesnt exist [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to define a link based of a $_GET variable, but it's saying there's an error on a line that doesn't exist...
<?php
if(isset($_GET['ref'])){
if(!empty($_GET['ref']))
{
$ref = $_GET['ref'];
}
?>
<?php
if ($ref != "") {
$link = "http://site.com/page.php?ref=$ref";
} else {
$link = "http://site.com/page.php";
}
?>
Anyone see what's up? I was pretty sure it was fine.
I've tried it multiple different ways, with isset etc... same result.
You are missing a closing }:
if(isset($_GET['ref'])){
if(!empty($_GET['ref']))
{
$ref = $_GET['ref'];
}
}
By the way, this code is quite redundant. empty() will also check whether the variable is set, so you don't need isset().
You can also use the ternary operator, which is for cases like this:
$ref = empty($_GET['ref']) ? null : $_GET['ref'];
And later check with:
if (!is_null($ref)) {
//whatever
}
Otherwise, in your code, when execution reaches if ($ref != "") {, the variable $ref might not even exist - this will throw an E_NOTICE, which you might not even see, depending on your settings.

Categories