paulmagm Posted November 6, 2021 Share Posted November 6, 2021 Hey i have a little function like <?php echo number_format($max_supply, 0, '.', '.'); if($max_supply ==0) { echo "--"; } ?> But i still have a 0 before "--" how can i hide this 0. Quote Link to comment https://forums.phpfreaks.com/topic/314175-little-function-like-if-variable-0-then-echo/ Share on other sites More sharing options...
ginerjm Posted November 6, 2021 Share Posted November 6, 2021 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? Quote Link to comment https://forums.phpfreaks.com/topic/314175-little-function-like-if-variable-0-then-echo/#findComment-1591814 Share on other sites More sharing options...
paulmagm Posted November 6, 2021 Author Share Posted November 6, 2021 (edited) 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 November 6, 2021 by paulmagm Quote Link to comment https://forums.phpfreaks.com/topic/314175-little-function-like-if-variable-0-then-echo/#findComment-1591815 Share on other sites More sharing options...
ginerjm Posted November 6, 2021 Share Posted November 6, 2021 You are echoing the formatted number and then asking for the -- to be echoed if is a 0. What did you expect? Quote Link to comment https://forums.phpfreaks.com/topic/314175-little-function-like-if-variable-0-then-echo/#findComment-1591816 Share on other sites More sharing options...
paulmagm Posted November 6, 2021 Author Share Posted November 6, 2021 4 minutes ago, ginerjm said: You are echoing the formatted number and then asking for the -- to be echoed if is a 0. What did you expect? 0 if its not available. otherwise it will deliver the number. Quote Link to comment https://forums.phpfreaks.com/topic/314175-little-function-like-if-variable-0-then-echo/#findComment-1591817 Share on other sites More sharing options...
ginerjm Posted November 6, 2021 Share Posted November 6, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/314175-little-function-like-if-variable-0-then-echo/#findComment-1591818 Share on other sites More sharing options...
paulmagm Posted November 6, 2021 Author Share Posted November 6, 2021 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 --. Quote Link to comment https://forums.phpfreaks.com/topic/314175-little-function-like-if-variable-0-then-echo/#findComment-1591819 Share on other sites More sharing options...
Barand Posted November 6, 2021 Share Posted November 6, 2021 (edited) if it is zero do this else do this endif Perhaps you should check if it's 0 then output according to the result of that check. Edited November 6, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/314175-little-function-like-if-variable-0-then-echo/#findComment-1591822 Share on other sites More sharing options...
ginerjm Posted November 6, 2021 Share Posted November 6, 2021 (edited) 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 November 6, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314175-little-function-like-if-variable-0-then-echo/#findComment-1591824 Share on other sites More sharing options...
gizmola Posted November 8, 2021 Share Posted November 8, 2021 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, '.', '.') : '--'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/314175-little-function-like-if-variable-0-then-echo/#findComment-1591857 Share on other sites More sharing options...
gizmola Posted November 8, 2021 Share Posted November 8, 2021 In general, you should only use ternary for something small like this. Don't try and overuse them, as it makes code harder to read and can lead to confusing and hard to maintain convolutions. Quote Link to comment https://forums.phpfreaks.com/topic/314175-little-function-like-if-variable-0-then-echo/#findComment-1591858 Share on other sites More sharing options...
ginerjm Posted November 8, 2021 Share Posted November 8, 2021 One reason that I RARELY use them at all. After 40 years I prefer to just write the old I/T/E. Quote Link to comment https://forums.phpfreaks.com/topic/314175-little-function-like-if-variable-0-then-echo/#findComment-1591860 Share on other sites More sharing options...
Psycho Posted November 9, 2021 Share Posted November 9, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/314175-little-function-like-if-variable-0-then-echo/#findComment-1591869 Share on other sites More sharing options...
gizmola Posted November 9, 2021 Share Posted November 9, 2021 Absolutely Psycho. Thanks for proof reading me -- I edited the code in my post. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314175-little-function-like-if-variable-0-then-echo/#findComment-1591871 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.