5 February 06
Beginner PHP Tutorial 6: Useful Functions
If you really want to delve into the php language properly, you should have a look at the PHP Manual since it has full function lists and it describes some extensions and how to use them as well. However, in this tutorial I will show you what functions may come in handy and the ones you will probably use the most.
When learning php you may come across multiple functions that are the same, or almost the same (like print() and echo()). In this situation choose the function you prefer, unless the other function does the job better.
String Functions
- int substr_count(string haystack, string needle [, int offset [, int length]]) – this is a really simple function, it simply finds how many times the needle occurs in the haystack. You can also set an offset, which starts the search in the haystack later by how much you enter. Finally, the length parameter lets you limit the amount of haystack to be searched.
- string substr ( string string, int start [, int length] ) – another simple function, this gets the string and cuts bits off so you can get the bit you want. The start parameter is where your new string starts. You can also set how long it is, or you can leave it and the string will go right up to the end of the original string.
- mixed str_replace ( mixed search, mixed replace, mixed subject [, int &count] ) – the parameters search and replace can be arrays or strings (one can be an array while one is a string). Basically, it takes the subject parameter and goes through it replacing search with replace. You can also set a final parameter while places the amount of finds into a variable. However, this is only available in PHP 5 so I don’t recommend you use this until it is more common.
- string strtoupper ( string string ) – this returns the parameter string, with all characters upper case.
- string strtolower ( string str ) – simply the opposite of the above function, it moves str to lower case.
- string strrev ( string string ) – reverses the parameter string
- int strlen ( string string ) – returns the length of string
- int ord ( string string ) – returns ASCII value of character. If you don’t know what ASCII is, don’t worry.
- string chr ( int ascii ) – returns character from ASCII value.
- array explode ( string separator, string string [, int limit] ) – splits string up where it finds separator. So Test+String could be exploded by the plus sign to get Test and String in an array. The final parameter limits the amount of values allowed in the array. The final value will have the rest of the string, if limit is given.
- string implode ( string glue, array pieces ) – the opposite of explode(), this function gets the values in the array pieces and puts them together putting the parameter “glue” between them.
For strings there is a full list on the PHP.net Site
Variable Handling Functions
- bool empty ( mixed var ) – returns true if var is considered to be empty, false if it isn’t.
- bool is_array ( mixed var ) – returns whether var is an array or not.
- bool is_bool ( mixed var ) – returns whether var is a boolean (true/false) or not.
- bool is_int ( mixed var ) – returns whether var is an integer.
- bool is_numeric ( mixed var ) – determines whether var is a number, or a string which actually is a number (from forms).
- bool is_string ( mixed var ) – returns whether var is a string or not.
- bool isset ( mixed var [, mixed var [, ...]] ) – determines if var has been set. If multiple var’s are given, it makes sure every one is set to return true, otherwise it returns false.
- bool print_r ( mixed expression [, bool return] ) – helpful function which prints a variable on the page in human readable form. If the return parameter is set to true, however, the output is returned and not printed on the screen.
- string serialize ( mixed value ) – generates a storable set of characters which represent the only parameter. This is useful when storing arrays and even objects in the database.
- mixed unserialize ( string str ) – opposite of the previous function, this returns the original variable.
- void unset ( mixed var [, mixed var [, mixed …]] ) – removes the variables passed to it. Once variables have been unset the isset function will return false. You can set variables again using the normal variable = value form.
I don’t want to spend the rest of this tutorial explaining every single function, but I hope this gives you an idea. Here are some good php.net function lists:
- Variable Handling
- Date and Time
- URL Functions
- Arrays
- HTTP
- Miscellaneous
- Math
- Function Handling
- PHP Options/Info
- Strings
- Program Execution
Make sure you get to grips with the functions, especially ones you think you will use a lot. 5 hours reading the manual can only be a good thing.

