SlideShare ist ein Scribd-Unternehmen logo
1 von 16
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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (16)

Optionals Swift - Swift Paris Junior #3
Optionals Swift - Swift Paris Junior #3 Optionals Swift - Swift Paris Junior #3
Optionals Swift - Swift Paris Junior #3
 
perl_lessons
perl_lessonsperl_lessons
perl_lessons
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHP
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Tip Top Typing - A Look at Python Typing
Tip Top Typing - A Look at Python TypingTip Top Typing - A Look at Python Typing
Tip Top Typing - A Look at Python Typing
 
tutorial7
tutorial7tutorial7
tutorial7
 
A quick python_tour
A quick python_tourA quick python_tour
A quick python_tour
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
 
Communities - Perl edition (RioJS)
Communities - Perl edition (RioJS)Communities - Perl edition (RioJS)
Communities - Perl edition (RioJS)
 
PHP - Introduction to String Handling
PHP -  Introduction to  String Handling PHP -  Introduction to  String Handling
PHP - Introduction to String Handling
 
C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
 
Becoming a Pythonist
Becoming a PythonistBecoming a Pythonist
Becoming a Pythonist
 

Ähnlich wie PHP string-part 1

Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
Raghu nath
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
Giovanni924
 

Ähnlich wie PHP string-part 1 (20)

Chap 3php array part 2
Chap 3php array part 2Chap 3php array part 2
Chap 3php array part 2
 
PHP-Overview.ppt
PHP-Overview.pptPHP-Overview.ppt
PHP-Overview.ppt
 
PHP_Lecture.pdf
PHP_Lecture.pdfPHP_Lecture.pdf
PHP_Lecture.pdf
 
PHP-01-Overview.ppt
PHP-01-Overview.pptPHP-01-Overview.ppt
PHP-01-Overview.ppt
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
php_string.pdf
php_string.pdfphp_string.pdf
php_string.pdf
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsYou Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager Needs
 
Achieving Parsing Sanity In Erlang
Achieving Parsing Sanity In ErlangAchieving Parsing Sanity In Erlang
Achieving Parsing Sanity In Erlang
 
First steps in C-Shell
First steps in C-ShellFirst steps in C-Shell
First steps in C-Shell
 
Pharo: Syntax in a Nutshell
Pharo: Syntax in a NutshellPharo: Syntax in a Nutshell
Pharo: Syntax in a Nutshell
 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
 
Php Basics Iterations, looping
Php Basics Iterations, loopingPhp Basics Iterations, looping
Php Basics Iterations, looping
 
Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3
 
Format String
Format StringFormat String
Format String
 
Web 8 | Introduction to PHP
Web 8 | Introduction to PHPWeb 8 | Introduction to PHP
Web 8 | Introduction to PHP
 
PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Ruby, muito mais que reflexivo
Ruby, muito mais que reflexivoRuby, muito mais que reflexivo
Ruby, muito mais que reflexivo
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
 
Chap1introppt2php(finally done)
Chap1introppt2php(finally done)Chap1introppt2php(finally done)
Chap1introppt2php(finally done)
 

Mehr von monikadeshmane (17)

File system node js
File system node jsFile system node js
File system node js
 
Nodejs functions & modules
Nodejs functions & modulesNodejs functions & modules
Nodejs functions & modules
 
Nodejs buffers
Nodejs buffersNodejs buffers
Nodejs buffers
 
Intsllation & 1st program nodejs
Intsllation & 1st program nodejsIntsllation & 1st program nodejs
Intsllation & 1st program nodejs
 
Nodejs basics
Nodejs basicsNodejs basics
Nodejs basics
 
Chap 5 php files part-2
Chap 5 php files   part-2Chap 5 php files   part-2
Chap 5 php files part-2
 
Chap 5 php files part 1
Chap 5 php files part 1Chap 5 php files part 1
Chap 5 php files part 1
 
Chap4 oop class (php) part 2
Chap4 oop class (php) part 2Chap4 oop class (php) part 2
Chap4 oop class (php) part 2
 
Chap4 oop class (php) part 1
Chap4 oop class (php) part 1Chap4 oop class (php) part 1
Chap4 oop class (php) part 1
 
Chap 3php array part4
Chap 3php array part4Chap 3php array part4
Chap 3php array part4
 
Chap 3php array part 3
Chap 3php array part 3Chap 3php array part 3
Chap 3php array part 3
 
Chap 3php array part1
Chap 3php array part1Chap 3php array part1
Chap 3php array part1
 
PHP function
PHP functionPHP function
PHP function
 
php string-part 2
php string-part 2php string-part 2
php string-part 2
 
java script
java scriptjava script
java script
 
ip1clientserver model
 ip1clientserver model ip1clientserver model
ip1clientserver model
 
Chap1introppt1php basic
Chap1introppt1php basicChap1introppt1php basic
Chap1introppt1php basic
 

Kürzlich hochgeladen

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
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
kauryashika82
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 

Kürzlich hochgeladen (20)

Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
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
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
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
 

PHP string-part 1

  • 1. Strings TYBSC Comp Sci. Chapter 2- 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 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
  • 11. 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}); }
  • 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
  • 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 monica deshmane(H.V.Desai college) 16