PHP "IF conditions" [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Example 1)
if(5 == $brandId)
{
}
Example 2)
if($brandId == 5)
{
}
can somebody please explain the difference between this two conditions

In first condition you have 5 compared with value of $brandId, while in second condition you have $brandId value compared with 5.
It's only code style difference, because there is no difference (5 == 5)

They work identically as you can say 5 == 4 + 1 or you can say 4 + 1 == 5. In that respect they are identical as both compute to true (as a boolean).

One reason to use example 1 is if you typo it, like if ($brandId = 5), then the $brandId will be overwritten with the value 5. I don't know any other differences between these two, I tend to use the latter (example 2), because I think its easier to read/understand.

Related

Php Or operator doesn´works propertly [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I´m trying to use or operator in a php sentence but only validate the first condition, what´s wrong?
if ($_SESSION['user_id'] != 4 || $_SESSION['rol'] != 1)
{
//exit
}else{
//do something
}```
You should invert the condition in this way:
if ($_SESSION['user_id'] == 4 || $_SESSION['rol'] == 1)
//it's ok
else
//exit
Because otherwise if the user_id is 4 it will otherwise fail if it doesn't have the role expected

Why is "00:00:00" seen as being the same thing as NULL in MYSQL Time field? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a mysql field called "time_of_birth" that is definitely set to NULL.
I have a PHP check on that field like this:
if ($row['time_of_birth'] !== "00:00:00") {
//do this stuff
} else {
//do something else
}
For that field that is definitely set to NULL, it's doing the "do this stuff". Isn't NULL different than "00:00:00"? I would expect it to do "do something else" because it's not exactly equal to "00:00:00".
What am I missing here?
I think it is doing it correctly. It is not seeing null as equal to '00:00:00' !== is a proof that it is not seeing it as same and entering that block.

PHP comparing two POST not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm currently having an issue with PHP, I'm trying to compare two POST variables using an if statement. However when i use != it will not work, but == will work and i don't understand why.
if($_POST["PasswordSignUp"] != $_POST[PasswordSignUpRepeat]){
setcookie("PassMatch","Error");
}
Edit:
Okay, i just had an idea that may fix my problem, at the moment i am using the cookie as basically a global variable. Is there a way to define a global variable to use from page to page without having to 'include' or 'require' it into the code.
$_POST[PasswordSignUpRepeat] should be $_POST['PasswordSignUpRepeat']. Also, use paranthesis in comparison. It isn't mandatory, but it's a good process to follow.
if(($_POST["PasswordSignUp"]) != ($_POST['PasswordSignUpRepeat'])){
setcookie("PassMatch","Error");
}
Try below:-
if($_POST['PasswordSignUp'] !== $_POST['PasswordSignUpRepeat']){
setcookie("PassMatch","Error");
}
!== will check fr value and type.

PHP Else, Elseif Logic Correct? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to have 3 actions based on an order being 0 percent fulfilled, 100 percent fulfilled or somewhere in the middle.
The tests I have ran so far suggest this is correct but as I could check every possibility I was hoping someone could run their eyes over it and point out any obvious wrong-doings.
//$success & $unsuccess are counts of the good/bad fulfilment items.
$percentfulfilled = round((($success/($success+$unsuccess))*100),2);
//If 0% fulfillled
if($percentfulfilled == '0')
{
}
//If 1-99% fulfilled
elseif($percentfulfilled < '100' && $percentfulfilled > '0')
{
}
//If 100% fulfilled
elseif($percentfulfilled = 100)
{
}
Your last statement is missing an equal
elseif($percentfulfilled == 100)
//^here
In the last stament you were not checking the condition but you were assigning a value to variable.

PHP If statement with two timestamps [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to run a PHP if statement that says if there are more than 3600 seconds between two MySQL timestamps than do something. I think I have the IF statement written correctly but something is going on where it is always being resolved to true.
$time1=mysqli_query($con,"SELECT time_to_sec(max(time)) as time FROM table");
$time2=mysqli_query($con,"SELECT time_to_sec(max(time)) as time FROM table and time <>(SELECT max(time) FROM table)");
if (($time1->fetch_object()->time) - ($time2->fetch_object()->time) > 3600);
{
echo $test
}
When I run:
echo $time1->fetch_object()->time-$time2->fetch_object()->time;
It shows the correct value that I would expect but even if its less than 3600 it still does the echo $test. Any ideas what can be causing this?
Thank you!!!
Use CASE statements, a better and efficient approach:
Select event, case when TIMESTAMPDIFF(SECOND, last_timstamp, first_timestamp)<3600 then `under_time` when TIMESTAMPDIFF(last_timstamp, first_timestamp)>3600 then `over_time` end case as `duration_flag` from your_table where your_conditions
Then in your PHP you condition the output based on the queried flag.
This way you make less requests to the server, and has a cleaner query.
Maybe mysql has an old bug with TIMESTAMPDIFF, so if it doesn`t work, use this instead:
Time_To_Sec(last_timstamp) - Time_To_Sec(first_timestamp)

Categories