PHP Strings

PHP Data Type

A string is a sequence of letters, numbers, special characters and arithmetic values or combination of all.The simplest way to create a string is to enclose the string literal (i.e. string characters) in single quotation marks ('), like this:

$my_string = 'Hello World';

You can also use double quotation marks ("). However, single and double quotation marks work in different ways. Strings enclosed in single-quotes are treated almost literally, whereas the strings delimited by the double quotes replaces variables with the string representations of their values as well as specially interpreting certain escape sequences.

The escape-sequence replacements are:

  • \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 (\)

PHP String

string is a sequence of characters, like "Hello world!". A string can be any text inside quotes. You can use single or double quotes:

  •       
                            <?php
    $my_str = 'World';
    echo "Hello, $my_str!
    "; // Displays: Hello World! echo 'Hello, $my_str!
    '; // Displays: Hello, $my_str! echo '<pre>Hello\tWorld!</pre>'; // Displays: Hello\tWorld! echo "<pre>Hello\tWorld!</pre>"; // Displays: Hello World! echo 'I\'ll be back'; // Displays: I'll be back ?>

    Manipulating PHP Strings

    PHP provides many built-in functions for manipulating strings like calculating the length of a string, find substrings or characters, replacing part of a string with different characters, take a string apart, and many others.

    PHP String Functions

    lets look at some commonly used functions to manipulate strings.

    strlen( ) - Return the Length of a String

    The PHP strlen() function returns the length of a string.

  •       
                            <?php
    echo strlen("Hello world!"); // outputs 12
    ?>
                        

    str_word_count( ) - Count Words in a String

    The PHP str_word_count() function counts the number of words in a string.

  •       
                            <?php 
    echo str_word_count("Hello world!"); // outputs 2
    ?>
                        

    strrev( ) - Reverse a String

    A Boolean represents two possible states: TRUE or FALSE. Booleans are often used in conditional testing. $x = true; $y = false;

  •       
                            <?php
    echo strrev("Hello world!"); // outputs !dlrow olleH
    ?>
                        

    strpos( ) - Search For a Text Within a String

    The PHP strpos() function searches for a specific text within a string. If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.

  •       
                            <?php
    echo strpos("Hello world!", "world"); // outputs 6
    ?>
                        

    str_replace( ) - Replace Text Within a String

    A Class and objects are the two main aspects of object-oriented programming. A class is a template for objects, and an object is an instance of a class.

  •       
                            <?php
    echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello Dolly!
    ?>
    
                        

    Dess App

    DessApp is an Integrated E-learning Education, Interactive and User-friendly features, smarter options and redefining your school costs effectively and efficiently.

    View
    1 1