elseif echo issue, prints wrong value [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 7 years ago.
Improve this question
I have a problem i cant figure out.
From my point of view, this should actually work...
The database is setup with 3 different priority values.
0,1,2 where 0=LOW, 1= MEDIUM, 2=HIGH.
Following code should be able to print the correct information using if and elseif, but for some reason it prints all the rows with "high" as priority, but in the database it has 0 or 1.
$pri = $row["prioritet"];
if($pri = 2) {
$pri = "<span class='badge badge-danger'>High</span>";
} elseif($pri = 1) {
$pri = "<span class='badge badge-warning'>Medium</span>";
} elseif($pri = 0) {
$pri = "<span class='badge badge-success'>Low</span>";
}

You are using = instead of == which means you are assigning the value of $pri to 2 on line 2 instead of comparing them.

The if and elseifs need to use the comparison operators ('==' or '===', etc). By using '=', you are inadvertently trying to assign the value 2 to $pri, for example.

Related

PHP elseif statement inside of a foreach loop [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 10 months ago.
Improve this question
Hello there I have a php foreach loop running that is all working except one part. I am pulling in data from a local json file. The beginning foreach call looks like this:
<?php foreach($boatslips as $boatslip): ?>
One of the json fields is: "rent_sale_or_both" : "rent" (but that field can be rent, sale, or both,)
Lower down in the code after some other fields are successfully rendered and inside the foreach loop still i'm trying the below code to render out whether a boat slip is for rent, sale or both; and wanting to then be able to style 'rent' 'sale' or 'both' with css eventually.
<?php
if ($boatslip->rent_sale_or_both == rent) {
echo "rent";
}elseif ($boatslip->rent_sale_or_both == sale) {
echo "sale";
}else {
echo "both";
}
?>
Then some more html after the elseif.
At the end of the for each loop it is closed off properly with <?php endforeach; ?>
Obviously this is probably not correct? I think I'm making it harder than it is?
Any thoughts?
You might be missing a couple of quotation marks.
$boatslip->rent_sale_or_both == "rent"
$boatslip->rent_sale_or_both == "sale"

If else not running on Boolean value [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 2 years ago.
Improve this question
Tried the below code. All I can ever get is "not working".
bp_is_my_profile(); //this is a Boolean.
echo bp_is_my_profile(); // this echo's out either one or nothing depending on true or false.
if ($bp_is_my_profile === "true") { // I've tried putting in numbers one and zero.
echo "Have a good morning!";
} elseif ($bp_is_my_profile === "false") { // I've tried without quotes.
echo "Have a good day!";
} else {
echo "Not working"; //whatever the Boolean is, each's it prints this line of code.
}
Call the function in the if
if (bp_is_my_profile()) {
echo "Have a good morning";
} else {
echo "Have a good day";
}
You shouldn't compare with strings, since the function returns a boolean. And since it returns a boolean, there are only two possibilities, so you don't need three cases.

If a variable is something and another variable is something else do, PHP [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'm doing some PHP traning, and wanted to make my own copy of "Rock, Paper and Scissors" and then I'd like to compare the results from the user and the computer, and I tried with this code:"
if($choice = 'Rock' and $pcSelect == 'Scissors'){
echo "You win!";
}
Plz help me,
Magn0053
You are assigning 'Rock' to $choice with =. You need to test equality with == like in the second conditional.
Use && instead of and in your if
if($choice == 'Rock' && $pcSelect == 'Scissors'){
echo "You win!";
}

PHP console does not echo [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
Okay this really strange or I am missing something. When I run this very simmple PHP script on cmd I get the expected output which is 0. but when I uncomment the last two lines of code. . .nothing is displayed.
<?php
$test1 = 0;
echo $test1;
$test2 = 0;
#$test1_weight = 0:
#$test2_weight = 0;
?>
Is there some rule against declaring variables after an echo statement?
$test1_weight = 0:
--> change ":" to ";"
No there is no rule against declaring variables after an echo statement

PHP if x then image, else other image (so close) [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
It's a basic question, but I've googled all sorts of variations of it & nothing that quite answers me. I want to display one of two images. One if the criteria is met & one if it is not met. This code works. If criteria is met, it does display the image. But there is no alternative
<? if(stripslashes($getfeedbackQryRow['CompanyID'])=='344'){?><img src='img1.png' alt='Todays Bite'><? }?>
Then I put this together, but it's still wrong
<?PHP
if ($getfeedbackQryRow['CompanyID']) == '344') {
print ("<IMG SRC =/img1.png>");
}
else {
print ("<IMG SRC =img2.png>");
}
?>
This one looks like it should be right, but it throws an error... It's probably a syntax issue. Does anyone know what I'm doing wrong?
You have an extra ")"
if ($getfeedbackQryRow['CompanyID']) == '344') {
Remove the ")" here ['CompanyID'])
if ($getfeedbackQryRow['CompanyID']) == '344') {
// ^------- this is causing your error
You should do something simple like this:
$image_name = $getfeedbackQryRow['CompanyId'] == '344' ? 'img1.png' : 'img2.png';
echo '<img src="' . $image_name . '">';

Categories