Jump to content

Little function like If $variable == 0 then echo "--"


paulmagm

Recommended Posts

1 - do not write code like you just presented to us.  Use separate lines for each statement.  You won't regret it.

2 - you should not have to enter and exit php mode for a couple lines of php code.  Unless you are poorly structuring your script.  So - break that habit.

3 - You are asking us to give you an explanation yet we don't know WTH your code is doing to the $max_supply variable.  Are you kidding?

Link to comment
Share on other sites

10 minutes ago, ginerjm said:

1 - do not write code like you just presented to us.  Use separate lines for each statement.  You won't regret it.

2 - you should not have to enter and exit php mode for a couple lines of php code.  Unless you are poorly structuring your script.  So - break that habit.

3 - You are asking us to give you an explanation yet we don't know WTH your code is doing to the $max_supply variable.  Are you kidding?

max_supply is a number. what else? maybe you should go for a forum only for solutions. i will ask somewhere else for help.

Edited by paulmagm
Link to comment
Share on other sites

2 hours ago, ginerjm said:

Actually - if you had saved the formatted number before doing the entire echo statements you probably would not be seeing the -- since the formatted resulted would be a string and therefore <>  to a numeric zero.  But you didn't.

i have both when it's 0. 0--. And i try to hide the 0 when the result is zero. I want to replace it with --.

Link to comment
Share on other sites

Here is  little script that demos how this could be written and how you wrote it.

// set the starting value
$max_supply = 0;
echo "Starting with max_supply of $max_supply<br>";
// show the formatted value
$fmt_val = number_format($max_supply, 0, '.', '.');
echo "The formatted value is: $fmt_val<br>";
//  now test the final value
echo "The test of the formatted value gives us: ";
if($fmt_val == 0)
	echo "--";
else
	echo $fmt_val;
echo "<br><br>";
//  now run the OP's original lines of code
echo "OP code results. (written on separate lines as it s/b.)<br>";
echo "Output of the formatted max_supply value: ";
echo number_format($max_supply, 0, '.', '.');
echo "<br>";
echo "Output of the test of max_supply for a value of zero: ";
if($max_supply == 0)
	echo "--";
exit();

And here is the output:

Starting with max_supply of 0
The formatted value is: 0
The test of the formatted value gives us: --

OP code results. (written on separate lines as it s/b.)
Output of the formatted max_supply value: 0
Output of the test of max_supply for a value of zero: --

I"ll let you decide what needs changing if you only want to see ONE value. 

The problem with your lines of code was that you didn't realize that number_format doesn't alter the starting value - you were seeing the output done by number_format but in the next line you were testing the original value.

 

Edited by ginerjm
Link to comment
Share on other sites

9 hours ago, gizmola said:

if then else blocks can be reduced using the ternary.  There are a few other syntax features described in that article that can reduce your code in certain cases.

<?php echo ($max_supply == 0) ? number_format($max_supply, 0, '.', '.') : '--'; ?>

 

I think you meant for the condition to be ($max_supplu != 0). Either that, or the results should be swapped.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.