Skip to content

PHP Tutorial Data Types

Strings

Lesson 35 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

A string is text: letters, digits, spaces and symbols, wrapped in quotes.

index.php
PHP
<?php
$greeting = "Hello, world";
$name = 'Anil';

echo $greeting . ", " . $name;
Output
OUTPUT
Hello, world, Anil

Single or double quotes

Both create a string. Double quotes can put variables and special characters like \n inside. Single quotes keep the text exactly as written. Single and Double Quotes explains this in detail.

index.php
PHP
<?php
$name = "Anil";

echo "Hi $name\n";
echo 'Hi $name\n';
Output
OUTPUT
Hi Anil
Hi $name\n

Joining strings

The dot . joins two strings.

index.php
PHP
<?php
$first = "Anil";
$last = "Kumar";

echo $first . " " . $last;
Output
OUTPUT
Anil Kumar

Strings are not numbers

A number inside quotes is still a string.

index.php
PHP
<?php
var_dump(10);
var_dump("10");
Output
OUTPUT
int(10)
string(2) "10"

PHP will often convert between them for you, but it is better to know which one you have. Type Juggling shows when that conversion happens.

Strings are made of bytes

PHP counts bytes, not characters. For plain English text they are the same. For characters like é or Hindi letters, they are not.

index.php
PHP
<?php
echo strlen("cafe") . "\n";
echo strlen("café");
Output
OUTPUT
4
5

The accented é uses two bytes. Use the mb_ functions for such text. That is covered in Multibyte Strings.

info
A whole chapter comes later
Chapter 6 is all about strings: searching, replacing, trimming and formatting. For now, remember: a string is text, and it needs quotes.