| String Function Upper and Lower Case Conversion in php |
php programming languages. We will also talk about where we will use in real life example let see some example of string functions.
Code For String Function Lower Case (Screenshot1) :-
1
2
3
4
5
6
7
8
| <?php$string = 'I Could Be Any Case.';$string_lower = strtolower($string);echo $string_lower;?> |
Output (Screenshot2):-
I had simple static string like just say I could be any case. This string could be user supply data or static string like just using in example. Now what happen if i create this string completely in lower case. strtolower functions creating new variable $string_lower is equal to strtolower function and with parameters the string which we want to convert $string. Then we output $string_lower and as u can see output starting I is covert into lower case i and also all other starting other words.
Code for string function Upper case (Screenshot3):-
1
2
3
4
5
6
7
8
9
| <?php$string = 'I Could Be Any Case.';$string_lower = strtolower($string);$string_upper = strtoupper($string);echo $string_upper;?> |
Output (Screenshot4):-
In this example we are converting whole string to upper case. as you see output. So, Its so simple to upper case to lower case and from lower case to upper.
Now Why we want to use this functions inside php? Let see example for this.
Code for string function (Screenshot5):-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| <?phpif (isset($_GET['user_name'])&&(!empty($_GET['user_name'])) {$user_name = $_GET['user_name'];$user_name_lc = strtolower($user_name);if ($user_name_lc=='alex') {echo 'You are the best, '. $user_name;}}?><form action="index.php" method="GET">Name: <input type="text" name="user_name"><br /><input type="Submit" value="Submit"></form> |
output (Screenshot6,Screenshot7, Screenshot8):-
In this example i had created form with action and method and taking one line of user data like name with input type will be text and name will be of the text-box user_name and add submit button. I want to perform if statement on username just take example if we want to compare values of user and static value user must type in upper case. So convert every word of user data and compare the value to the static value. The user data must be Alex or aLeX or aleX to compare with alex in lower case you had to convert all word to lower case. You don’t know how people are going to type there name. The reason i am using get method because i can keep the eye on address bar. if text-box will not empty and this two conditions of if statement is true we will store value $user_name. If you will declare variable outside $user_name = $GET['user_name']; we will have properly undefined index error. So, If you get any undefined index error it is because you had defined variable which may not exits before you do checks. Now we had defined static value from user data is alex so, if value of user data is alex echo you are the best but if user but different values such as Alex or aLeX or aleX so, we will convert string to lower case. See different type of results.
Read More Extra Tutorials
No comments:
Post a Comment