I have this condition here:
if($name != '' || $name != 0 || $name != "0"){
$where .= ' AND readyBuilt.home_title = "' . $name . '"';
}
My problem with this condition if $name is equal to "0" it still adds the $where to my $where variable. how do I fix this ?
i think, you mean something else, try use && instead of ||
Well, if this statement is true:
$name is equal to "0"
Then this condition evaluates to true:
$name != ''
Therefore the entire conditional check evaluates to true.
It sounds like you want to use && instead of ||:
if($name != '' && $name != 0 && $name != "0")
That way the entire condition will evaluate to true only if all three conditions are met, instead of only one condition.
The best solution is to use :
if(!empty($name))
Just using this should solve your issue:
if($name){
//your condition
}
try this
if ( !in_array($name, array('','0',0), true ) ) {
$where .= ' AND readyBuilt.home_title = "' . $name . '"';
}
Related
I know that is basic question, but i can't figure out how to test if $_GET['zanr'] contains '' or 'sve' to do other stuff in my script...i try using this code but it only checks '' and 'sve' is ignored...so how to check if 'zanr' contains '' or 'sve' do stuff a else do stuff b?
if (isset($_GET['zanr']) === '' || isset($_GET['zanr']) === 'sve'){
echo "zanr = '' or 'sve'";
} else {
echo "zanr contains other values...";
}
I also try using ?? but no success...thanks.
Since your goal is to check if your $_GET is empty, use PHP built in function: empty(). And your second statement is wrong because isset() is returning a boolean and therefore you're not validating the string itself. So be sure to remove isset() and just compare if $_GET['zanr'] has your specific string.
Use this:
if (empty($_GET['zanr']) || $_GET['zanr'] == 'sve'){
echo "zanr = '' or 'sve'";
} else {
echo "zanr contains other values...";
}
Try below code.
if (isset($_GET['zanr']) && ($_GET['zanr'] == '' || $_GET['zanr'] == 'sve')){
echo "zanr = '' or 'sve'";
} else {
echo "zanr contains other values...";
}
if condition 1 :
if(isset($_POST['searchOrder']) && $_POST['searchOrder']!='')
{
$userToSearch = "WHERE name='Conformed'";
}
If condition 2 :
if(strstr($status, "value1") !== false )
{
$hide .= 'style="display: none;"';
}
i need if condition 1 OR If condition 2 , i tried below :
if (((strstr($status, "value1") !== false) ||
isset($_POST['searchOrder']) && $_POST['searchOrder']!='')
{
$userToSearch = "WHERE name='Conformed'";
})
{
$hide .= 'style="display: none;"';
}
I also tried below , but both gave error....
if ((strstr($status, "value1") !== false) || isset($_POST['searchOrder']) && $_POST['searchOrder']!=''))
{
$userToSearch = "WHERE name='Conformed'";
$hide .= 'style="display: none;"';
}
&& has higher precedence than ||, so you might want to rearrange your conditions, or put parentheses around the second group. Then just write the second part in the same block, like this.
if (strstr($status, "value1") !== false || (isset($_POST['searchOrder']) && $_POST['searchOrder']!=''))
{
$userToSearch = "WHERE name='Conformed'";
$hide .= 'style="display: none;"';
}
Something like that?
if((isset($_POST['searchOrder']) && $_POST['searchOrder']!='') || strstr($status, "value1") !== false) {
$userToSearch = "WHERE name='Conformed'";
$hide .= 'style="display: none;"';
}
If you need to satisfy both conditions then do something like this :
if (((strstr($status, "value1") !== false)) && (isset($_POST['searchOrder']) && $_POST['searchOrder']!='')))
{
$userToSearch = "WHERE name='Conformed'";
$hide .= 'style="display: none;"';
}
If you want two conditions alternatively you should remember about grouping.
(condition1) || (condition2)
In your case you've got && condition as part of second case, it is not grouped so it is processing first your OR condition between condition1 and first part of condition2, than last AND which is not you wanted.
i want to check two string in if condition equal or not but it is give me return true whats the problem please check my code.
$if = "(1 == 2)";
if($if){
echo 'hi';
}
see my code above.. it always return hi.. what i was done wrong please help me.
its return only hi.. i have many condition store in if variable but my first condition not fulfill so i want it now.. please suggest me.
my full code is here..
$if = "(1 == 2)";
if($location_search != ''){
$if .= " && ('."$location_search".' == '."$get_city".')";
}
if($location_state != ''){
$if .= " && ('."$location_state".' == '."$get_state".')";
}
if($location_bedrooms != ''){
$if .= " && ('."$location_bedrooms".' == '."$get_bedrooms".')";
}
if($location_bathrooms != ''){
$if .= ' && ('."$location_bathrooms".' == '."$get_bathrooms".')';
}
if($location_type != ''){
$if .= ' && ('."$location_type".' == '."$get_type".')';
}
if($location_status != ''){
$if .= " && ('".$location_status."' == '".$get_status."')";
}
if($if){
echo 'hi';
}
i added this code but always return tru and print hi. please help me.
Collect all of your variables in one array and go through it with foreach checking everyone. If one of variables is empty, assign false to your if variable. Will write code later, when will be next to computer.
Your variable $if is a declared as a string due to the double quotes. How about this
$a = 1;
$b = 2;
if ($a == $b) {
// When $a equals $b
echo "hi";
} else {
// When $a doesn't equal $b
$if is a string, and not null. In PHP this means it's true. I think if you remove the quotes, it should instead evaluate the expression to false.
Remove the double quotes here " (1 == 2)"
You can remove quotes (with quotes it's a normal string), but keep in mind that this is bad approach and should not be used like this.
Working code:
$if = (1 == 2); // evaluates to true
if($if) {
// code
}
My SQL query below won't output the $srchRegion && $srchCategory string if there are $_POST values for both. Why?
if (($srchRegion!='00') || ($srchRegion!='') && ($srchCategory=='00') || ($srchCategory=='')) {
return $query .= "AND region='$srchRegion'";
} elseif (($srchRegion=='00') || ($srchRegion=='') && ($srchCategory!='00') || ($srchCategory!='')) {
return $query .= "AND category='$srchCategory'";
} elseif (($srchRegion!='00') || ($srchRegion!='') && ($srchCategory!='00') || ($srchCategory!='')) {
return $query .= "AND region='$srchRegion' AND category='$srchCategory'";
}
Is there a better way of doing this?
Instead of looking for all possible boolean values and cluttered if-else, why just keep on appending the where clause based on corresponding value populated.
if($srchRegion != '00' && $srchRegion != '')
$query .= " AND region='$srchRegion'";
if($srchCategory != '00' && $srchCategory != '')
$query .= " AND category='$srchCategory'";
return $query;
So $query acts as query builder and it keeps on adding where clause based on $_POST values being set.
Try this:
if(($srchRegion!='00' || $srchRegion!='') && ($srchCategory=='00' || $srchCategory=='')){
return $query .= "AND region='$srchRegion'";
} elseif(($srchRegion=='00' || $srchRegion=='') && ($srchCategory!='00' || $srchCategory!='')){
return $query .= "AND category='$srchCategory'";
} elseif($srchRegion!='00' || $srchRegion!='') && ($srchCategory!='00' || $srchCategory!='')){
return $query .= "AND region='$srchRegion' AND category='$srchCategory'";
}
You haven't structured the if condition right. You check two OR conditions with one AND condition, so you'll need to put the OR conditions in brackets.
Here is my If Else Statement
if(isset($row['content']) && strlen($row['content'])) {
$content = $row['content'];
}
elseif(isset($row['description']) && strlen($row['description'])) {
$content = $row['description'];
}
I tried to create a condition using ternerary operator and ended for with a error: Here is my ternerary condition
$content = isset($row['content']) && strlen($row['content']) ? $row['content'] : isset($row['description']) && strlen($row['description']) ? $row['description'] : '';
What is the correct statement?
You're making your code very very unreadable by changing your condition into a ternary operator. Anyhoo, the following works without an error.
$content = (isset($row['content']) && strlen($row['content']))
? $row['content']
: (isset($row['description']) && strlen($row['description'])
? $row['description']
: '');
Wrapped the last expression in parenthesis so PHP doesn't try to evaluate it separately.
Try putting inside bracket the first term of ?: and the last term of first ?:.
$content = (isset($row['content']) && strlen($row['content'])) ? $row['content'] : ((isset($row['description']) && strlen($row['description'])) ? $row['description'] : '');