Tuesday, October 22, 2013

Basic string functions

Hey!, now lets try learning a little bit more about “Strings” in PHP, as you may or may not know, Strings are variables used to store “text” data, however PHP have some functions that allow us to manipulate this variables to acomplish certain tasks. Lets see some examples with commonly used functions to handle strings.

Common functions to manipulate Strings

Most commonly used functions may includo explode(), implode(), strlen() or strpos(), how ever on this file I’ll leave you simple explanations for strlen and strpos, which are really basic functions to learn, remember that handling strings correclty, will always come in handy when developing programs, so that’s a pretty good reason to learn how to use them.
the strlen() function
This function returns back the size of a String, the size is the same number of characters included in the string variables, here is an example.
1<?php
2$four = "four";
3//declare a string variable that has 4 characters
4
5echo strlen($four);
6//print the number '4' as it is the number of characters in the string variable
7
8?>

the strpos() function
This functions returns the index position of an unique specified character or phrase, that is contained within any string variable, for example look at this code.
01<?php
02
03$text = "hey everyone! this is my code!";
04//create a string variable
05
06$position = strpos($text,'everyone');
07//get the position of 'everyone'
08
09echo $position;
10//print the result which is 4, wich is the index location of the first 'e' in 'everyone'
11
12//remember that the first index is always 0
13?>

Video tutorial for basic String functions


That’s all for now guys, thanks for cheking out the post, see ya later!!
Don’t forget to comment! :)