PHP Strings

PHP Strings

A string variable is used to store and manipulate a piece of text. 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:

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 (\)
Here’s an example to clarify the differences between single and double quoted strings:

Following are valid examples of string

  $string1 = "This is a simple string";
  $string2 = "This is a somewhat longer, singly quoted string";
  $string3 = "This string has thirty-nine characters";
  $string4 = ""; // a string with zero characters

Singly quoted strings are treated almost literally, whereas doubly quoted strings replace variables with their values as well as specially interpreting certain character sequences.

   $variable = "name";
   $literally = 'My $variable will not print!\\n';
   print "<br />";
   $literally = "My $variable will print!\\n";

This will produce the following result −

  My $variable will not print!\n
  My name will print

Get The Length of a String

The PHP strlen() function returns the length of a string. The example below returns the length of the string “Hello world!”:

 echo strlen("Hello world!"); // outputs 12

Count The Number of Words in a String

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

  echo str_word_count("Hello world!"); // outputs 2

Reverse a String

The PHP strrev() function reverses a string:

echo strrev("Hello world!"); // outputs !dlrow olleH

Replace Text Within a String

The PHP str_replace() function replaces some characters with some other characters in a string.
The example below replaces the text “world” with “Rahul”:

  echo str_replace("world", "Rahul", "Hello world!"); // outputs Hello Dolly!


Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.