how to use string in php

By | October 28, 2019

String in php-:  String is a sequence  characters in php like a “hello netnic “. String is  used to store and manipulate text and php supports only 256-character set and so that it does not offer native Unicode support. There are 4 ways to specify a string literal in php.

Types of string in php-: In the php has four type string. These are the

string in php

1-: Single string in php-:  Single string are use in single quote in php. For example

$String1 = ' This is the example of single string in php';

2-: Double string in php : Double string are use in double quote in php. For example

$String2 = " This is the example of single string in php";

3-:Heredoc string in php-: This heredoc syntax (<<<) is the third way to delimit strings. Its identifier  heredoc <<< operator and immediately a new line is started to write any text. To close the quotation, the string follows itself and then again that same identifier is provided. That closing identifier must begin from the new line without any white-space or tab. Example of this ..

<?php  
    $string3 = <<< Demo;  
 
Demo;   
echo $string3;  
?>

4. Newdoc string in php- : This is the similar as heredoc string in php. But newdoc string is use in single quote and heredoc is used in double quote . newdoc parsing is not done. It is also identified with three less than symbols <<< followed by an identifier. But here identifier is enclosed in single-quote for example <<<‘exp’.

 

String function in php-: In the php has many string function. These function  built-in String manipulation functions, supporting operations ranging from string repetition and reversal to comparison and search-and-replace. Some string function list has been given in the table.

  1. empty() -: A string is empty.
  2. strlen() -: This string is use calculate the character
  3. strrev () -: This string is use return of a given number.
  4. str_repeat() -: This is use how many time string repeat.
  5. substr()-: This is use retrieve a section of string
  6. strcmp()-: This is use to compare two string
  7. str_word_count()-: This is use to calculate the number of words
  8. str_replace() -: this is use to replace the number
  9. trim() -: This string use removes leading and trailing white-spaces from a string
  10. ucfirst() -:Converts in uppercase the first character of a string

 

A example of string function in php-:

<html>
<head>
<title>string function in php</title>
</head>
<body>
<?php
echo strlen("welcome netnic");// character count
  echo " </br>";
  echo str_word_count("welcome netnic"); //word count 
    echo " </br>";
  echo strrev("welcome netnic");//reverse word
  echo " </br>";
 echo strpos("Hello world!", "world");
    echo " </br>";
  echo str_replace("world", "Dolly", "netnic");// replace of word
?> 
</body>
</html>

 

Output of this program

14
2
cinten emoclew
6
netnic

Leave a Reply

Your email address will not be published. Required fields are marked *