PHP 8 Strings

A string is a data type used in programming, that is used to represent text rather than numbers. String is a sequence of characters.

There are 4 ways to specify a string literal in PHP -
  • Single quoted
  • Double quoted
  • Heredoc syntax
  • Newdoc syntax

Single quoted

The simplest way to specify a string is to enclose it in single quotes. This method in used when we want to get the string to be exactly as it is written. It does not process special characters inside quotes. If we place a variable inside single quotes, it will output as the same variable name but not its value. It executes slightly faster than double quotes because everything written inside single quotes is treated as a plain string.

<?php
// single-quote string
echo 'Hello World!';
$site  = 'etutorialspoint';
 
echo ' Welcome to $site';
?>
Output of the above code -
Hello World! Welcome to $site


Double quoted

Unlike single-quote strings, double-quote strings in PHP are capable of parsing special characters, regular expressions, and variables in the strings. Because of parsing escaped characters & variables, this is slower than single quoted string.

<?php
// double-quote strings
$site  = "etutorialspoint";
 
echo "Welcome to $site";
?>
Output of the above code-
Welcome to etutorialspoint


Heredoc quoted

Heredoc is basically used for specifying string values. It is an alternative way to define string values using double quotes and single quotes and basically used in multiple line strings. The heredoc syntax is <<<, after this an identifier is provided, then the strings come on the new line and the same string identifier is used to close the quotation with semicolon(;) at the end. Heredoc is equivalent to double quoted strings.

<?php
$attendant = <<<EOT
Welcome to the event!
EOT;
echo $attendant;
?>
Output of the above code -
Welcome to the event!


Newdoc quoted

Nowdoc is equivalent to single quoted strings. The difference between Heredoc and Nowdoc is same like the difference between double quoted strings and single quoted strings. The Nowdoc syntax is much like Heredoc, except the opening identifier is enclosed in single quotes. No parsing is done inside Nowdoc.

<?php
$attendant = <<<'EOT'
Welcome to the event!
EOT;
echo $attendant;
?>
Output of the above code -
Welcome to the event!

Both heredoc and newdoc are introduced in PHP 7.3. It is recommended to use heredoc and nowdoc where you need to write multiple line long strings.



PHP String Functions

PHP has a good combination of built-in functions for string handling and for easily manipulation of strings.

Like by using PHP string built-in functions, we can -
  • a. Get length of string.
  • b. Reverse a string.
  • c. Convert the string in lowercase or uppercase.
  • d. Split a string.
  • e. Replace some part or character of a string.
  • f. Trim whitespaces from beginning and end of the string.
  • g. Convert the string in encoded form.
  • h. Get position of substring or a character in a string.

Lists of all built-in functions are given in PHP official website -
http://php.net/manual/en/ref.strings.php

Some of the uses of built-in functions are -

echo()

The echo() function is used to display one or more strings. The echo() function can be used with parentheses or without parentheses. The echo() function is slightly faster than print().

<?php
    $str1 = "Hello John, How are you?";
    echo ($str1);
?>




strlen()

The strlen() function is used to get length of a string. It takes a string as a parameter and returns its length.

<?php
    $str = 'India';
    echo strlen($str); // Output 5
?>

strcmp()

The strcmp() function is used to compare two strings. It takes two strings as parameter. The strcmp() function is binary-safe and case-sensitive.

<?php
    $str1 = "hello";
    $str2 = "Hello";
    $str3 = "";
    $str4 = "lo";

    echo strcmp($str1, $str2); 
    echo strcmp($str3, $str4); 
    echo strcmp($str1, $str4); 
?>

strpos()

The strpos() function is used to find the first occurrence of a substring in a string. The strpos() function is case-sensitive.

<?php
    $str1 = "etutorialspoint";
    $str2 = "tutorial";
    $getpos = strpos($str1, $str2); 
    echo $getpos; // output 
?>

strtolower()

The strtolower() function converts a string to lowercase. This function takes a string as parameter and converts all the uppercase to lowercase.


strtoupper()

The strtoupper() function converts a string to uppercase. This function takes a string as parameter and converts all the lowercase to uppercase.

<?php
    $str1  = "Hello World";
    echo $strlowercase  = strtolower($str1 ); // Output hello world
    echo $struppercase = strtoupper($str1 ); // Output HELLO WORLD   
?>


Related Articles

PHP convert string into an array
PHP String Contains
PHP remove last character from string
PHP random quote generator
PHP reverse a string without predefined function
PHP String Contains


More Practice Exercises





Read more articles


General Knowledge



Learn Popular Language