String Functions PHP Essential Training

String Functions PHP Essential Training

PHP

String Functions PHP Essential Training

Welcome to another effective lesson in our essential training. In this lesson we're gonna learn as we told in the previous class in depth functions and secrets of the String Functions PHP Essential Training. So far we teach what is the basic operations of the string functions. Not like the other variables string has extra features and functions. Checkout the previous lesson PHP Variable Types Essential Training
The string is nothing but the sequence of characters. We can use any number of characters unlimited inside this variable. We've different kind of choices to display the string variables. Lets jump into the example and see how really fun they are.
<?php
//This is a general string without using any variable.
echo "Hello World<br />";
//We also use single quote for the string
echo 'Hello World<br />';
//Now lets save the string in variable and display from the variable
$var1 = 'Hello World<br />';
echo "$var1<br />";
//This will show you how to concatenate a variable with string
echo "$var1" . "Again"
?>
String Functions PHP Essential Training
The above will shows you the basic functions of the strings and now lets see how the singly quoted and doubly quoted strings are differed.
<?php
//Consider a variable name with in the single quotes
echo '$var1 Again.<br />'
//It directly prints the variable name as it is.
So here we use curly braces to differentiate a variable from the normal string
echo '{$var1} Again.<br />'
But in double quotes it returns the value of the variable
echo "$var1 Again.<br />"
?>
String Functions PHP Essential Training

Extra String Functions PHP Essential Training

Like the other programming and scripting languages PHP also has the predefined functions for the particular tasks. Like as the String has some of the following functions for various tasks on string.
$first = "The quick brown fox";
$second = " jumped over the lazy dog";
$third = $first;
// The following is the way of another concatenate method of two strings or any other variable values.
$third .= $second;
echo "$third <br />";
String Functions PHP Essential Training
Here lets see some other string functions PHP Essential Training
// This strtolower class is change all characters in to lower case
Lower Case: <?php echo strtolower($third); ?>
String Functions PHP Essential Training
// This strtoupper class is change all characters in to Upper case
Upper Case: <?php echo strtoupper($third); ?>
String Functions PHP Essential Training
//This ucfirst class is change the first letter of the sentence in to upper case
Upper Case First-letter: <?php echo ucfirst($third);?>
String Functions PHP Essential Training
//This ucwords class is change the first letter of all words in the sentence in to upper case
Upper Case Words: <?php echo ucwords($third); ?>
String Functions PHP Essential Training
//The strlen function is used to find the length of the string
Length: <?php echo strlen($third); ?>
String Functions PHP Essential Training
//The trim is just remove the unwanted spaces
Trim: <?php echo $four = $first . trim($second); ?>
String Functions PHP Essential Training
//This strstr is used to find the exact word in the given sentence
Find: <?php echo strstr($third, "brown"); ?>
String Functions PHP Essential Training
//The str_replace is a function used to replace a particular word in to another word
Replace By String: <?php echo str_replace("quick", "super-fast", $third); ?>
String Functions PHP Essential Training
//The strpos is a function used to find a particular position of a word in a sentence
String Position: <?php echo strpos("Hello world!", "world"); ?>
String Functions PHP Essential Training
We hope this lesson will gives you an idea about the functions we can do with Strings. Check out the Variable types in PHP Essential Training
Download the source Code of String Functions PHP Essential Training from here.

Leave a Reply