I have just finished watching Amitabh Bachchan’s 3 hr film- ‘Muqaddar ka Sikander’. It has some beautiful songs. But you know, the whole film was like full of dialogues, statements and even songs too were statements i.e everything is in STRING. Thus, STRINGs are of great use everywhere. The data submitted by user is mostly in strings and we’ll have to operate upon those strings. So for this, we have some predefined functions and PHP offers them free of cost. 🙂
Before moving to functions let’s work on some basic tips.
- To print a statement in PHP, we use
echo. Right. We know this. But there is a difference in printing any statement using double quotes and printing a statement using single quotes. Let me show you-
<?php
$ques1 = "Who will be the next IMF Chief?";
$ques2 = "Who will now head Bank of England?" ;
echo 'My first question is:: $ques2'. "<br>";
echo "My second question is:: $ques1" ;
?>
In line 5th I have used single quotes so in its output the value of $ques2 will not be replaced. Whereas in line 6th, $ques1 will be replaced by its value. So the output will be-
My first question is:: $quest2
My second question is:: Who will be the next IMF Chief?Got it ! So its better to use double quotes.
You must have learnt of escape sequences earlier. Let me just brief it. Escape sequences begin with ‘\’ and are used to give some special meaning to a character (not all characters) like- ‘\n'(to transfer control to new line) or ‘\t'(tab space) or ‘\f'(formfeed) or ‘\a'(a beep sound), you must have studied all this.
Apart from this, escape sequences are also used to remove the speciality of special characters. When we print some statement using echo, we use double quotes most of the time. Suppose we want to print double quotes, then? Look at following code-
<?php
$ques1 = "Who will be the next IMF Chief?";
echo "My first question is:: \"$ques1\" " ;
?>
In above code, double quotes are special character because they represent string. That’s why inside echo–
- I have first used double quotes- to tell PHP that we are printing some string
- then I have written my string “My first string is::”
- then look carefully, I have used – \” . This is done to tell PHP that it should neglect the meaning of double quotes, which is to begin or end the string. So when its actual meaning is neglected, double quotes gets printed just as any normal character.
- So in order to print double or single quotes, simply use \ . Try the code by yourself, make some changes and analyze the result by yourself.
- We can even print this \ by writing –
echo "Today Indians should be \\proud\\ of their scientists at \" ISRO \" ". Please run this and you’ll understand the things in a better way.
Now, coming to String Functions–
- strlen( $str ) – tells the length of any string
- strcmp( $str1, $str2) – returns 0 if both strings are equal, returns -1 if str2 is greater than str1, returns 1 if str2 is smaller than str1 . The point to remember her is it is case sensitive.
- strcasecmp($str1, $str2) – it is case- insensitive, and returns the same output as strcmp().
- strstr($haystack, $needle) – here $haystack and $needle are not any ‘bhoot’ :). These are simple parameters. First look at this code-
<?php
$ques1 = "my name is khan, and yours?";
$ques2 = "is";
echo strstr($ques1,$ques2);
?>
Please, run this code first and then read my explanation. $haystack is a string in which PHP finds $needle (it is also a string) and the from the position where it finds $needle, it prints the complete $haystack till last. I have replaced $haystack with $ques1 and $needle with $ques2 $haystack and $needle are terms given by PHP. You may completely ignore these complex-looking terms.
- strpos( $haystack, $needle, [offset]) – it tells the index number of $needle in $haystack. Remember that index number starts with 0. Here offset is an optional parameter, it gives the position from where the $needle is searched in $haystack.
- str_replace($search, $replace, $subject, [$count]) – the term $search which will be replaced by $replace in the sentence $subject. Here too, $count is optional and it returns the total number of items replaced.
- ucfirst($str) – it is used to capitalize first character of $str.
- ucwords($str) – used to capitalize first character of each word in $str.
- strtoupper($str) – used to convert $str to uppercase.
- strtolower($str) – used to convert $str to lowercase
- str_shuffle( $str) – shuffles the string $str
- explode($delimiter, $str, [int $limit]) – this performs fantastic job. First look at code and then move on explanation.
<?php
$str = "India should improve its primary education first to aim for New India";
var_dump(explode('i', $str));
?>
In above code, ‘i’ is $delimiter which never gets printed and breaks the string from the place where this ‘i’ is present. When the string is broken its various elements gets stored as an array. So when you will run above code, you will get an array. The third parameter $limit is optional, it states the number of elements in a broken. If you will pass $limit as 2, then there will be only two elements in array. Test it Yourself, it will be more beneficial…
- implode($glue, $array) – this functions joins all elements of $array with $glue in between. Not got it . Superb ! Look at code below-
<?php
$str = "India should improve its primary education first to aim for New India";
$arr = (explode('i', $str));
echo implode(" ", $arr);
?>
In above code the elements are first separated using explode (it is same as we did in previous code) and then i have stored those elements in $arr and then joined them using implode(). Inside implode(), I have passed space ” ” as $glue and $arr as $array to join the elements with space between them. You run the code and you will understand.
- str_split($original_str, [$splitting_length]) – this splits the $original_string into $splitting_length size elements. If you have given $splitting_length as 3, then each element of $original_string will be split into elements each of size 3.Try it yourself, its not at all complex.
- md5($str) – it is a globally used function for security of data. It stands for- Message Digest Algorithm 5. It is used to encrypt $str. The output will always be a 128-bit value irrespective of $str.
That’s it for PHP functions. And this is not the complete list of functions there are many more. These, that I have mentioned, are the most used ones.
Have ERROR FREE coding ! You can now go out and play… 🙂
