Website Templates Search

String basics in PHP

  • Click to view live demo By Admin
  • Click to view live demo Aug 26, 2017

A string is a sequence or series of characters, numbers, symbols in php.
Characters comes inside single quotes or double quotes treat as string.
We can create string by enclosing characters inside single / double quotation mark.

<?php

    $string ='My First String';  // Inside single-quotes
	$string ="My First String";  // Inside double-quote
	echo "$string";
	
?>

We will concatenate two strings by dot(.) character

Example:

<?php

	$str1 = "example";
	$str2 = "Php String ". $str1;
	$str3 = 'String Basic '.$str1;
	print $str2;
	print $str3;
	
?>
 
------------ ****** ------------
OUTPUT:
        Php String example.
        String Basic example.
		

In Double quoted String we can take variable directly inside double quote or with concatinate sign(.).
In single quoted string we will take variable with concatenation sign (.).

Example :

<?php

	$str1 = "String example";
	
	// This line is slow then below line 
	//(parser first check concatinate string with variable the parse whole string)
	
	$str2 = "Double quote $str1";   
	
	// This line is fast then double qoute
	$str3 = 'Single quote '.$str1;  
	
	print $str2;
	print $str3;
	
?>
 
------------ ****** ------------
OUTPUT:
        Double quote String example.
		Single quote String example.

we will escape character if double quoted string contains double quote or single quoted string contains single quote.

Example:

<?php

	$str1 = "Php String Manipulation\" examples"; // Working
	$str2 = 'Php String\' example ';  // Working
	$str3 = "Php string \" tutorial";  // Error (Must be escape inner double quote)
	$str4 = 'Php string \' tutorial';  // Error (Must be escape inner single quote)

?>

Single and double quotation marks work in different ways.
String between single-quotes does not change variable with value,
Strings inside double quotes replaces variables with the string representations of their values.

<?php

	   $str1 = "";
	   print '<pre>Single quoted string show escape character \n as it is.!</pre>';
	   print "<pre>Double  quoted string will show string \n representation of escape characters!</pre>";
	 
	 /*  Escape Characters String Replacement Values
	 
	   \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 (\)
	   
	 */

?>
 
------------ ****** ------------
OUTPUT:
        Single quoted string show escape character \n as it is.!
		Double quoted string will show string
		representation of escape characters!
		

Commonly Used PHP String Functions:

PHP have many built-in functions for strings ex. calculating the length of a string,
find sub strings or characters, replacing sub string of a string and many others.
we are providing examples of some commonly used string manipulation functions.

 

1. Find length of a String :

Getting Number of characters any string contains.

<?php
	
	$str = "Php Tutorial";
	echo strlen($str);  // space also count as one character
	
?>
 
------------ ****** ------------
OUTPUT:
        12

2. Replace text in a string

<?php

	$str = "String basic examples";
	echo str_replace('examples','geeks',$str);  // space also count as one character

?>
 
------------ ****** ------------
OUTPUT:
        String basic geeks
		

3. Convert first character of every word in a string to Uppercase

<?php

	$str = "phponweb devepolper blogs";
	echo ucwords($str);  // space also count as one character

?>
 
------------ ****** ------------
OUTPUT:
        Phponweb Devepolper Blogs
		

4. Converting all string letters to UPPERCASE

<?php
 
	$str = "download php example source code";
	echo strtoupper($str);  // space also count as one character

?>
 
------------ ****** ------------
OUTPUT:
         DOWNLOAD PHP EXAMPLE SOURCE CODE

5. Converting all string letters to LOWERCASE

<?php
 
	$str = "CONVERT ALL STRING CHARACTERS INTO LOWERCASE";
	echo strtolower($str);  // space also count as one character

?>
 
------------ ****** ------------
OUTPUT:
        convert all string characters into  lowercase
		

5. Reversing string letters

<?php
 
	$str = "Reverse a String";
	echo strrev($str);  // space also count as one character

?>
 
------------ ****** ------------
OUTPUT:
        gnirtS a esreveR

6. Count the word of a string

<?php

	$str = "Count Numbetr of words in a String";
	echo str_word_count($str);  // space also count as one character

?>
 
------------ ****** ------------
OUTPUT:
        7