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

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Recently uploaded (20)

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).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