John Davidson

If value is empty or 0,0 echo something else in php

0 comments
Message:


i code a value for my progress bar in percent. But i need a if / else for the case, when supply is "null" and 0,0. Doesn't work so far. Maybe someone can help me out.. what is wrong here. I appreciate it.


case 'supplyper':
$x = $coin->available_supply;
$y = $coin->total_supply;

$percent = $x/$y;
$percent_friendly = number_format( $percent * 100, 0 ) . '%'; // change 2 to # of decimals

if (empty($y)) {
$text = '0';
}

// Evaluates as true because $var is set
if (isset($y)) {
$text = '' . $percent_friendly . '';
}


My Answer:
empty or 0. Here is how you can achieve this in PHP:

php
$value = 0; // This is the value for your progress bar

if(empty($value) || $value == 0) {
echo "Something else"; // Echo something else if value is empty or 0
} else {
echo $value . "%"; // Echo the value with percent sign if it is not empty or 0
}


You can replace the value of `$value` with your actual progress bar value. This code will check if the value is empty or 0, and echo "Something else" in that case. Otherwise, it will echo the value followed by a percent sign.

Rate this post

3 of 5 based on 1685 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us