STRINGS IN PHP

Strings in PHP are sequences of bytes and are used to represent text. We can assign any string to a variable, the limit is given only by the amount of memory available, can be enclosed in single or double quotes, there are differences e.g. with single quotes no parsing of variables is done, also con double superscripts escape sequences are recognized.

String
Escape

HEREDOC AND NOWDOC

NOWDOC does not parse variables; otherwise it behaves like HEREDOC.

NEWDOC
HEREDOC

FUNCTIONS ON CODE STRINGS

Copy to Clipboard

DEEPENING

Strings in PHP are a sequence of characters used to represent text. PHP offers a wide range of functions for working with strings. Here is an overview of strings in PHP, along with some of the most common operations.

String Declaration

Strings can be declared using double quotes (“) or single quotes (‘).

– String with double quotes:

$string = “Hello, world!”

– String with single quotes:

$string = ‘Hello, world!’

The main difference is that variables and special escape sequences within a string with double quotes are interpreted, while with single quotes they are not.

Concatenation of Strings

In PHP, strings can be concatenated using the operator .:

$prima_parte = “Ciao“;
$seconda_parte = ” mondo!“;
$stringa_completa = $prima_parte . $seconda_parte;
echo $stringa_completa; // Output: Ciao mondo!

Common functions for strings

PHP offers many built-in functions for manipulating strings. Here are some of the most commonly used ones:

strlen(): Returns the length of a string.

$lunghezza = strlen(“Ciao mondo!”);
echo $lunghezza; // Output: 11

strpos(): Finds the position of a substring within a string.

$posizione = strpos(“Ciao mondo!“, “mondo“);
echo $posizione; // Output: 5

str_replace(): Replaces all occurrences of one substring with another.

$nuova_stringa = str_replace(“mondo“, “PHP“, “Ciao mondo!“);
echo $nuova_stringa; // Output: Ciao PHP!

substr(): Returns a specified portion of the string.

$sub_stringa = substr(“Ciao mondo!“, 0, 4);
echo $sub_stringa; // Output: Ciao

Strings and variables

If double quotes are used, variables within the string are interpreted; with single quotes, however, the variable is treated as literal text:

$nome = “Mario“;
echoCiao, $nome!“; // Output: Ciao, Mario!

Multi-row strings

PHP allows you to create multi-line strings using heredoc or nowdoc syntax.

Heredoc:

$testo = <<<EOD
Questo è un esempio
di stringa multiriga
in PHP.
EOD;

echo $testo;

Nowdoc (similar to heredoc but does not interpret variables):

$testo = <<<‘EOD’
Questo è un esempio
di stringa multiriga
in PHP.
EOD;

echo $testo;

Escaping special characters

If you want to include a special character in a string, such as an apostrophe in a string with single quotes, you must use the backslash (\):

$stringa = ‘L\’apostrofo‘;
echo $stringa; // Output: L’apostrofo

This is a basic overview of strings in PHP. Each function and operation can be explored in more detail depending on the specific needs of your project.

LINKS TO PREVIOUS POSTS

THE PHP LANGUAGE

LINK TO THE CODE ON GITHUB

GITHUB