Strings TYBSC Comp Sci. Chapter 2- Functions & Strings monica deshmane(H.V.Desai college) 1
Points to learn • String • Types of strings in PHP • Printing functions • Basic functions of string monica deshmane(H.V.Desai college) 2
String types • 1. Single quoted Strings ‘’ Ex. $str=‘hi’; $str1=‘nhi’ echo “$str1”; • 2. Double quoted String “ ” Strings interpolate & expand by escape sequences. • Variable Interpolation- Interpolation is replacing variable names in string with values of those variables. Ex.$str1=“nhi” echo “$str1”; monica deshmane(H.V.Desai college) 3
String types • 3. Here Documents(heredoc) heredoc allows multiline String. <<< identifier is used in php for displaying string using heredoc. <?php $str = <<< my_doc ‘This is an example. This is used for multiline.’ my_doc; echo $str; ?> //may use ‘ or “ or no quotes. • 4. New Documents(Newdoc) this allows multiline String same as heredoc just put name in single quotes. Ex. <?php $str = <<< ‘my_doc’…. monica deshmane(H.V.Desai college) 4
Printing Strings 1. Echo – This allows to print many values at a time. echo "Hello Abcd"; echo ("Hello Abcd"); echo "Hello Abcd", "How are u?"; //echo ("Hello Abcd", "How are u?"); - not allowed 2. print() This function returns true or false boolean value. if (print(“Hello PHP”)) { echo “String is displayed successfully”; } monica deshmane(H.V.Desai college) 5
Printing Strings 3. printf() - function used to print formatted String output. $str = “Abc”; $age=20.5; printf(“%s is %d years old”, $str, $age); 4.print_r() – function displays what is passed to it, It will not convert everything into string as echo and print(). $Student = array(‘name’ => ‘A’, ‘RollNo’ => 100, ‘Class’ => ‘TY’); print_r($Student); Output: Array ( [name] => A [RollNo] => 100 [Class] => TY ) monica deshmane(H.V.Desai college) 6
Printing Strings 5. var_dump() – It is preferred over print_r() for debugging. This function displays PHP value in human readable format. Eg: <?php $b = 3.1; $c = true; var_dump($b, $c); ?> Output: float(3.1) bool(true) monica deshmane(H.V.Desai college) 7
Printing Strings var_dump(array('name' => John, 'age' => 35)); Output: array(2) { ["name"]=> string(4) “John" ["age"]=> int(35) } 6.sprintf() To print string with format Ex. $str=sprintf(“hi %s is %s”,”this “,”php”); Echo $str; monica deshmane(H.V.Desai college) 8
revise • Printing functions 1. Echo 2. Print 3. printf 4. Sprintf 5. Print_r 6. Var_dump monica deshmane(H.V.Desai college) 9
Calculating length of String strlen() What we require to compute length of string? Syntax: count= strlen(String variable); Example: $len = strlen($str); monica deshmane(H.V.Desai college) 10
Calculating length of String $str = “Hello, world”; $len = strlen($str); monica deshmane(H.V.Desai college) 11 Accessing Individual Characters $str = “Welcome”; for ($i=0; $i < strlen($str); $i++) { printf(“%d is %s”,$i, $str{$i}); }
Removing Whitespaces trim() $str1 = ltrim($title); //leading spaces $str2 = rtrim($title); //trailing spaces $str3 = trim($title);//all spaces monica deshmane(H.V.Desai college) 12
Removing Whitespaces $title = “ Welcome “; $str1 = ltrim($title); //leading spaces $str2 = rtrim($title); //trailing spaces $str3 = trim($title); //all spaces echo “$str1 $str2 $str3”; monica deshmane(H.V.Desai college) 13
Changing Case strtolower($title)); strtoupper($title)); ucfirst($title)); ucwords($title)); monica deshmane(H.V.Desai college) 14
Changing Case $title = “hELLO welcome“; printf(strtolower($title)); //hello welcome printf(strtoupper($title)); //HELLO WELCOME printf(ucfirst($title)); //HELLO welcome printf(ucwords($title)); //HELLO Welcome Note: ucfirst() works on first character of whole string and ucwords works on first character of each words in the string to convert it in uppercase. monica deshmane(H.V.Desai college) 15
End of Presentation monica deshmane(H.V.Desai college) 16

PHP string-part 1

  • 1.
    Strings TYBSC Comp Sci. Chapter2- Functions & Strings monica deshmane(H.V.Desai college) 1
  • 2.
    Points to learn •String • Types of strings in PHP • Printing functions • Basic functions of string monica deshmane(H.V.Desai college) 2
  • 3.
    String types • 1.Single quoted Strings ‘’ Ex. $str=‘hi’; $str1=‘nhi’ echo “$str1”; • 2. Double quoted String “ ” Strings interpolate & expand by escape sequences. • Variable Interpolation- Interpolation is replacing variable names in string with values of those variables. Ex.$str1=“nhi” echo “$str1”; monica deshmane(H.V.Desai college) 3
  • 4.
    String types • 3.Here Documents(heredoc) heredoc allows multiline String. <<< identifier is used in php for displaying string using heredoc. <?php $str = <<< my_doc ‘This is an example. This is used for multiline.’ my_doc; echo $str; ?> //may use ‘ or “ or no quotes. • 4. New Documents(Newdoc) this allows multiline String same as heredoc just put name in single quotes. Ex. <?php $str = <<< ‘my_doc’…. monica deshmane(H.V.Desai college) 4
  • 5.
    Printing Strings 1. Echo– This allows to print many values at a time. echo "Hello Abcd"; echo ("Hello Abcd"); echo "Hello Abcd", "How are u?"; //echo ("Hello Abcd", "How are u?"); - not allowed 2. print() This function returns true or false boolean value. if (print(“Hello PHP”)) { echo “String is displayed successfully”; } monica deshmane(H.V.Desai college) 5
  • 6.
    Printing Strings 3. printf()- function used to print formatted String output. $str = “Abc”; $age=20.5; printf(“%s is %d years old”, $str, $age); 4.print_r() – function displays what is passed to it, It will not convert everything into string as echo and print(). $Student = array(‘name’ => ‘A’, ‘RollNo’ => 100, ‘Class’ => ‘TY’); print_r($Student); Output: Array ( [name] => A [RollNo] => 100 [Class] => TY ) monica deshmane(H.V.Desai college) 6
  • 7.
    Printing Strings 5. var_dump()– It is preferred over print_r() for debugging. This function displays PHP value in human readable format. Eg: <?php $b = 3.1; $c = true; var_dump($b, $c); ?> Output: float(3.1) bool(true) monica deshmane(H.V.Desai college) 7
  • 8.
    Printing Strings var_dump(array('name' =>John, 'age' => 35)); Output: array(2) { ["name"]=> string(4) “John" ["age"]=> int(35) } 6.sprintf() To print string with format Ex. $str=sprintf(“hi %s is %s”,”this “,”php”); Echo $str; monica deshmane(H.V.Desai college) 8
  • 9.
    revise • Printing functions 1.Echo 2. Print 3. printf 4. Sprintf 5. Print_r 6. Var_dump monica deshmane(H.V.Desai college) 9
  • 10.
    Calculating length ofString strlen() What we require to compute length of string? Syntax: count= strlen(String variable); Example: $len = strlen($str); monica deshmane(H.V.Desai college) 10
  • 11.
    Calculating length ofString $str = “Hello, world”; $len = strlen($str); monica deshmane(H.V.Desai college) 11 Accessing Individual Characters $str = “Welcome”; for ($i=0; $i < strlen($str); $i++) { printf(“%d is %s”,$i, $str{$i}); }
  • 12.
    Removing Whitespaces trim() $str1 =ltrim($title); //leading spaces $str2 = rtrim($title); //trailing spaces $str3 = trim($title);//all spaces monica deshmane(H.V.Desai college) 12
  • 13.
    Removing Whitespaces $title =“ Welcome “; $str1 = ltrim($title); //leading spaces $str2 = rtrim($title); //trailing spaces $str3 = trim($title); //all spaces echo “$str1 $str2 $str3”; monica deshmane(H.V.Desai college) 13
  • 14.
  • 15.
    Changing Case $title =“hELLO welcome“; printf(strtolower($title)); //hello welcome printf(strtoupper($title)); //HELLO WELCOME printf(ucfirst($title)); //HELLO welcome printf(ucwords($title)); //HELLO Welcome Note: ucfirst() works on first character of whole string and ucwords works on first character of each words in the string to convert it in uppercase. monica deshmane(H.V.Desai college) 15
  • 16.
    End of Presentation monicadeshmane(H.V.Desai college) 16