Php, mysqlpart2

1,023 views

Published on

php mysql training(string functions)

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,023
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
29
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Php, mysqlpart2

  1. 1. SUBHASIS NAYAK CMC Php MySql part -2
  2. 2. <ul><li>STRING & STRING FUNCTIONS </li></ul><ul><li>ARRAYS </li></ul>Content
  3. 3. String <ul><li>A string is a collection of characters that is treated as a single entity. </li></ul><ul><li>In PHP strings are enclosed in quotation marks. </li></ul><ul><li>We can declare a string type variable by assigning it a string that is contained in </li></ul><ul><ul><li>Single quote </li></ul></ul><ul><ul><li>Double quote </li></ul></ul>
  4. 4. Example Both are same. $myString =“HI Good Morning”; $myString = ‘HI good Morning’;
  5. 5. Escaping Characters <ul><li>We can put double quote within single quote and single quote within double quote . as follows </li></ul>$myString =“HI ‘Good Morning’ ”; $myString = ‘HI “Good Morning” ’;
  6. 6. Cont’d ….. <ul><li>However, if we want to use the same character within a quoted string, we must escape that quote by using a backslash as follows: </li></ul>It is depend upon us which one we want to use double quote or Single quote $myString =“HI /”Good Morning/” ”; $myString = ‘HI /’Good Morning/’ ’;
  7. 7. Variables in quote <ul><li>variable prefixed with a dollar sign inside a double-quoted string is replaced with its value but not in single quote. </li></ul><ul><li>But in a single-quoted string, the dollar sign and variable name will remain as it is. </li></ul><ul><li>If we want dollar sign to form part of a double-quoted string, we can also escape this by using a backslash. </li></ul>$myString =“HI $myName ”; $myString = ‘HI $myName ’;
  8. 8. concatenation <ul><li>Strings can be joined using the period symbol as a concatenation operator. </li></ul><ul><li>A compound version of this operator, .=, can be used to append a string to an existing variable. </li></ul>$phrase = “I want “; $phrase .= “to teach “;
  9. 9. Strings comparision <ul><li>We can compare string values simply by using the standard comparison operators. </li></ul>$strN =“Oye Oye”; if($strN == “Hulala”){ echo “It’s cool”; } else{}
  10. 10. String Formatting <ul><li>We can format the string using two powerful function. </li></ul><ul><ul><li>printf </li></ul></ul><ul><ul><li>Sprintf </li></ul></ul>
  11. 11. Printf <ul><li>printf to display a formatted string. At its very simplest, printf takes a single string argument and behaves the same as echo. </li></ul><ul><li>The power of printf, however, lies in its ability to substitute values into placeholders in a string. </li></ul>$price = 5.99; printf(“The price is %f”, $price); The price is 5.99
  12. 12. Printf format cahacter
  13. 13. Format Codes <ul><li>A format specifier can also include optional elements to specify the padding, alignment, width, and precision of the value to be displayed. </li></ul>Refer some books for format codes
  14. 14. sprintf <ul><li>The sprintf function is used to assign formatted strings to variables rather than displaying it. </li></ul><ul><li>Syntax is the same as for printf. </li></ul>$price = 5.99; $str = sprintf(“The price is %f”, $price); echo $str;
  15. 15. Capitalization <ul><li>You can switch the capitalization of a string to all uppercase or all lower- case by using </li></ul><ul><ul><li>strtoupper – to upper </li></ul></ul><ul><ul><li>Strtolower – to lower </li></ul></ul>$str = “I love PHP”; echo strtoupper($str) . “<br>”; echo strtolower($str) . “<br>”; Out put
  16. 16. <ul><li>Functions capitalize only the first character of a string is </li></ul><ul><ul><li>ucfirst() </li></ul></ul>$phrase = “welcome to the jungle”; echo $ucfirst($phrase); Out put
  17. 17. Dissecting a String <ul><li>substr function allows you to extract a substring by specifying a start position within the string and a length argument. </li></ul>$phrase = “I love PHP”; echo substr($phrase, 3, 5); Out put
  18. 18. Position in string <ul><li>To find the position of a character or a string within another string, you can use strpos. </li></ul>$email = “chris@lightwood.net”; echo strpos($email, “@”);
  19. 19. Extract a portion from string <ul><li>The function strstr extracts a portion of a string from the position at which a character or string appears up to the end of the string. </li></ul><ul><li>This is a convenience function that saves your using a combination of strpos and substr . </li></ul>$domain = strstr($email, “@”); $domain = strstr($email, strpos($email, “@”)); Out will be same
  20. 20. Arrays. <ul><li>An array is a variable type that can store and index a set of values. </li></ul><ul><li>An array is useful when the data we want to store </li></ul><ul><ul><li>has something in common </li></ul></ul><ul><ul><li>logically grouped into a set. </li></ul></ul>
  21. 21. Creating arrays <ul><li>The following PHP statement declares an array called $temps and assigns it 12 values that represent the temperatures for January through December: </li></ul><ul><li>To reference an indexed value from an array, you suffix the variable name with the index key. </li></ul>$monthTemps = array(38, 40, 49, 60, 70, 79, 84, 83, 76, 65, 54, 42); To know march temp = $temp[2] ;
  22. 22. To display content of arrays <ul><li>function, print_r, that can be used to recursively output all the values stored in an array. </li></ul>
  23. 23. Looping Through an Array <ul><li>We can easily replicate the way print_r loops through every element in an array by using a loop construct to perform another action for each value in the array. </li></ul><ul><li>By using a while loop, you can find all the index keys and their val from an array—similar to using the print_r function—as follows: </li></ul>
  24. 24. Cont’d …..
  25. 25. Associative Arrays <ul><li>An associative array allows you to use textual keys so that the indexes can be more descriptive. </li></ul><ul><li>To assign a value to an array by using an associative key and to reference that value, you simply use a textual key name enclosed in quotes. </li></ul><ul><li>To define the complete array of average monthly temperatures </li></ul>
  26. 26. Lab question <ul><li>Write a program to compare two strings and display something when test is true. </li></ul><ul><li>Do complex comparison in which you will check two strings with string “xyzabc” </li></ul><ul><ul><li>If both true display something </li></ul></ul><ul><ul><li>Else display something. </li></ul></ul><ul><li>Write a a program using array to find average and total of all elements. </li></ul><ul><li>Write a program to compare each array. </li></ul><ul><ul><li>If odd display odd </li></ul></ul><ul><ul><li>If even display even </li></ul></ul><ul><li>Write a program using associative array and display the elements. </li></ul>
  27. 27. <ul><li>TO TEST ALL SKILLS WE LEARNED JUST NOW. </li></ul>Let’s go to lab

×