Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I'm attempting to troubleshoot some code and something is happening that I can't make any sense of... I have a $forum object which contains a threadExists method, which returns an associative array of any results found or false otherwise.
The following will print the array as expected:
if (!$test = $forum->threadExists($thread_id)) {
// do something
}
echo '<pre>';
var_dump($test);
echo '</pre>';
exit;
However; by adding a condition the screen will simply print bool(true):
if (!$test = $forum->threadExists($thread_id) || $test['topic_id'] != $topic_id) {
// do something
}
echo '<pre>';
var_dump($test);
echo '</pre>';
exit;
Why is the array lost?
I'm using PHP 5.4.12.
operator precedence causes it to be interpreted like this
if (!($test = ($forum->threadExists($thread_id) || $test['topic_id'] != $topic_id))) {
// do something
}
more clearly,
$test = $forum->threadExists($thread_id) || $test['topic_id'] != $topic_id;
if (!$test) {
// do something
}
you can force the correct behavior with parenthesis
if (!($test = $forum->threadExists($thread_id)) || $test['topic_id'] != $topic_id) {
// do something
}
personally, I would write it like the following, because I hate code that is even slightly tricky to read
$test = $forum->threadExists($thread_id);
if (!$test || $test['topic_id'] != $topic_id) {
// do something
}
Read it like this:
if(!$test = $forum->threadExists($thread_id) || $test['topic_id'] != $topic_id)
Assign $forum->threadExists($thread_id) || $test['topic_id'] != $topic_id to $test
Negate and check the value of $test
And since $forum->threadExists($thread_id) || $test['topic_id'] != $topic_id evaluates to true, so you get true assigned to $test.
Fix is:
if((!$test = $forum->threadExists($thread_id))||($test['topic_id'] != $topic_id))
Parenthesis issue. You're assigning to $test the value of the compound condition, so it will have a boolean value based on whether either side of it resolves to true. Try:
if (!($test = $forum->threadExists($thread_id)) || $test['topic_id'] != $topic_id) {
Related
This question already has answers here:
whats the difference in parentheses in IF statements?
(9 answers)
Closed 2 years ago.
I have this simple if statement but I do not get the results I expect. If all three vars match then I get not supported as expected. However I expect that as soon as I change one of the vars to a value that is not in the if statement, e.g. $Main = "SomethingElse", for the if statement to not match and therefor echo supported. However, supported is only returned if all three vars do not match the if statement.
Why does this happen?
$Main = "Main_Other";
$Backup = "Back_None";
$online = "no";
if ($online == "no" && $Main == "Main_Other" && $Backup == "Back_Other" || $Backup == "Back_None") {
echo "not support";
} else {
echo "supported";
}
In your example the if statement will always return true if the value of $backup is set to Back_None.
Try using below code. Here it will check $backup value first using || operator and then it will check the result with && operator
$Main = "Main_Other";
$Backup = "Back_None";
$online = "no";
if ($online == "no" && $Main == "Main_Other" && ($Backup == "Back_Other" || $Backup == "Back_None")) {
echo "not support";
} else {
echo "supported";
}
if (isset($data['start']['tan']) AND $data['start']['tan'] == true) {
$get_data[] = 'tan';
}
if (isset($data['start']['ban']) AND $data['start']['ban'] == true) {
$get_data[] = 'ban';
}
if (isset($data['start']['pan']) AND $data['start']['pan'] == true) {
$get_data[] = 'pan';
}
Try to take only tan equal to "a111" values. Tested the code below, but I could not get even tan values.
if (isset($data['start']['tan']) AND $data['start']['tan'] == true AND $data['start']['tan'] == 'a111') {
$get_data[] = 'tan';
}
Have you tried to debug this yourself? Is it possible that the data you are looking for is not in the $data variable? Or that it's in a different format?
Try adding this:
print_r($data)
before your IF statements to help you debug the issue. Your code MOSTLY looks correct. Though, I know that I have always used .= when potentially appending to arrays, instead of just = . Like:
$get_data=array();
if ($tan) $get_data[] .= $data;
Of course, I was generalizing in my above example, but you get the idea.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I need to make a conditional statement where the 5 variables should be empty before perform an action. The problem is some variables can be zero, false or NULL value. How to catch all of them?
Which one is better?
If (!$a && !$b && !$c && !$d && !$e) {
// do some action
} else {
exit;
}
OR
If (empty($a) && empty($b) && empty($c) && empty($d) && empty($e)) {
// do some action
} else {
exit;
}
Thank you.
First of all, use "code sample" to show code.
When you use !$a you are actually casting $a to boolean.
The problem here is
$a = 0;
if (!$a) {
echo 'NOT EMPTY';
} else {
echo 'EMPTY';
}
//OUTPUT EMPTY BECAUSE 0 to boolean is FALSE
Same example with NULL value.
About empty i advice you to check the documentation
Read it and choose the way that fit your needs.
Hope this helps
This could be an approach to check multiple variables are empty at once.
<?php
$a = $b = $c = $d = $e = '';
#$b = 5; // comment out to go on else block
if (empty($a . $b . $c . $d . $e)){
echo "All variables are empty, do what you want to do man";
}else{
echo "One of variable is not empty";
}
?>
This question already has answers here:
The 3 different equals
(5 answers)
Closed 7 years ago.
I'm trying to compare two strings (one from my database and another supplied by the user) and see if they match! The problem I'm having is that they don't seem to match - even though the strings seems to be exactly identical?
My PHP code is below:
public function Verify($pdo, $id, $token) {
$prepsql = $pdo->prepare("SELECT * FROM Profiles WHERE id = '$id' LIMIT 1");
$prepsql->execute();
$currentrow = $prepsql->fetch();
$current = preg_replace("/[^a-zA-Z0-9]+/", "", $currentrow["token"]);
echo '<p>'.var_dump($current).'</p>';
echo '<p>'.var_dump($token).'</p>';
$token = preg_replace("/[^a-zA-Z0-9]+/", "", $token);
if ($current == null || $current = "") {
return false;
} else {
if (strcmp($token, $current) == 0) {
return true;
} else {
return false;
}
}
}
And here is the webpage output:
string(244) "CAAW4HRuZBuB4BACA7GffOAwLHgmLgMMLGQxDAw8IJDCwahZAh0S4wZAcP8Q9DmMwsDpBq7jFcH1EzUIsZBbhKov12utoYFQns0HhgB5xKLeDqtZBRqavaNjNSn7KAcObZAEcavQCRbGlVKZBArfDEHskBSR8qAoU543DVTZCOyHm5oYNDVafwHl0bAkc4jyIhh2YHEPaNpWGC0FhezsSidOgLjnfFq8CeLVxHH0nUZBMLgAZDZD"
<p></p>string(244) "CAAW4HRuZBuB4BACA7GffOAwLHgmLgMMLGQxDAw8IJDCwahZAh0S4wZAcP8Q9DmMwsDpBq7jFcH1EzUIsZBbhKov12utoYFQns0HhgB5xKLeDqtZBRqavaNjNSn7KAcObZAEcavQCRbGlVKZBArfDEHskBSR8qAoU543DVTZCOyHm5oYNDVafwHl0bAkc4jyIhh2YHEPaNpWGC0FhezsSidOgLjnfFq8CeLVxHH0nUZBMLgAZDZD"
<p></p><p>Not authenticated</p>
Not authenticated just means that this function is returning false...
What on earth am I doing wrong? As per advice given on other similar Stack Overflow answers, I've used the regex function to basically only keep alphanumeric characters but that has made no difference? It isn't a trimming issue either as that didn't work!
Any help would be much appreciated!
Thanks!
Here is your problem:
if ($current == null || $current = "") {
// ^ now `$current` is "", an empty string
You assign a new value to $current, an empty string.
You probably want something like:
if ($current == null || $current == "") {
// ^^ now you are comparing
I am sorry to ask such a question but am bit confused about this.
I am having simple variables defined.
$a =1;
$b=2;
$c=3;
$d="";
for($i=0;$i<10;$i++)
{
$testa = 1;
$testb = 4;
$testc = 3;
$testd = 7;
if($a!="" || $b!="" || $c!="" || $d!="") {
if($a==$testa && $b==$testb && $c==$testc && $d==$testd) {
echo $testa;
echo $testb;
echo $testc;
echo $testd;
}
}
}
This is sample php code.
what I need is that I have variables defined at top. SO in my loop i want to display result if user has any 1 variable but in below loop display result based on "and" parameter.
I actually want to skip the empty variable. SO in this case, I want as $d is empty, so it should be prevented somehow from if($a==$testa && $b==$testb && $c==$testc && $d==$testd) from here.
Any help is really appreciated.
To skip the empty values from the check, use ||
if ( ... && (empty($d) || $d == $testd)) {
Then if you also want to skip it in your echos :
echo !empty($d) ? $testd : '';