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

Php, mysqlpart2

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