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
when i test this code , it was not echo 8 > 3 (it's will echo 1 = 1)?
i check my code are correct but why not echo real result?
<?PHP
$number = 8;
if ($number = '0')
{
echo $number." = 0";
}
elseif ($number = '1')
{
echo $number." = 1";
}
elseif ($number = '2')
{
echo $number." = 2";
}
else
{
echo $number." > 3";
}
?>
Comparison in PHP is done via the == operator, not the = operator.
<?PHP
$number = 8;
if ($number == '0')
{
echo $number." = 0";
}
elseif ($number == '1')
{
echo $number." = 1";
}
elseif ($number == '2')
{
echo $number." = 2";
}
else
{
echo $number." > 3";
}
?>
Try this:
<?PHP
$number = 8;
if ($number == 0)
{
echo $number." = 0";
}
elseif ($number == 1)
{
echo $number." = 1";
}
elseif ($number == 2)
{
echo $number." = 2";
}
else
{
echo $number." > 3";
}
?>
You were using assigning operator, instead of equal to operator, and secondly check with integer value instead of string.
The = operator assigns the value on the right hand side to the variable on the left.
if ($number = '0')
This sets $number to 0, which is a falsy value, so returns false.
elseif ($number = '1')
This sets $number to 1 which is a truthy value, which returns true. This is why your code outputs 1 = 1.
You need to instead use the == equality operator:
if ($number == '0')
...
elseif ($number == '1')
...
...
You are assigning a value to the variable $number, you'll need to use the == or better === to match the number against the value of the variable.
In this case you know $number is going to be an integer, it is best practice in PHP to compare as strict as possible, which is the === operator. You can use the == operator, however the variable will only be loosely matched at that point.
For example a boolean false will also match an if-statement containing if ($number == 0), which is not what you want.
Please check the documentation of PHP to see the full extend of comparison.
Such as the following:
<?PHP
$number = 8;
// See the triple equal sign here
if ($number === 0)
{
echo $number." = 0";
}
// and here
elseif ($number === 1)
{
echo $number." = 1";
}
// and here
elseif ($number === 2)
{
echo $number." = 2";
}
else
{
echo $number." > 3";
}
?>
Related
This question already has answers here:
The 3 different equals
(5 answers)
Closed 4 years ago.
I am trying to apply some if else statements in my code but getting a bit confused. I want a result if I got value=0 answer should be One. if I got value=1 answer should be Two and if I got any value greater then 1 value should be Three. Following is my code: -
$value = 1
if($value < 1)
{
echo "One";
}
else if($value === '1')
{
echo "Two";
}
else if($value >= 2)
{
echo "Three";
}
The problem is that it is not giving me results if value is = 1.
change this else if($value === '1') to else if($value == 1)
=== matched both value and type that's why 1==='1' is false, See Ref.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.
<?php
$value = 1;
if($value < 1)
{
echo "One";
}
else if($value == '1')
{
echo "Two";
}
else if($value >= 2)
{
echo "Three";
}
?>
DEMO: https://3v4l.org/avOWc
I want to check different conditions into if statement based on different scenario (Will get the $status value as 'Y' or 'N'). Please check the code below and let me know the issue here.
$fstrto = "10";
$cstrto = "7";
if($status == 'N')
{
$cond = "$fstrto <= $cstrto";
}
else
{
$cond = "$fstrto >= $cstrto";
}
if($cond)
{
echo "Success";
}
This code is not working as it takes the "$fstrto <= $cstrto" as variable.
Remove the quotes. Use intval/doubleval if the input is a string as in $fstrto = intval($fstrto);.
$fstrto = 10;
$cstrto = 7;
if($status == 'N')
{
$cond = $fstrto <= $cstrto;
}
else
{
$cond = $fstrto >= $cstrto;
}
if($cond)
{
echo "Success";
}
Why it works: $cond is being assigned the value of a boolean expression, the values of which can be true or false. if($cond) just checks whether $cond is true or false
what is need to do is when using string as a php code use
eval — Evaluate a string as PHP code
Use below code work like charm:
$fstrto = "10";
$cstrto = "7";
if($status == 'N')
{
$cond = "$fstrto <= $cstrto";
}
else
{
$cond = "$fstrto >= $cstrto";
}
if(eval("return $cond;"))
{
echo "Success";
}
IMPORTANT:
Use of eval is highly discouraged
NEVER EVER use eval with params by POST/GET without sanitize them
When is eval evil in php?
You "$fstrto <= $cstrto" is a string now a compare statement.
$fstrto = "10";
$cstrto = "7";
if( ($status == 'N' && $fstrto <= $cstrto) || ($status != 'N' && $fstrto >= $cstrto) )
{
echo "Success";
}
Potentially turn it into a function that funnels into a switch statement like so:
function evaluateCondition($status, $a, $b) {
switch ($status) {
case 'Y':
return $a >= $b;
break;
case 'N':
return $a <= $b;
break;
default:
// Error Log. Unknown Status.
}
}
Any future addition can be appended onto the switch statement as necessary, if it gets more convoluted have each case return a separate function() to improve readability.
In terms of the current version you could use it like so:
$result = evaluateCondition('Y', 5, 6);
var_dump($result); // bool(false)
Hope that helps.
$fstrto = "10";
$cstrto = "7";
$cond = false;
if($status == 'N')
{
if($fstrto <= $cstrto){
$cond = true;
}
}
else
{
if($fstrto >= $cstrto){
$cond = false;
}
}
if($cond)
{
echo "Success";
}
I am trying to add a condition dynamically in the if condition . But it is not working . Please help me to fix the issue.
I am trying a code like this
$day_difference = "some integer value";
if(sheduled_time == 'evening'){
$condition = '>';
}else{
$condition = '==';
}
then
if($day_difference.$condition. 0){
echo "something";
}else{
echo "h";
}
An alternative to gerald's solution; I would suggest that you use a function that validates the inputs using a switch-case operation:
function evaluate ($var1, $operator, $var2)
{
switch $operator
{
case: '<': return ($var1 < $var2);
case: '>': return ($var1 > $var2);
case: '==': return ($var1 == $var2);
}
return null;
}
What you need is the eval() method.
I.e.
$var1 = 11;
$var2 = 110;
$cond1 = '$var1 > $var2';
$cond2 = '$var1 < $var2';
if(eval("return $cond1;")){
echo $cond1;
}
if(eval("return $cond2;")){
echo $cond2;
}
As justly noted beneath, you should exercise the necessary precautions when using this method!
This is not the way to do this.
Just define a function which returns true if the desired conditions are met.
For example, we can define the function decide which receives two arguments, $day_difference and $scheduled_time:
function decide($day_difference, $scheduled_time)
{
if($scheduled_time == 'evening')
{
return $day_difference > 0;
}
else
{
return $day_difference == 0;
}
}
And use it like so:
if( decide($day_difference, $scheduled_time) )
{
echo "something";
}
else
{
echo "h";
}
according to your requirements this can be done using the PHP eval() function which i don't recommend using it only when necessary.
you can check When is eval evil in php?
you can use the below script instead:
if( $sheduled_time == 'evening' && $diff > 0 )
{
echo "This is the Evening and the Difference is Positive";
}
else if($diff == 0)
{
echo "This is not evening";
}
Thankyou for helping me solve my question
I solved this in another way
$day_difference = "some integer value";
$var1 = false ;
if($sheduled_time == 'evening_before'){
if($day_difference > 0 ){
$var1 = true ;
}
}else{
if($day_difference == 0 ){
$var1 = true ;
}
}
if($var1 === true){
echo "something";
}else{
echo "h";
}
if i have statement:
$a = 1;
$b = 2;
$c = 3;
if($a == 1 && $b == 2 && $c == 3)
{
echo 'correct';
}
else
{
echo 'what variable's weren't matched';
}
Is there any way of knowing what didn't watch instead of writing everything separately?
Cheers!
No. Your expression was turned into a boolean, so apart from checking the equality(s) again you cannot find out which triggered the "false".
You need to test each individually, but you could do something like this:
$a = 1;
$b = 2;
$c = 3;
$a_matched = $a == 1;
$b_matched = $b == 1;
$c_matched = $c == 1;
if($a_matched && $b_matched && $c_matched)
{
echo 'correct';
}
else
{
if (!$a_matched) echo 'a did not match!';
if (!$b_matched) echo 'b did not match!';
if (!$c_matched) echo 'c did not match!';
}
but that's less clear than just:
$a = 1;
$b = 2;
$c = 3;
if($a == 1 && $b == 2 && $c == 3)
{
echo 'correct';
}
else
{
if (!$a == 1) echo 'a did not match!';
if (!$b == 2) echo 'c did not match!';
if (!$c == 3) echo 'b did not match!';
}
Actually, heh, I take back my comment. You can rely on the boolean short-circuiting to set a variable indicating the last part of the conditional which was true:
if (($x = 'a') && $a == 1 && ($x = 'b') && $b == 2 && ($x = 'c') && $c == 3) {
echo "correct\n";
} else {
echo "$x is wrong\n";
}
Note, I would never write this in production code because it's goofy and very hard to understand what's supposed to be going on. But fun to fiddle with, at least.
Nope! That's not possible. You can make life a lot simpler by using arrays, though:
$results = array(1, 2, 4);
$expected = array(1, 2, 3);
$count = count($results);
$wrong = array();
for($i = 0; $i < $count; $i++) {
if($results[$i] !== $expected[$i]) {
$wrong[] = $i;
}
}
if(count($wrong) > 0) {
echo "There were wrong ones. They were at positions: " . implode(', ', $wrong);
} else {
echo "All good!";
}
For example.
I am trying to echo some text if my loop returns with no data but can't get it do work. I have tried a few things but no luck.
my code:
$result = mysql_send("SELECT * FROM datatable WHERE id='".
$_SESSION['id']."'ORDER BY id ASC LIMIT 2");
while($row=mysql_fetch_array($result)) {
$a = 1;
extract($row);
echo 'Trans ID: ';
echo $row['trans_id'];
echo '<br>';
echo 'Amount: ';
echo $row['amount'];
echo ' ';
echo $row['euros'];
echo '<br>';
echo '<br>';
}
if ($a = 1) {
echo 'No History';
} else {
echo 'View history';
};
Can Anyone help me with how to do if statements on a loop`?
You have an assignment, which returns the result of the assignment (i.e. 1)
if ($a = 1) {
instead of a comparison, which is what you probably want:
if ($a == 1) {
Therefore, "No history" will always be echoed.
use mysql_num_rows(); it will tell you if you have any results from query
$result = mysql_send("SELECT * FROM datatable WHERE id='".
$_SESSION['id']."'ORDER BY id ASC LIMIT 2");
$count = mysql_fetch_array($result);
if($count > 0) {
while($row=mysql_fetch_array($result)) {
# your code
}
}
# NOTE THE == not =
if ($a == 1) {
echo 'No History';
} else {
echo 'View history';
};
You will also have issue with your if statement as youu are using assigment rather than comparison: $a = 1 will set $a = 1 and $a == 1 will check of it equals 1
if($a == 1) ....
== is a comparison, = is an assignment.
Change to
if ($a == 1) {