Chapter 2.5
Chapter 2.5
The other way is to place the variable being interpolated within curly braces.
Example :
$a = 10;
echo "You are the {$a}th person";
output: You are the 10th person
Single-Quoted Strings : Single-quoted strings do not interpolate variables. Thus, the variable name in the
following example is not expanded because the string literal in which it occurs is single quoted:
Example :
$a=10;
echo ‘Hello , Today is $a’;
Output : Hello ,Today is $a
Printing Strings :
There are four ways to send output to the browser.
1. echo (): The echo construct lets you print many values at once.
2. print() prints only one value.
3. printf() function builds a formatted string by inserting values into a template.
4. print_r() function is useful for debugging—it prints the contents of arrays, objects, and other things,
in a more or-less human-readable form.
1. str_word_count() function : This function is used to count the number of words in a string.
syntax : str_word_count(string,return,char);
string : It indicates string to be checked.
return :It is optional. It specifies the return value of the function.
0- default. Returns the number of words found.
1- returns an array with the words from the string.
2- returns an array where the key is the position of the word in the string, and value is the
actual word.
char : Optional. It specifies special characters to be considered as words.
Example:
<?php
$str1="Welcome to WBP Theory & practical";
echo " <br> Total words in string str1= ".str_word_count($str1,0,"&");
echo "<br> words from str1 are as follows: -";
$str2=str_word_count($str1,1,"&");
foreach($str2 as $val)
{
echo " ".$val;
}
echo "<br> words from str1 along with their position in string: -";
$str2=str_word_count($str1,2,"&");
foreach($str2 as $key => $val)
{
echo "  {$key}=>{$val}";
}
?>
Output:
Total words in string str1= 6
words from str1 are as follows: - Welcome to WBP Theory & practical
words from str1 along with their position in string: 0=>Welcome 8=>to 11=>WBP 15=>Theory 22=>& 24=>practical
2. strlen() function : This function is used to find number of characters in a string . While counting
number characters from string, function also considers spaces between words.
syntax : strlen(string);
- string specify name of the string from which characters have to be counted.
Example :
<?php
$str3="Hello,welcome to WBP";
echo "<br> Number of characters in a string '$str3' = " .strlen($str3);
?>
Output : Number of characters in a string 'Hello,welcome to WBP' = 20
3. strrev() function : This function accepts string as an argument and returns a reversed copy of it.
Syntax : $strname=strrev($string variable/String );
Example :
<?php
$str4="Polytechnic";
$str5=strrev($str4);
echo "Orginal string is '$str4' and reverse of it is '$str5'";
?>
Output : Orginal string is 'Polytechnic' and reverse of it is 'cinhcetyloP'
4. strcmp() function : This function is used to compare two strings with each other . It is a case
sensitive comparison.
Syntax : $result= strcmp(string1,string2);
- string1 and string2 indicates strings to be compared with each other.
- This function returns 0 if both the strings are equal. It returns a value <0 if string1 is less than
string2 and >0 if string 1 is greater than string2
Example 1 :
<?php
$str6="Welcome";
$str7="Welcome";
echo strcmp($str7,$str6);
?>
Output =0
Example 2:
<?php
$str6="Welcome";
$str7="Whycome";
echo strcmp($str6,$str7);
?>
Output =-1 // as e comes first and then h so str6 is less than str7
Example 3:
<?php
$str6="Welcome";
$str7="Whycome";
echo strcmp($str7,$str6);
?>
Output=1 // as h comes after e so str7 is greater than str6
5. strpos() function : This function is used to find the position of the first occurrence of specified
word inside another string. It returns False if word is not present in string. It is a case sensitive function. by
default, search starts with 0th position in a string.
Syntax : strpos(String,findstring,start);
- string specify string to be searched to find another word
- findstring specify word to be searched in specified first parameter.
- start is optional . It specifies where to start the search in a string. If start is a negative number
then it counts from the end of the string.
Example:
<?php
$str8="Welcome to Polytechnic";
$result=strpos($str8,"Poly",0);
echo $result;
?>
Output : 11
6. str_replace() function : This function is used to replace some characters with some other
characters in a string.
Syntax : str_replace(findword,replace,string,count);
- Find word specify the value to find
- replace specify characters to be replaced with search characters.
- string specify name of the string on which find and replace has to be performed.
- count is optional . It indicates number of occurrences replaced from a string.
Example 1:
$str10="Welcome to vpm";
$str11=str_replace("vpm","msbte",$str10);
echo $str11;
Output : Welcome to msbte
Example 2:
$str10="Welcome to poly poly poly poly";
$str11=str_replace("poly","msbte",$str10,$a);
echo $str11,$a;
output : Welcome to msbte msbte msbte msbte4
7. ucwords() function : This function is used to convert first character of each word from the string
into uppercase.
Syntax : $variable=ucwords($Stringvar);
Example :
<?php
$str9="welcome to poly for web based development";
echo ucwords($str9);
?>
Output : Welcome To Poly For Web Based Development
8. strtoupper() function :This function is used to convert any character of string into uppercase.
Syntax : $variable=strtoupper($stringvar);
Example:
<?php
$str9="POLYtechniC";
echo strtoupper($str9);
?>
Output : POLYTECHNIC
9. strtolower() function : This function is used to convert any character of string into lowercase.
Syntax: $variable=strtolower($stringvar);
Example :
<?php
$str9="POLYtechniC";
echo strtolower($str9);
?>
Output: polytechnic