PHP if data = 0 then else

Hi guys

i’m a total noob in php and just messing around with a webpage and have run into this problem.

i need to make a if/else statement and can figure out how to do it the following code is what it’s about

echo “

” . $del->Price . “”;

problem is that Price can be 0 then it should show a text instead, like below

if Price = 0 then
echo “

Please call”;
else
echo “” . $del->Price . “”;
end

what’s the best and easiest way to do this?

[php]if ($price === 0) {
echo “

Please call\n”;
} else {
echo “” . $price . “”; // Are you using some kind of framework or confusing JavaScript with PHP?
}[/php]

or

[php]/* ternary operator */
echo ($price === 0) ? “

Please call\n” : “” . $price . “”;[/php]

Hi thanks for the answer, i’m not sure what you are thinking of, so heres the full code from the webpage,

in below code it’s line 25 that should have a “switch” for the “0” check.

Thanks again for your time!

[php] case “searchengine”:
if(empty($data)){
echo “Ingen motor fundet ud fra din kode.”;
break;
}
echo “

Reservedele

”;
echo “

Her er de forskellige motorer vi har på lager:

”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
foreach($data as $del){
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “";
echo “”;
}
echo “
Model / MotorBeskrivelseKmÅrgangKortStandPris U/MomsLink
” . $del->Make . " - " .$del->Model. " - " .$del->Engine. “” . $del->Notes . “” . $del->Mileage . “” . $del->Year . “” . $del->CardNr . “” . $del->QN . “” . $del->Price . “<a href=’?action=getstockdetails&id=” .$del->StockId ."&brand=". $_GET[‘brand’]."&model=".$_GET[‘model’]."&number=".$_GET[‘number’]."’ class=‘se-mere’>Se mere
”;
break;[/php]

i was thinking of doing this, but i’m not sure if it works, and i don’t have direct access to replace the file/fix the bug, if critical php errors occur.

[php]if ($del->Price == “0”) {
echo “

Please Call”;
} else {
echo “” . $del->Price . “”;
}[/php]

Is $data a class? I think that is where the confusion is coming from.

$data is a json_decode and looks like this

i hope it clears up the confusion :slight_smile:

{"StockId":"d388a07f-425b-4f77-b881-32c7a59c5628","CardNr":"R1407914","MasterCard":"S1400055","Model":"MONDEO (01-07)","Make":"FORD","PartName":"STARTER","Year":2002,"Engine":"1.816V","Mileage":202,"QN":"B","Location":"B00E072C","Notes":"25-2466..1S7U-11000-AB","Price":0.0000,"Name":"Højby Autoophug Aps","CompanyNr":3004,"IsA":false,"NumI":2,"DeliveryPrice":0.0000,"Pg":0,"Rx":13},

I see.

[php]if ($del->{‘Price’} <= 0 ) {
echo “

Please Call”;
} else {
echo “” . $del->Price . “”;
}[/php]

Or

[php]if ($del->Price <= 0 ) {
echo “

Please Call”;
} else {
echo “” . $del->Price . “”;
}[/php]

Should work

Thanks a lot :slight_smile: i used solution 2, worked like a charm

Sponsor our Newsletter | Privacy Policy | Terms of Service