2

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?

2
  • Try if($koostis ==null) Commented Dec 19, 2015 at 12:32
  • Check if isset and not empty $ koostis Commented Dec 19, 2015 at 12:56

3 Answers 3

5

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

if (!empty($koostis)) {
    echo "<h2>Color: ".$koostis."</h2>";
}
Sign up to request clarification or add additional context in comments.

Comments

3

You can check for not empty without any funciton:

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

Comments

1
echo $koostis != "" ? "Color:" . $koostis : "";

2 Comments

Whats wrong with this answer I thing its same as other answers by using tarnary just missing h2 tag
Don't know. Works fine and is more straight forward then to add 3 lines of code just for a simple task like this

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.