SlideShare a Scribd company logo
1 of 16
Instruction By:

          Dr. Bou
eun   Chhun             1
Content:

1. String Variable
2. Concatenate Operators
3. String with Single and Double
Quote
4. String with heredoc
5. The PHP strlen() function
6. The PHP strpos() function
7. The str_replace function
8. The strtoupper() function
9. The ucfirst() function
10. The trim() function            2
11. The strtolower() function
String Variable
        String variables are used for values that
         contain characters.
        After we have created a string variable we
         can manipulate it.
        A string can be used directly in a function
         or it can be stored in a variable.
        Here is PHP script with string value
             <?php
                    $str=‚This is String in PHP.‛;
                    echo $str;
             ?>
        Output of the above script are:
             This is string in PHP                     3
The PHP Concatenation Operator

        There is only one string operator in PHP.
        The concatenation operator (.) is used to
         join two string values together.
        Below is the example of string
         concatenation
             <?php
                   $txt1="Hello world!";
                   $txt2="What a nice day!";
                   echo $txt1 . " " . $txt2;
             ?>
        The result of the above
             Hello world What a nice day!
                                                     4
String with Single and Double
quote
    There are two way when you can specify a string in
     php single quote and double quote.
    Single quoted strings are treated almost
     literally.
    double quoted strings replace variables with their
     values as well as specially interpreting certain
     character sequences.
    Here is an example
         <?php
               $variable=‚name‛;
               $literally=‘My $variable will not print
   n’;
               print($literally);
               $literally=‚My $variable will print n‛;
                                                          5
               print($literally);
 The result of the above code will be
       My $variable will not print!n
       My name will print
 String with double quote will process in both two way:
       1. Certain character sequences beginning with backslash
          () are replaced with special characters
       2. Variable names (starting with $) are replaced with string
                 representations of their values.
 The escape-sequence replacements are:
       n is replaced by the newline character
       r is replaced by the carriage-return character
       t is replaced by the tab character
       $ is replaced by the dollar sign itself ($)
       " is replaced by a single double-quote (")
        is replaced by a single backslash ()                       6
String with Heredoc
   You can assign multiple lines to a single string
    variable using heredoc .
   See the example below

       <?php
             echo <<<END
             This uses the "here document" syntax to
       output
             multiple lines with variable
       interpolation. Note
             that the here document terminator must
       appear       on a
             line with just a semicolon. no extra
       whitespace!                                     7
             <br />
 In code above after heredoc must be follow by
  some identifier for example END and must be
  end with the same identifier as the start one
  with semiconlon sign
 Even though using heredoc is difficult and can
  Couse some problem.
 Note that the closing sequence END; must occur
  on a line by itself and cannot be indented.




                                                   8
The PHP strlen() Function

    Strlen() function used to return the length of
     the string
          example:
                <?php
                         $str=‚Welcome back to PHP‛;
                         echo strlen($str);
                ?>
    Result will be
                19
    The length of a string is often used in loops or other
     functions, when it is important to know when the
     string ends. (i.e. in a loop, we would want to stop      9
     the loop after the last character in the string).
The strops() Function

     Strpos() function is used to return the search
      of character with in the string.
     If match is found, function will return the
      position of the first character of the match
      word.
          example:
                <?php
                      echo strpos(‛Welcome back to
                                  PHP‛,‛PHP‛);
                ?>
     The result of the above script is follow
                16
     The resean is that it return only the first
                                                       10
       character of the whole word .
The str_replace function

    The str_replace function is use to search and
     replace the search result with the new word.
    If no word match it will return the original
     word.
          example
               <?php
                     $str=‚Welcome back to PHP.‛;
                     echo str_replace(‚PHP‛,‛PHP
                           Function‛,$str);
               ?>
    This will search for the word PHP and replace
      with the word PHP Function.
    The result is:                                  11
                     Welcome back to PHP Function.
The strtoupper() Function

 The strtoupper() function is used to convert all
  lowercase of the string word or sentence to
  uppercase.
      example
            <?php
                  $str=‚start your first php with
w3school.‛;
                  echo strtoupper($str);
            ?>
 Here is the result
                  START YOUR FIRST PHP WITH
            W3SCHOOL.
                                                     12
The strtolower() Function

     The strtolower() function is used to convert all
      uppercase string to lowercase.
          example
                <?php
                      $str=‚START YOUR FIRST PHP WITH
                      W3SCHOOL.‛;
                      echo strtolower($str);
                ?>
     Here is the result
                start your first php with w3school.


                                                         13
The ucfirst() function

    Ucfirst() function used to capital the first letter of
     the first word in the whole string.
         example
                <?php
                      $str=‚using ucfirst() function.‛;
                      echo($str);
                ?>
    The result is
                Using ucfirst() function.


                                                              14
The trim() function
     Trim() function strip a string of white space in
      front and at the end of the whole string.
          example
                <?php
                      $str=‚ Welcome! ‛;
                      echo($str);
                ?>
     The above will result as below
                      Welcome!




                                                         15
Referent:

1. http://www.w3schools.com/php/php_string.asp
2. http://www.tutorialspoint.com/php/php_variable_types.htm




                                                              16

More Related Content

What's hot (16)

Subroutines
SubroutinesSubroutines
Subroutines
 
Perl Scripting
Perl ScriptingPerl Scripting
Perl Scripting
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
Lecture 22
Lecture 22Lecture 22
Lecture 22
 
Php basics
Php basicsPhp basics
Php basics
 
Achieving Parsing Sanity In Erlang
Achieving Parsing Sanity In ErlangAchieving Parsing Sanity In Erlang
Achieving Parsing Sanity In Erlang
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 Training
 
Wx::Perl::Smart
Wx::Perl::SmartWx::Perl::Smart
Wx::Perl::Smart
 
Perl programming language
Perl programming languagePerl programming language
Perl programming language
 
Unit vii wp ppt
Unit vii wp pptUnit vii wp ppt
Unit vii wp ppt
 
1 the ruby way
1   the ruby way1   the ruby way
1 the ruby way
 
php string part 4
php string part 4php string part 4
php string part 4
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
 
Improving Dev Assistant
Improving Dev AssistantImproving Dev Assistant
Improving Dev Assistant
 
Perl
PerlPerl
Perl
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
 

Similar to String variable in php

Similar to String variable in php (20)

UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
 
UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeString handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
 
php_string.pdf
php_string.pdfphp_string.pdf
php_string.pdf
 
PHP MATERIAL
PHP MATERIALPHP MATERIAL
PHP MATERIAL
 
Php
PhpPhp
Php
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kz
 
Free PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in IndiaFree PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in India
 
PHP - Web Development
PHP - Web DevelopmentPHP - Web Development
PHP - Web Development
 
Php Learning show
Php Learning showPhp Learning show
Php Learning show
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 
Manipulating strings
Manipulating stringsManipulating strings
Manipulating strings
 
OpenGurukul : Language : PHP
OpenGurukul : Language : PHPOpenGurukul : Language : PHP
OpenGurukul : Language : PHP
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
 
Web Technology_10.ppt
Web Technology_10.pptWeb Technology_10.ppt
Web Technology_10.ppt
 
php basics
php basicsphp basics
php basics
 
lab4_php
lab4_phplab4_php
lab4_php
 
lab4_php
lab4_phplab4_php
lab4_php
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 

Recently uploaded

Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 

Recently uploaded (20)

Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 

String variable in php

  • 1. Instruction By: Dr. Bou eun Chhun 1
  • 2. Content: 1. String Variable 2. Concatenate Operators 3. String with Single and Double Quote 4. String with heredoc 5. The PHP strlen() function 6. The PHP strpos() function 7. The str_replace function 8. The strtoupper() function 9. The ucfirst() function 10. The trim() function 2 11. The strtolower() function
  • 3. String Variable  String variables are used for values that contain characters.  After we have created a string variable we can manipulate it.  A string can be used directly in a function or it can be stored in a variable.  Here is PHP script with string value <?php $str=‚This is String in PHP.‛; echo $str; ?>  Output of the above script are: This is string in PHP 3
  • 4. The PHP Concatenation Operator  There is only one string operator in PHP.  The concatenation operator (.) is used to join two string values together.  Below is the example of string concatenation <?php $txt1="Hello world!"; $txt2="What a nice day!"; echo $txt1 . " " . $txt2; ?>  The result of the above Hello world What a nice day! 4
  • 5. String with Single and Double quote  There are two way when you can specify a string in php single quote and double quote.  Single quoted strings are treated almost literally.  double quoted strings replace variables with their values as well as specially interpreting certain character sequences.  Here is an example <?php $variable=‚name‛; $literally=‘My $variable will not print n’; print($literally); $literally=‚My $variable will print n‛; 5 print($literally);
  • 6.  The result of the above code will be My $variable will not print!n My name will print  String with double quote will process in both two way: 1. Certain character sequences beginning with backslash () are replaced with special characters 2. Variable names (starting with $) are replaced with string representations of their values.  The escape-sequence replacements are: n is replaced by the newline character r is replaced by the carriage-return character t is replaced by the tab character $ is replaced by the dollar sign itself ($) " is replaced by a single double-quote (") is replaced by a single backslash () 6
  • 7. String with Heredoc  You can assign multiple lines to a single string variable using heredoc .  See the example below <?php echo <<<END This uses the "here document" syntax to output multiple lines with variable interpolation. Note that the here document terminator must appear on a line with just a semicolon. no extra whitespace! 7 <br />
  • 8.  In code above after heredoc must be follow by some identifier for example END and must be end with the same identifier as the start one with semiconlon sign  Even though using heredoc is difficult and can Couse some problem.  Note that the closing sequence END; must occur on a line by itself and cannot be indented. 8
  • 9. The PHP strlen() Function  Strlen() function used to return the length of the string example: <?php $str=‚Welcome back to PHP‛; echo strlen($str); ?>  Result will be 19  The length of a string is often used in loops or other functions, when it is important to know when the string ends. (i.e. in a loop, we would want to stop 9 the loop after the last character in the string).
  • 10. The strops() Function  Strpos() function is used to return the search of character with in the string.  If match is found, function will return the position of the first character of the match word. example: <?php echo strpos(‛Welcome back to PHP‛,‛PHP‛); ?>  The result of the above script is follow 16  The resean is that it return only the first 10 character of the whole word .
  • 11. The str_replace function  The str_replace function is use to search and replace the search result with the new word.  If no word match it will return the original word. example <?php $str=‚Welcome back to PHP.‛; echo str_replace(‚PHP‛,‛PHP Function‛,$str); ?>  This will search for the word PHP and replace with the word PHP Function.  The result is: 11 Welcome back to PHP Function.
  • 12. The strtoupper() Function  The strtoupper() function is used to convert all lowercase of the string word or sentence to uppercase. example <?php $str=‚start your first php with w3school.‛; echo strtoupper($str); ?>  Here is the result START YOUR FIRST PHP WITH W3SCHOOL. 12
  • 13. The strtolower() Function  The strtolower() function is used to convert all uppercase string to lowercase. example <?php $str=‚START YOUR FIRST PHP WITH W3SCHOOL.‛; echo strtolower($str); ?>  Here is the result start your first php with w3school. 13
  • 14. The ucfirst() function  Ucfirst() function used to capital the first letter of the first word in the whole string. example <?php $str=‚using ucfirst() function.‛; echo($str); ?>  The result is Using ucfirst() function. 14
  • 15. The trim() function  Trim() function strip a string of white space in front and at the end of the whole string. example <?php $str=‚ Welcome! ‛; echo($str); ?>  The above will result as below Welcome! 15