This document discusses strings in PHP. It covers string types like single quoted, double quoted, heredoc and nowdoc strings. It also covers various functions for printing strings like echo, print, printf, print_r and var_dump. Additional string functions covered are strlen for calculating string length, trim for removing whitespaces, and functions for changing case like strtolower, strtoupper, ucfirst and ucwords.
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
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