oobradersoo Posted January 27, 2014 Share Posted January 27, 2014 (edited) i dont want to use "str_repeat". could someone give me some ideas how to make my own function without using str_repeat i am wanting the user to enter 4 numbers into 4 input boxes for example input 1 : 6 input 2 : 5 input 3 : 4 input 4 : 3 answer 666666 55555 4444 333 Edited January 27, 2014 by oobradersoo Quote Link to comment https://forums.phpfreaks.com/topic/285718-creating-your-own-function/ Share on other sites More sharing options...
.josh Posted January 27, 2014 Share Posted January 27, 2014 If you don't want to use str_repeat (why?), then you need to use a loop that counts to whatever the number is. Quote Link to comment https://forums.phpfreaks.com/topic/285718-creating-your-own-function/#findComment-1466750 Share on other sites More sharing options...
oobradersoo Posted January 27, 2014 Author Share Posted January 27, 2014 i am wanting to learn how to build your own functions. i dont want to use str_repeat at all. sorry im a nub! how would you go about doing the loop? Quote Link to comment https://forums.phpfreaks.com/topic/285718-creating-your-own-function/#findComment-1466751 Share on other sites More sharing options...
Barand Posted January 27, 2014 Share Posted January 27, 2014 http://www.php.net/manual/en/control-structures.for.php Look at "for" and "while" Quote Link to comment https://forums.phpfreaks.com/topic/285718-creating-your-own-function/#findComment-1466754 Share on other sites More sharing options...
.josh Posted January 27, 2014 Share Posted January 27, 2014 oobradersoo I applaud your effort to wanting to learn it the "long" way without built-in functions. It is better that way because then you will better understand the concepts and principles of programming. You will better understand what stuff like str_repeat is doing internally, etc. You acknowledge that you're a nub, and I commend you for that as well. Which is why you need to step back and start on square 1 with the basics. As it has already been shown in your previous threads, there is no benefit to just handing you the code. You aren't learning anything from that, because you don't understand the basic syntax. There are a ton of php tutorials out there, not to mention php.net's excellent manual. Start with the proverbial "hello world!" tutorial and go from there. You need to learn to crawl before you walk. Quote Link to comment https://forums.phpfreaks.com/topic/285718-creating-your-own-function/#findComment-1466756 Share on other sites More sharing options...
oobradersoo Posted January 27, 2014 Author Share Posted January 27, 2014 i have learned how to echo out information via arrays etc. ATM i am trying to learn to build functions . Quote Link to comment https://forums.phpfreaks.com/topic/285718-creating-your-own-function/#findComment-1466762 Share on other sites More sharing options...
.josh Posted January 27, 2014 Share Posted January 27, 2014 Okay, well read up on functions. If you have a specific question, then by all means post. Quote Link to comment https://forums.phpfreaks.com/topic/285718-creating-your-own-function/#findComment-1466765 Share on other sites More sharing options...
billyb14 Posted January 30, 2014 Share Posted January 30, 2014 (edited) Hello, I don't know if this is what you want. And I'm sure the code could be more efficient, but is this what you want to do? $list = array(6,5,4,3); //Create an array with the numbers $out_arr = array(); //Create an array to hold the answers foreach( $list as $num ){ //Cycle through first array $tmp = ""; //Create temp string to hold number for ($i = $num; $i > 0; $i --){ //Loop to add $num as a string to the temp string we just made, $num times $tmp .= "$num"; } $newnum = (int) $tmp; //Create temp number to add to array array_push($out_arr, $newnum); //Add answer to array } print_r($out_arr); //Echo array on screen (for testing) I'm sorry I can't help any more, I don't really understand the question. Good Luck! EDIT: I just read you question again, and did you want that as a function as opposed to a loop that runs through an array? Maybe like this? function counter_thing($num){ //$num is the input number $tmp = ""; //Create temp string to hold answer for ($i = $num; $i > 0; $i --){ //Add $num to temp string $num times $tmp .= "$num"; } return = (int) $tmp; //Convert temp string into an int (number) and return it. } Again, I still don't completely understand what you want, nor do I understand what the function is meant to be for. (That's why I called it "counter_thing") Hope this helps Edited January 30, 2014 by billyb14 Quote Link to comment https://forums.phpfreaks.com/topic/285718-creating-your-own-function/#findComment-1467017 Share on other sites More sharing options...
arunpatal Posted January 30, 2014 Share Posted January 30, 2014 its very easy with the help of str_repeat <?php $val = $_POST["val"]; function x($y){ $ans = str_repeat($y,$y); echo $ans; } x($val); ?> <form method="POST"> <input type="text" name="val"> <input type="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/285718-creating-your-own-function/#findComment-1467032 Share on other sites More sharing options...
doddsey_65 Posted January 30, 2014 Share Posted January 30, 2014 i dont want to use "str_repeat". Quote Link to comment https://forums.phpfreaks.com/topic/285718-creating-your-own-function/#findComment-1467033 Share on other sites More sharing options...
therocker Posted January 30, 2014 Share Posted January 30, 2014 oobradersoo I applaud your effort to wanting to learn it the "long" way without built-in functions. It is better that way because then you will better understand the concepts and principles of programming. You will better understand what stuff like str_repeat is doing internally, etc. You acknowledge that you're a nub, and I commend you for that as well. Which is why you need to step back and start on square 1 with the basics. As it has already been shown in your previous threads, there is no benefit to just handing you the code. You aren't learning anything from that, because you don't understand the basic syntax. There are a ton of php tutorials out there, not to mention php.net's excellent manual. Start with the proverbial "hello world!" tutorial and go from there. You need to learn to crawl before you walk. Agreed. I started PHP when I was in High School and I did recall asking for codes on here once, but that was when I was stupid. Now I'm learning everything by myself and I know a lot more. Learning by yourself and getting errors then asking for help will help you improve. If you just ask for the codes straight forward, you won't learn a single thing. If you say "I'm a beginner, I'm a nub". That's no excuse. I too was a noob and left this forum for a year or so and came back knowing a few more than I did back then. Quote Link to comment https://forums.phpfreaks.com/topic/285718-creating-your-own-function/#findComment-1467126 Share on other sites More sharing options...
objnoob Posted January 30, 2014 Share Posted January 30, 2014 I give this guy a standing ovation for asking people to complete his class assignments. Bravo! Quote Link to comment https://forums.phpfreaks.com/topic/285718-creating-your-own-function/#findComment-1467129 Share on other sites More sharing options...
mac_gyver Posted January 31, 2014 Share Posted January 31, 2014 I give this guy a standing ovation for asking people to complete his class assignments. Bravo! this is the third thread for this particular part of the assignment. one of the points of solving simple programming problems like this, is to get you thinking like a programmer. programming is a do-it-yourself activity, not a run home to get mom to do it for you activity. you need to be trying to devise a way of accomplishing the task; then writing and testing code you think will accomplish the task; observing if it produces the result you intended; then going back and troubleshooting what it is doing when it does not. Quote Link to comment https://forums.phpfreaks.com/topic/285718-creating-your-own-function/#findComment-1467177 Share on other sites More sharing options...
oobradersoo Posted February 13, 2014 Author Share Posted February 13, 2014 { // do shit with numbers bradley_output($number1); bradley_output($number2); bradley_output($number3); bradley_output($number4); } function bradley_output($dogsbollocks) { for ($x=1; $x<=$dogsbollocks; $x++) { echo "$dogsbollocks <br>"; } // } Quote Link to comment https://forums.phpfreaks.com/topic/285718-creating-your-own-function/#findComment-1468686 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.