Jump to content

Recommended Posts

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 by oobradersoo
Link to comment
https://forums.phpfreaks.com/topic/285718-creating-your-own-function/
Share on other sites

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.

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 by billyb14

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.

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.

  • 2 weeks later...
{

 

// 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>";

}

//

}

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.