loudog Posted June 21, 2011 Share Posted June 21, 2011 Hey guy's I want you to write custom php functions which behaves like the built-in ones. What is the best way to learn how to write you're own functions?? Quote Link to comment https://forums.phpfreaks.com/topic/239952-plz-help-how-do-i-create-my-own-functions/ Share on other sites More sharing options...
fugix Posted June 21, 2011 Share Posted June 21, 2011 I suggest you look here Quote Link to comment https://forums.phpfreaks.com/topic/239952-plz-help-how-do-i-create-my-own-functions/#findComment-1232605 Share on other sites More sharing options...
loudog Posted June 21, 2011 Author Share Posted June 21, 2011 wow.. simple answers like that make me feel so dumb for asking questions like that .lol I guess that's the process you go thru to learn valuable skill in life. Like they once told me your not nosy you will never learn.lol ps....thanks to everyone in advance for having such patience with noob like me. Quote Link to comment https://forums.phpfreaks.com/topic/239952-plz-help-how-do-i-create-my-own-functions/#findComment-1232610 Share on other sites More sharing options...
fugix Posted June 21, 2011 Share Posted June 21, 2011 No problem at all. The manual that I referred you to will teach you alot about many different features that php has to offer Quote Link to comment https://forums.phpfreaks.com/topic/239952-plz-help-how-do-i-create-my-own-functions/#findComment-1232614 Share on other sites More sharing options...
loudog Posted June 23, 2011 Author Share Posted June 23, 2011 hey guys, I just got an assignment which im having alot of trouble with and i was hoping that you guy's could help me. The assignment is that i must write my own functions. I've read the php manual and yes i do understand how to write a function but im not very good at writing my own this is my first time and what makes it worst is that i can't copy someone else function i have to write my own. In this assignment you will have to rewrite some of the built-in functions. For example: If I ask you rewrite the function strrev() (strrev reverses a given string),you can do something like this: function dan_strrev($input) { $i = 0; $myLen = 0; while (true) { if (isset($input[$i])) { $myLen = $myLen + 1; $i = $i + 1; } else { break; } } $temp = ''; for ($i = $myLen - 1; $i >= 0; $i--) { $temp = $temp.$input[$i]; } return $temp; } echo dan_strrev('Hello'); echo strrev('Hello'); So, you can see that both the built-in function strrev() as well as the one we wrote (dan_strrev()) gives the same output. Please do the same for the following built-in functions: 1).str_pad — Pad a string to a certain length with another string 2).str_split — Convert a string to an array 3).str_repeat — Repeat a string 4).array_combine — Creates an array by using one array for keys and another for its values 5).array_flip — Exchanges all keys with their associated values in an array 6).array_pop — Pop the element off the end of array 7).array_udiff_assoc — Computes the difference of arrays with additional index check, compares data by a callback function .array_walk — Apply a user function to every member of an array 9).array_uintersect — Computes the intersection of arrays, compares data by a callback function 10).usort — Sort an array by values using a user-defined comparison function 11).array_flip — Exchanges all keys with their associated values in an array 12).ksort — Sort an array by key 13).in_array — Checks if a value exists in an array 14).array_search — Searches the array for a given value and returns the corresponding key if successful 15).array_merge — Merge one or more arrays If you want to know how these functions,please take a look at the php manual/documentation on php.net. Email me if you have not understood anything that I have explained so far. I am not setting a deadline but it will be good if you can complete this in the next 2 weeks Quote Link to comment https://forums.phpfreaks.com/topic/239952-plz-help-how-do-i-create-my-own-functions/#findComment-1233692 Share on other sites More sharing options...
gizmola Posted June 23, 2011 Share Posted June 23, 2011 Yes, looks like you have your work cut out for you, your teacher is tough. The php manual is very nice, you can go to the manual page of any function by adding the name to the php.net url. Read the description of the functions and then start writing your code. I can give you a tip, since a lot of these are string related, and that is that a php string is also an array of characters. Try: $str = 'This is a test string'; echo $str[3]; echo ' '; $strlength = strlen($str); for ($x=0; $x echo "Character $x = $str[$x] " } You can go through an array using foreach(). Good luck, and the only way to learn is to dive into it. Quote Link to comment https://forums.phpfreaks.com/topic/239952-plz-help-how-do-i-create-my-own-functions/#findComment-1233704 Share on other sites More sharing options...
loudog Posted June 24, 2011 Author Share Posted June 24, 2011 its not so much the the function but what type of argument should i use that gets me confuse in this assignment. i don't know if i use and if statement,while loop,or , foreach. I look at the examples the they have in php manaul and it just makes me feel even more lost :-\ Quote Link to comment https://forums.phpfreaks.com/topic/239952-plz-help-how-do-i-create-my-own-functions/#findComment-1234141 Share on other sites More sharing options...
gizmola Posted June 24, 2011 Share Posted June 24, 2011 You need to replicate the behavior of the functions with php code. So you need the same arguments, and your task is to figure out how to match the behavior. Start with str_pad Quote Link to comment https://forums.phpfreaks.com/topic/239952-plz-help-how-do-i-create-my-own-functions/#findComment-1234178 Share on other sites More sharing options...
loudog Posted June 24, 2011 Author Share Posted June 24, 2011 that's the problem that idk what statement should i use. should i use the if,while, or foreach. im lost and the teacher won't except a reuse of the php net example even if i change the name or the variable. am i missing the point?? this is the php manual example <?php $input = "Alien"; echo str_pad($input, 10); // produces "Alien " echo str_pad($input, 10, "-=", STR_PAD_LEFT); // produces "-=-=-Alien" echo str_pad($input, 10, "_", STR_PAD_BOTH); // produces "__Alien___" echo str_pad($input, 6 , "___"); // produces "Alien_" ?> Quote Link to comment https://forums.phpfreaks.com/topic/239952-plz-help-how-do-i-create-my-own-functions/#findComment-1234181 Share on other sites More sharing options...
gizmola Posted June 24, 2011 Share Posted June 24, 2011 Here's the key part of the manual: string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] ) This functions returns the input string padded on the left, the right, or both sides to the specified padding length. If the optional argument pad_string is not supplied, the input is padded with spaces, otherwise it is padded with characters from pad_string up to the limit. You might ask him if you need to match every option, because some of those functions have a lot of flexibility. As you can see this defaults to taking 2 parameters, so your function needs to start with those 2, and you need to implement it. Assuming you need complete replication, you have 2 default parameters you need to define in your function. function my_str_pad($input, $length, $padchar = ' ', $location = STR_PAD_RIGHT) { // now you need to start coding it. $inputLen = strlen($input); $padsize = $length - $inputLen; if ($padsize > 0) { switch ($location) { case STR_PAD_BOTH : break; case STR_PAD_LEFT : break; default: // STR_PAD_RIGHT $repeat = ceil($padsize / strlen($padchar)); $t = $input; for ($x=0; $x $t .= $padchar; } return substr($t, 0, $length); } else { return $input; } } That should be a good start for you. As you can see i threw in one of the 3 cases, albeit probbly the easiest one. It also occurs to me that there is a better way to handle the padding string -- figure that out seperately and concat it at the very end when you return it, would be better. At any rate, flesh it out, and you have an idea how to approach the rest of these. Fair warning, there could be problems but with this, it's all off the top of my head, but at least it should give you an idea how you should start approaching the process of coding these. Quote Link to comment https://forums.phpfreaks.com/topic/239952-plz-help-how-do-i-create-my-own-functions/#findComment-1234191 Share on other sites More sharing options...
loudog Posted June 25, 2011 Author Share Posted June 25, 2011 function my_str_pad($input, $length, $padchar = ' ', $location = STR_PAD_RIGHT) { // now you need to start coding it. $inputLen = strlen($input); $padsize = $length - $inputLen; if ($padsize > 0) { switch ($location) { case STR_PAD_BOTH : break; case STR_PAD_LEFT : break; default: // STR_PAD_RIGHT $repeat = ceil($padsize / strlen($padchar)); $t = $input; for ($x=0; $x < $repeat; $x++) $t .= $padchar; } return substr($t, 0, $length); } else { return $input; } function my_str_pad($input, $length, $padchar = ' ', $location = STR_PAD_RIGHT) { // now you need to start coding it. $inputLen = strlen($input); $padsize = $length - $inputLen; if ($padsize > 0) { switch ($location) { case STR_PAD_BOTH : break; case STR_PAD_LEFT : break; default: // STR_PAD_RIGHT $repeat = ceil($padsize / strlen($padchar)); $t = $input; for ($x=0; $x < $repeat; $x++) $t .= $padchar; } return substr($t, 0, $length); } else { return $input; } } That should be a good start for you. As you can see i threw in one of the 3 cases, albeit probbly the easiest one. It also occurs to me that there is a better way to handle the padding string -- figure that out seperately and concat it at the very end when you return it, would be better. At any rate, flesh it out, and you have an idea how to approach the rest of these. Fair warning, there could be problems but with this, it's all off the top of my head, but at least it should give you an idea how you should start approaching the process of coding these. That's what im talking about gizmola. what you just did above in the quote section and how u just wrote that function even tho it was just of the top of your head and you are not sure it works That's what my problem is when i start the coding process. i get stuck i don't know if i should use the if, switch, or foreach statements like you did. how did u decide what statements to use? what process do take to know what kind of statements ur going to to be using when u begin the code or function? Quote Link to comment https://forums.phpfreaks.com/topic/239952-plz-help-how-do-i-create-my-own-functions/#findComment-1234635 Share on other sites More sharing options...
gizmola Posted June 26, 2011 Share Posted June 26, 2011 It's just something you pick up over time, and get a feel for which types of loops to use. Here, you have 3 different options that control the way the string is padded, so building that around a switch statement for each case tends to keep the code simple and clear. The same thing could be done with a big if-then-elseif statement. For loops are good when you know exactly how many times you need to run through something. While is good for when you need to loop until a condition is met (like fetching some unknown number of rows from a sql result set.) Try not to get caught up in being overly concerned that you're "doing it wrong." The worst thing that happens is you write bad code, and even writing bad code is going to teach you a lot. Quote Link to comment https://forums.phpfreaks.com/topic/239952-plz-help-how-do-i-create-my-own-functions/#findComment-1234842 Share on other sites More sharing options...
loudog Posted June 26, 2011 Author Share Posted June 26, 2011 That is the hardest thing about all this, i am still not very familiar with loops and functions. so what is the best way to learn how to use them and when to use them?? btw that last comment u made gizmola really took some weight off me. I was trying to feel like i was biting more than i could chew and was going to choke to death .thanks bro for reminding me that this is learning process. Quote Link to comment https://forums.phpfreaks.com/topic/239952-plz-help-how-do-i-create-my-own-functions/#findComment-1234903 Share on other sites More sharing options...
Philip Posted June 26, 2011 Share Posted June 26, 2011 Try not to get caught up in being overly concerned that you're "doing it wrong." The worst thing that happens is you write bad code, and even writing bad code is going to teach you a lot. I'll definitely second that - you learn from your mistakes. I still do, and I've been doing this for many years... just when you get to dealing high-end sites make sure to not make those mistakes on a large live site / database (e.g. deleting all the members.) And if you do... and you don't learn from those kind of mistakes, you shouldn't be playing around with code The PHP manual actually has pretty good explanations on each type of "control structure", with examples you can try. Like learning from your mistakes, learn by trying! Quote Link to comment https://forums.phpfreaks.com/topic/239952-plz-help-how-do-i-create-my-own-functions/#findComment-1234906 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.