PHP

PHP String


 

 

A string is a sequence of characters, where characters can be letters, numbers, or symbols i.e., used to store and manipulate it.

 

Characteristics of PHP.

  • Simplicity
  • Efficiency
  • Flexibility
  • Familiarity
  • Security
     

We can define a string in 3 ways in PHP:

  • Single quoted
  • Double quoted
  • Heredoc syntax

 

1. Single Quoted String

A single-quoted string is a sequence of characters enclosed within single quotes (').
 

Example:

<?php
    $singleQuotedString = 'Hello, Learners';
    echo $singleQuotedString;
?>

 

Output:

 

2. Double Quoted String

A double-quoted string is a sequence of characters enclosed within double quotes (`”`).

 

Example:

<?php
    $greets = 'Hello, "Learners!"';

    // Double quotes allow single quotes within the string
    echo "Greets: $greets". "<br>";

    // Single quotes not process special characters
    echo 'Greets: $greets'. "<br>";

    // Single quotes allow double quotes within the string
    echo 'Greets: '.$greets;
?>

 

Output:

 

Note:  When using double quotes, PHP will interpret certain special characters and variables within the string, while single quotes will not interpret special characters.

 

3. Heredoc Syntax

Heredoc syntax (<<<) is the third way to declare strings. Heredoc define a block of text in some programming languages like PHP. It allows you to create a multi-line string without having to enclose each line in quotes.

 

Example:

<?php  
    $str = 'heredoc';
    $hereStr = <<<EOT
        This is a
        multi-line
        string using heredoc syntax.
        You can include variables like this: $str
        EOT;
    
    echo $hereStr; 
?>

 

Output:

 

4, String Concatenation Operator

In PHP, the string concatenation operator is a dot (.). Dot is used to concatenate two or more strings together.

 

Example:

<?php
    $str1 = "Hello";
    $str2 = "Learners";
    $greet = $str1 . $str2;
    echo $greet;
?>

 

Output:

 

5. Finding length of a string:

The strlen() is the built in function used to find the length of a string.

 

Example

<?php
    $str = "LearnCoding";
    echo strlen($str);
?>

 

Output

 

6. Finding position

The strops() is the built in function in PHP which is used to search for a character and string within a given string.

 

Example:

<?php
    $str = "PHP was created by Rasmus lerdorf";
    echo strpos( $str,"created");
?>

 

Output:

 

7. Searching & Replacing String:

In PHP, str_replace() function is used to replace occurrences of a substring with another string within a given string.


Syntax:

<?php
    str_replace($search, $replace, $subject);
?>

 

Example:

<?php
    $str = "Hello Geek";
    $str_new = str_replace("Geek", "Learners", $str);
    echo $str_new;
?>

 

Output:

 

8. Formatting String

Php sprintf() and printf() function is used for formatting of string.
 

In printf() function uses format specifiers to format strings, while sprintf() function returns a  formatted string instead of printing it. Replaces the percent(%) sign by variable passed as an argument. Related functions: printf(), vprintf(), vsprintf(), fprintf() and vfprintf().


Formatter values:

 

  • %% - Returns a percent sign
  • %b - Binary number
  • %c - The character according to the ASCII value
  • %d - Signed decimal number (negative, zero or positive)
  • %e - Scientific notation using a lowercase (e.g. 1.2e+2)
  • %E - Scientific notation using a uppercase (e.g. 1.2E+2)
  • %u - Unsigned decimal number (equal to or greater than zero)
  • %f - Floating-point number (local settings aware)
  • %F - Floating-point number (not local settings aware)
  • %g - shorter of %e and %f
  • %G - shorter of %E and %f
  • %o - Octal number
  • %s - String
  • %x - Hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (uppercase letters)

 

Example:

<?php
    $num = 2;
    printf("The rank is %d", $num);

    $str_sprintf = sprintf("THe rank is %d", $num);
    echo $str_sprintf;
?>

 

Output:

 

Other common String Library functions:

 

Function

Uses

substr()

It returns the subpart of a string

strtolower()

It converts the string to lowercase

strtoupper()

It converts the string to uppercase

trim()

It removes whitespace and other characters from the start and end of a string

strcmp()

It compares two strings

number_format

It is a case-sensitive string comparison

 

Did you Know?

There are more than 50 php functions related to string & Php supports 256-character set.