ayaya Posted December 29, 2012 Share Posted December 29, 2012 Hello, I want to have a multiplication table only from 3 to 7. Please see my code, first: <?php function fun($startNum,$endNum=9,$order){ if($startNum == ""){ echo "fun () // repeat an alert six times: <br>"; for($j=1;$j<=6;$j++){ echo "please enter at least one number!"."<br>"; } } else if($order=="resort"){ //multiplication table in descendant order for($j=$endNum;$j>=$startNum;$j--){ for($l=1;$l<=$j;$l++){ print "$l*$j=".$j*$l." "; } echo "<br />"; } } else { //multiplication table in ascendant order for($j=$startNum;$j<=$endNum;$j++){ for($l=1;$l<=$j;$l++){ print "$l*$j=".$j*$l." "; } echo "<br />"; } } } echo "multiplication table: <br>"; fun(1); echo "<br />"; echo "ascendant from 3 to 7: <br>"; fun(3,7); echo "<br />"; echo "descendant from 7 to 3: <br>"; fun(3,7,"resort"); echo "<br />"; fun(); ?> The result of this code is: But I want to have this: So I change my code to this: else if($order=="resort"){ //multiplication table in descendant order $l=$startNum; for($j=$endNum;$j>=$startNum;$j--){ for($l=$j;$l<=$j;$l--){ print "$l*$j=".$j*$l." "; } echo "<br />"; } } But it doesnt work! it's a dead loop... why? Can anyone help me? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/272501-php-newbies-question-for-a-multiplication-table/ Share on other sites More sharing options...
ayaya Posted December 29, 2012 Author Share Posted December 29, 2012 (edited) hehe, finally i did it myself! i'm very happy else if($order=="resort"){ //multiplication table in descendant order for($j=$endNum;$j>=$startNum;$j--){ for($l=$startNum;$l<=$j;$l++){ print "$l*$j=".$j*$l." "; } echo "<br />"; } } else { //multiplication table in ascendant order for($j=$startNum;$j<=$endNum;$j++){ for($l=$startNum;$l<=$j;$l++){ print "$l*$j=".$j*$l." "; } echo "<br />"; } } Edited December 29, 2012 by ayaya Quote Link to comment https://forums.phpfreaks.com/topic/272501-php-newbies-question-for-a-multiplication-table/#findComment-1402094 Share on other sites More sharing options...
timothyarden Posted December 30, 2012 Share Posted December 30, 2012 (edited) Not entirely sure how to fix your problem but one thing I picked up on is that if you want in your function to have a non-required parameter you need to either do = NULL or = '', otherwise when you execute that function without it, it will have an error. Examples: function fun($var,$var2,$var3){ //Do stuff } fun('value','value2'); //Will fail function fun2($var,$var2 = NULL,$var3 = ''){ //Do stuff } fun2('value'); //Will Run Hope this helps Timothy Edited December 30, 2012 by timothyarden Quote Link to comment https://forums.phpfreaks.com/topic/272501-php-newbies-question-for-a-multiplication-table/#findComment-1402108 Share on other sites More sharing options...
Christian F. Posted December 30, 2012 Share Posted December 30, 2012 timothyarden: The parameters can have any value set, doesn't need to be an empty string or null. Quote Link to comment https://forums.phpfreaks.com/topic/272501-php-newbies-question-for-a-multiplication-table/#findComment-1402209 Share on other sites More sharing options...
timothyarden Posted December 30, 2012 Share Posted December 30, 2012 Yep, they can - I meant for if he just wanted to use them if they were set - otherwise not. Quote Link to comment https://forums.phpfreaks.com/topic/272501-php-newbies-question-for-a-multiplication-table/#findComment-1402238 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.