Top 100 PHP Functions ( 1 - 10 ) | Learn PHP Programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what's going on it's Clara ticky and in this video we're gonna go over top 10 PHP functions so I'm getting this list from a website called execute that I oh so the credit goes to these guys and they actually have a list of top 100 PHP functions and I first wanted to make a video of top 100 PHP functions but I realize it's gonna take about five hours so instead what I decided to do is break down the videos in 10 different videos covering all 100 PHP functions and so this is the number one video in the series covering top 10 functions and so as you can see here they have a rank of the function and the function name along with the frequency and average so the frequency is how much the function is used overall in in the PHP community and the average is the amount of times the function is called in PHP project alright so let's go ahead and cover the first top 10 PHP functions so the first one here is count and I'm gonna be referring to PHP manual just because it makes things easier so account count count all the elements in an array or something in an object and so let's go over a real fast of how to read the description of the syntax provided by PHP manual so as you can see here we got an int and that is followed by the function name so the int is the value that's going to be returned by the function after we run it so count is going to return a number an integer number then we have a mixed which means it can be a mixed datatype like a string array integer float etc then we have an array and inside of these square brackets are the optional parameters so in this case we can set the mode of the count to either count normal or we can also use count recursive and I'll show you guys what that means in a second okay so count is super useful function so for example if we have an array let's say we have an array of let's create an associative array of car names actually let's do animal names Fox bear deer okay so we can either do count array so that will just count the elements inside an array so we got one two three four five six seven so it should print out the 7 yep prints out number seven so that's an integer and that is the return value of the function so that's what we expected so count is really useful when we want to create a loop and we want to go and iterate or go over the entire array so if we had a loop we can say I is less than count array and then what this will do is it's going to loop through the array until the end of the array because we know that the count is seven and we can go ahead and print out the actual array values like this insert the BR tag and that should print out all of our elements all the values of the array and in fact it does so that's why this function is the number one used function in PHP and another example is okay so I just found this out you can use either count normal but we also have something called count recursive and count recursive is used is it's used if we have another array inside of instead of our array so we can add another array here and we can say let's just use for simplicity sakes just colors blue red orange white black so now let's see what happens if we just use regular okay so you can see that we just added an array and it's counting it as just another value so we got an eight however if we add count recursive it's also going to count all the elements inside of this array so it's gonna count all these values and add it on top of what we had before so now we get a 13 so if we wanted to count the whole thing including the arrays that are inside of our array the count recursive is the way to go otherwise count normal is used by default and we get an 8 because it's not counting and recursively all right so that's count let's move on to function number two which is is array and that one is really self-explanatory it just checks if the variable is an array and once again we got the return type which is going to be boolean so that means true or false we got a function name which is array and then we have a mixed data type variable because we don't know if the variable is going to be an array or not so so that's the way to check if if we get an array okay so for example we can say before looping through the array we can say if is array array then we're going to look through it otherwise else if it's not a nari I can say something like cannot look the variable it's not an array yeah you can say something like that so so before usually before looping through an array in when writing a script we want to check if it's in fact an array and if it is then we want to loop through it otherwise don't look through it notice area to string conversion for line 9 okay so you just delete this part here because we're trying to look through a multi-dimensional array all right so it just prints out that array because we have an array and if we had something like 1 which is not an array obviously cannot loop through array very variable it's not an area okay so just a simple function to make sure that we have an array next up is sub STR that's to basically find or extract part of the string so once again the function is going to return the string in this case and then we have a string and the actual string that we are extracting the valley from and then we have a start and an optional length parameter so let's see let's just go ahead and delete this part here and then create the text okay so suppose we have a text and then extract sub STR so this is the most basic way that this function is going to work so we provide the text and we provide that it the start of the integer where we want to start extracting the text so in this case is just gonna extract the whole string and then let's not forget to print it out so since we're starting from zero and we're not specifying the length it's gonna extract the whole text and so if we want adjust the clarity just a clever word part we would say seven I think Oh actually okay so this is the zero is a starting position and then the length is how many characters we want to extract so that will extract the clever part since we're starting from zero and then one two three four five six seven see what happens reduce six okay yeah because seven also included the space so six since clever it has six letters six is enough okay so that's how we extract those words and let's try to extract techy so the starting position is going to be eight one two three four five six let's see what happens here okay so it's seven starting position seven and then six is the amount of letters that teki has and if we wanted to go from the end we can just say negative 1 and that will extract this letter e here just like that so if we wanted to extract awesome we would need one two three four five six seven negative 7 and that will extract awesome so negative number it starts extraction from the end of the string and that's pretty much it foots for the sub STR let's go ahead and move on to the next one which is in array and that is also pretty self-explanatory just by the functions name it checks if the value is in fact inside of the array it returns boolean so here we have a mixed needle which is the search value that we're looking for inside of the array and then we got the actual array which is specified by the haystack we also have optional parameter called strict and strict is also going to check the third parameter strict is set if the the strict is set to true then in array function we'll also check the types so it's going to check the data type off the needle in the haystack so it's going to make sure that the value is an integer the value is a string the value is an array etc and if the strict is false which it is by default it's just gonna use loose comparison it's not gonna check the data types so let's see what it looks like in an example okay so let's see here okay okay let's go ahead and insert some random values here inside of our existing array let's also do 100 okay so now we can who can do if in array 12.5 and then array okay so here what we're doing is we're searching for 12.5 but we're using 12.5 as a string where is 12.5 is actually a float inside of our array and since we're not setting strict so strict here is false by default it should still print out and find the value and the function should still return true so let's go ahead and see what happens so the valley is found so now if we set strictly true it shouldn't work boom you see how that works so because the strict also checks if the valley is the same data-type it this returns false because our search string is a string where is we got a float inside of our array so let's search for 100 which should now return true because it's the same data type the valley is found okay so that's just something that I didn't know about um before making this video so I just wanted to make sure you guys understand how strict works and I believe it also it's also case sensitive so if we search for Fox and let's remove true here that's also not going to be found because it's the search is case sensitive so just make sure to remember that it's case sensitive as well alright let's go ahead and move on to the next one is explode this is one of my favorite functions when reading files or when you want to convert a bunch of text into a nicely formatted array and then import the data back into the database for example explode is the way to go so if we have a bunch of just great create a new text here and say let's just use a bunch of colors for simplicity sakes okay that's enough colors okay so suppose we have a bunch of text and also let's see let's imagine that we're importing the excel data so we got some some numbers there or some let's just use I just use people's names Bob and see all right so let's go ahead okay so the way that we would use explode is when we have a bunch of text and that text is separated by by a delimiter like a comma that that is a delimiter and here the pipe is a delimiter and usually the Excel or spreadsheets are separated by either pipes or a tab so we can use tab pipe comma as different delimiters we can even use a space as a delimiter and we can go ahead and read that text into our explode function so let's create a text array and say explode so the first value that we provide is a delimiter I mean just look it up in PHP manual real fast okay so first of all the function is going to return array so we know that the return value is going to be an array and then we got a string delimiter which is a comma pipe space or any other delimiter that we want to use and then we got the actual string that we want to explode or that we want to convert into an array and finally we got an optional parameter which I'll I'll explain a little bit which is the max amount of number to break apart okay so in this case let's use for our text and that will break or explode the text up and convert it into an array and it's going to use comma as a delimiter so it knows where to break apart those values so there you go so red orange black white sign teal purple and pink and let's see what happens if you use a space here as well so now you can see that I remove the space because it's using comma and a space as a delimiter so if we want to exclude the space we would include the comma and a space as as it our delimiter so that's how that works and okay let's also use columns here so I did the same thing and exploded our text by the columns okay so we can also specify the limit as explained by the PHP in max so so with the limit I'm just gonna show you guys here so when you specify the limit is just gonna break apart the limited amount of file values so here it only it only broke apart - it only exploded the text byte and then put it inside the - array keys so if we added number two here you can see that it only created two area values so in case you wanted to limit the amount of explosion of the text this is what you would use so if I add it for it would only create four keys etc and there's also a negative value which has been added since PHP five I believe which explodes all values except the last one so it's just an additional functionality to keep in mind alright so that's pretty much it for explode let's move on to STR replace it's another very useful function used to search and replace the text of the string so this one returns the mixed or different data types then we got our mixed search mixed replace and mixed subject along with optional count ok so so we we just search for a string and let me show you some examples all right so let's just use I'm just gonna use PHP manual examples because they have a really good one on this one so so let's suppose we have some text and let's say you have some HTML text like body text and you wanted to make the body text dynamic so you would create you could create a text like this so that you could later replace it with any color that you want okay so here we can say a replace we can one can say search for this value and replace it with a color of our choice so it's a handy way to create a dynamic functionality inside of your webpage by specifying it in this way so then we can print out the body tag and that will replace percentage body with the actual color red so since this HTML is gonna show up here and you can see the actual text is in fact red alright so that's how that works and we can also specify the array and this is a really awesome so so if you have a bunch of bowels inside of your array you can actually use that to replace all the values all the bowels inside of the inside of your text or you can also do this with any other text as well so this is again coming from PHP manual and then for the actual text we can say whatever it is awesome videos ok and then let's see what happens when we do STR replace and we instead of just the text because remember this value the search value is a mixed so we're using an array in this time we're using array this time and our replace is oh we're replacing it with nothing so this this this means the empty string so we're taking all the vowels which is our search and when we're replacing it with an empty string and then our text is clear we take is awesome I love your videos and so that is going to remove all the vowels all the vowels from the text and you can see that in fact it did so this is a really awesome way to use advanced search and replace functionality on your text alright so that's it for a sorry place let's move on here to implode and implode it's pretty much the opposite of explode it you can say it glues the text back together and again we specify the delimiter or and it's not a delimiter it's a glue this time but it works in a similar way so this time it takes in the array and it glues it back into text so let's see what happens when we run it on our array here so glued equals implode and again what what are we gonna use as a glue to glue back all the values well we can use a comma you can use a comma with a space and then provide our array and then we can just echo out glued and that should print out all these values separated by comma so there it is our whole our whole array has been glued together by the comma and now it's just the single string of text separated by commas so again the function returns the string with a string as glue so that's what we used as comma and a space and we also specify the array which is the second parameter of this function and feel free to look over php.net examples as well and if you want to go more into details of what each function does alright so str length is a really easy one we just get you the the character value of of the string of the text so just it just gets the string length and you can see that it's an integer so it's going to return the integer of the of the strings that are inside the text it returns the length of a given string so here we can say echo str wine glue and the amount of characters is 52 and that's in fact an integer all right so that's STR length next up is array merge and I use this function all the time to merge arrays together so it's create a second array here and actually well you can just use this one right here vowels and suppose we wanted to merge array and vowels together so we can simply say merged array equals array marriage vows and array and then we're gonna use print R to print out print out our new array boom so it took the first array in the secondary and combined the two together so that's our a merge and this one is super useful you know you're gonna run into this one a lot so let me just check if there's anything about this function that I missed ok so again the return value is this function is going to be an array and then we got secondary and the optional parameter is a I mean we got a first array and then the second parameter is optional which is the second array so it merges the elements of one or more areas together so the values of one are appended to the end of the previous one it returns the result in array so that's exactly what we did and you can look at these examples as well alright so that's airy merge and the last the number 10 top PHP function is STR pause and this one is to find the position of the first occurrence of a sub string and a string so this one can also be very useful when working with strings and manipulating text so you you're able to find the the first position our occurrence of the string in the text and then it returns the position in an integer so find the numeric position at the first occurrence of needle-in-a-haystack string all right let's give you an example okay so if we wanted to search for orange or let's do black inside of our text I'm gonna make sure it's a string just gonna delete this part here all right let's see what happens so you should search for the black and return STR pause black text haystack you know okay so the haystack is the first one I made a mistake here so first we specify the text that we're searching and then we specify the needle which is the search value okay now let's see what happens okay so the position of black is 13 I'm not sure if it counts goes from 0 or 1 but 1 2 3 4 5 6 7 8 9 10 11 12 okay so it counts from 0 and the position of the first occurrence of black is 13 okay so that's STR pause and just something to keep in mind if needle is not a string it isn't converted to an integer and apply it as an ordinal value of a character all right so they gave some examples here but it's really useful when searching and making sure if the valley is found inside the text so this is what they're showing here so if the valley is found then you can do one thing and if the valley is not found you can do another thing and by using this function in particular you can know the position of the text as well so then you can you can replace it or do whatever you want to do with it and and it's really useful when working with text all right guys so this is the top 10 PHP functions in the next videos I'm going to be covering the rest of the functions and I'm gonna be covering them 10 at a time so the next video I'm gonna cover pragma sprint F trims STR to lower file exists is string preg replace file get contents array key exists and array keys and I'm gonna keep going and covering all these functions because I really think it's useful to know top 100 PHP functions and that will really set you up and get you going with PHP so that so that you really know what these functions do and how they work and since they are the most they're the top functions that are most frequently used it's gonna be super handy and useful to know all hundred PHP functions alright guys so I'll see you next time and I hope you like this video if you did please like share and subscribe and I'll see you next time clever take it
Info
Channel: Clever Techie
Views: 39,194
Rating: undefined out of 5
Keywords: php, php functions, php function, top php functions, top 10 php functions, clever techie, learn php, php programming
Id: t9FrpTZm1ds
Channel Id: undefined
Length: 32min 14sec (1934 seconds)
Published: Thu Oct 18 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.