Functions in Python

Functions provide code re-usability i.e. once a function is written can be invoked anytime upon need.

They have one disadvantage too. They take more time to implement because the control has to jump from normal code to the functions and then returns back to the calling environment. This is known as Context Switching.

Information or data is passed to functions through parameters, if needed. Functions can be with or without parameters.

Functions if needed, can return some value or values or even a function. Thus function can have or cannot have return type.

Some Important String Functions in PHP

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… ๐Ÿ™‚

Default value arguments in PHP

Functions can be inbuilt or user-defined, inbuilt functions are ready made ones, these include array(), var_dump(), array_search(), there are many…

When we create functions, they are termed as user- defined functions. In user defined functions we pass some values in it those values are passed as formal parameters inside the function. Let me show you an example-

One thing to note in above code is- we can call our function anywhere in program irrespective of its position of function definition. In program, I have called calci() 2 times and it is valid in PHP.

The variables $a,$b,$c,$d are formal parameters and their scope is limited within function only. They do not exist outside function. While the values passed during calling of function 4,6,8,82 are actual parameters.

Imagine if $a, $b, $c, $d are prices of four items and what if price of $d remains same every time. We can do it in following way-

In above code, I have set $d as default value argument i.e. by default its value will be 82. In case you want to pass your own value, you can do so. In that case 82 will be over-written by your value.

What happens is – the values 4,6,8,82 goes to $a, $b, $c, $d respectively. When we make $d as default argument, the values 4,6,8 goes to remaining three variables.

Suppose we want to make $a and $d both as default value arguments, then ? We all will quickly do this-

function calci($a=4, $b, $c, $d=82)

and in function calling – calci(6,8);

But no, this is absolutely wrong . Because how will PHP know that it has to copy the value 6 to $b. PHP will copy 6 to $a and 8 to $b and then no value for $c, and no value for $d also. But remember that $d is in last and it has its default value, so no matter if $d gets value or not, it has it’s own value. But the problem is for $c. So what to do now? Relax and look at the following code-

What I have done is- I have written all default arguments ($a ,$d) at last. Now, the value 6 is copied to $b, the value 8 is copied to $c and no value is copied to $a and $d. But this is not an error here, because both of these have their default arguments with them.

So the conclusion is always keep default value arguments at the end of the list.

I hope this is clear, if not, then no problem comment your doubt here and it will be resolved and you will feel SUPER GOOD.

Have a ERROR-FREE coding…