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";
}
Related
This question already has answers here:
The 3 different equals
(5 answers)
Closed 5 years ago.
why the IF(the lastest one with else if and else) is doing all the time only first condition and only the first part ($filtry_1value[$key] = 'min_cena'), even if the condition shouldnt be true. I have another solution (less dynamic), if I will not fix this one, but I would like to know, why it is not working... I think it will be a trivial thing, but I cannot see it.
PS: I am working with laravel.
$filtry_1value = ['stat', 'lokalita', 'patro', 'min_cena', 'max_cena', 'min_uzitna_plocha', 'max_uzitna_plocha'];
foreach ($filtry_1value as $key => $filtr_1value) {
$filtr_1value = \Request::has($filtr_1value) ? \Request::get($filtr_1value) : null;
if(!empty($filtr_1value)){
if ($filtry_1value[$key] = 'min_cena' OR $filtry_1value[$key] = 'min_uzitna_plocha') {
$query->where(substr($filtry_1value[$key], 4),'>=',$filtr_1value);
}
elseif ($filtry_1value[$key] = 'max_cena' OR $filtry_1value[$key] = 'max_uzitna_plocha') {
$query->where(substr($filtry_1value[$key], 4),'<=',$filtr_1value);
}
else {
$query->where($filtry_1value[$key],'=', $filtr_1value);
}
}
}
may be-
foreach ($filtry_1value as $key => $filtr_1value) {
$filtr_1value = \Request::has($filtr_1value) ? \Request::get($filtr_1value) : null;
if(!empty($filtr_1value)){
if ($filtry_1value[$key] == 'min_cena' OR $filtry_1value[$key] == 'min_uzitna_plocha') {
$query->where(substr($filtry_1value[$key], 4),'>=',$filtr_1value);
}
elseif ($filtry_1value[$key] == 'max_cena' OR $filtry_1value[$key] == 'max_uzitna_plocha') {
$query->where(substr($filtry_1value[$key], 4),'<=',$filtr_1value);
}
else {
$query->where($filtry_1value[$key],'=', $filtr_1value);
}
}
}
You need to use the double equal sign for comparisons. == not a single =
Your if's should look like:-
if ($filtry_1value[$key] == 'min_cena' OR $filtry_1value[$key] == 'min_uzitna_plocha') {
// ...
} elseif ($filtry_1value[$key] == 'max_cena' OR $filtry_1value[$key] == 'max_uzitna_plocha') {
// ...
}
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
Please professional programmers in the house, what is wrong with this code?
I get this error whenever i try to run it.
Parse error: syntax error, unexpected ';' in C:\xampp\htdocs\a\go.php on line 8
The php code:
<?php
$term=$_POST['term'];
$level=$_POST['level'];
if (
$term = 'First';
$level='js1';
)
{
header("Location: result.php");
exit();
}
elseif (
$term = 'First';
$level='js2';
)
{
header("Location: result2.php");
exit();
}
else {
$error = "Entry is invalid";
}
?>
Check your if condition. The if and else if conditions must not contain any semicolons. If you are using two comparisons you must use && or || in between them. If you are using = in the if and elseif statement then it will always return true.
<?php
$term=$_POST['term'];
$level=$_POST['level'];
if ($term == 'First' && $level=='js1')
{
header("Location: result.php");
exit();
}
else if ($term == 'First' && $level='js2')
{
header("Location: result2.php");
exit();
}
else {
$error = "Entry is invalid";
}
?>
Change your if condition
it should be
if($term = 'First'&& $level='js1') or if($term = 'First'|| $level='js1')
elseif ($term = 'First' && $level='js2') or elseifif($term = 'First'|| $level='js2')
not
if ($term = 'First'; $level='js1';)
elseif ($term = 'First' ; $level='js2';)
All of your if statements have a malformed format. All you are doing is setting variables within your if statement. Therefore you are not using assignment operators properly. An example of how your if statement should look is:
if($condition === true || $condition2 == 'hello'){
doSomething();
} else if($condition === false || $condtion2 == 'bye'){
doSomethingElse();
}
EDIT: Also i would recommend working on your code indentation skills, this will really help to read your code in the future.
You are not making correct boolean expressions in those ifs.
if ($term === 'first' && $level === 'js1') {...}
elseif($term === 'First' && $level === 'js2') {...}
Also, I strongly recommend you to put a die(); after a redirect to avoid unnecessary load (unless you need to execute code after the redirect).
try this.
<?php
$term=$_POST['term'];
$level=$_POST['level'];
if( $term == 'First' && $level=='js1'){
header("Location: result.php");
exit();
} elseif ( $term == 'First' && $level=='js2'){
header("Location: result2.php");
exit();
} else {
$error = "Entry is invalid";
}
?>
If there is a header already sent error you receive then put ob_start() to top of your php file OR You can also use javascript for this like below.
<script>window.location.href = "a.php";</script>
This question already has answers here:
The 3 different equals
(5 answers)
Closed 7 years ago.
I wrote a login script, but it doesn't work:
In this script I can't log in
if($_SESSION['logged'] == TRUE){
echo "logged in";
}
if($_POST['pass'] == "blabla"){
$_SESSION['logged'] = TRUE;
}
if($_GET['logout']){
$_SESSION['logged'] = FALSE;
}
Your first two lines are a comparison:
$var1 === TRUE;
$var2 == TRUE;
You want them to be a declaration
$var1 = TRUE;
$var2 = TRUE;
This is not legal syntax for setting a variable
var1 === TRUE;
And nor is this
var2 == TRUE;
Use = to set a variable to a value.
The === and == are comparison tests not value assignments.
This is also not going to do a test
if($var1 = TRUE){echo "3";}
it will set $var1 to be true, ditto the other 2 times you try this line for $var2 and $var3
This question already has answers here:
This code is working fine if ($(this).val() == "Spine") but not ($(this).val() == "Spine"||"Brian")
(6 answers)
Closed 7 years ago.
I'm catching the name of an user logged on $userrow['name'] and then I'm using if/else to compare to 2 strings
$user1 = "admin";
$user2 = "Gil";
if ($userrow['name'] == $user1 || $user2 ) {
echo "true";
}else{
echo "false";
}
in this case my $userrow['name'] its = "Andres" and obviously Andres != Gil || Admin
What I'm doing wrong?
More maintainable is this logic:
$users = [$user1, $user2];
if (in_array($userrow['name'], $users)) {
// Username exists
}
Your logic is incorrect. I will try in english to say what your if statement does:
If $userrow['name'] equals the value held in the variable $user1, or if the value held in the variable $user2 is not falsey ...
In $user2 you have the value "Gil". Since "Gil" is not falsey (in other words, it is not false or null or zero), then it is considered to be true when evaluated within the context of an if statement. I think this is what you want to say instead:
if ($userrow['name'] == $user1 || $userrow['name'] == $user2 ) {
You are doing the first check right, but checking $user2 gives you true because if("Admin") is true.
Change
if ($userrow['name'] == $user1 || $user2 )
To
if ($userrow['name'] == $user1 || $userrow['name'] == $user2 )
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) {