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";
}
Related
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).
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.
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 9 years ago.
Improve this question
if(isset($_POST["value1"]) && $_POST["value1"] != ""
&& isset($_POST["value2"]) && $_POST["value2"] != ""
&& isset($_POST["value3"]) && $_POST["value3"] != "") {
}
I send text type data using ajax and in the backend I've to validate like this. Is there any other better way to do that?
Use the empty function.
if(!empty($_POST["value1"]) && !empty($_POST["value2"]) && !empty($_POST["value3"])) {
}
PHP documentation
Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
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 can use the empty function.
instead of isset and checking for "" you could simply use empty:
if(!empty($_POST["value1"]) && !empty($_POST["value2"])){}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Im wondering if i can do and/or together. I'm trying to make a code that checks if both variables are wrong or if only one of them is wrong and the other one is right then I want to show a message like: "one of the "variables" are wrong.
Can anyone help me out here?
In addition to Tim Whites answer, if you don't want it nested, you could also use:
if($a == false AND $b == false) { echo "Both variables are false"; }
elseif($a == false OR $b == false) { echo "One variable is false"; }
if ($a==false OR $b==false) {
if($a==false AND $b==false) { echo "Both variables are false"; }
else { echo "On of the variables is false"; }
}
Something like that will do.
You could just use multiple IF statements, to check if 1 variable is wrong or both are wrong. Like:
if ($variable1==false && $variable2==false){
}
else{
if ($variable1==false || $variable2==false){
}
}
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.