Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide output if variable don't have data PHP [duplicate]

Tags:

php

I made this:

 global $product;
 $koostis = $product->get_attribute( 'color' );

 echo "<h2>Color: ".$koostis."</h2>";

This show a color attribute on my product.

But if a product don't have a color attribute this script show only: "Color:" of course.

I try to use:

if (isset($koostis)) {
echo "<h2>Color: ".$koostis."</h2>";
}

But don't work because the variable is not exactly empty, i need a method to hide echo "<h2>Color: ".$koostis."</h2>"; if the variable not contain data.

Exist?

like image 486
Eugenio Segala Avatar asked Apr 26 '26 13:04

Eugenio Segala


1 Answers

If the variable is blank, you can check for that with empty:

if (!empty($koostis)) {
    echo "<h2>Color: ".$koostis."</h2>";
}
like image 92
John C Avatar answered Apr 29 '26 05:04

John C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!