String Basics and Concatenation
Build and join strings in PHP with . and interpolation.
String Basics and Concatenation is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Strings in PHP
A string is a sequence of characters. PHP strings are binary-safe and can contain any byte. They are immutable — string operations return new strings, they don't modify originals.
Single vs Double Quotes
Single-quoted strings are literal; double-quoted strings parse variables and escape sequences:
<?php
$name = 'Alice';
echo 'Hello $name\n'; // Hello $name\n — literal
echo "Hello $name\n"; // Hello Alice — parsed
// Prefer single quotes for performance when no interpolation neededConcatenation with .
Use the dot operator . to join strings:
<?php
$first = 'Hello';
$last = 'World';
$msg = $first . ', ' . $last . '!';
echo $msg; // Hello, World!
// Append with .=
$log = 'Start';
$log .= ' -> Process';
$log .= ' -> End';
echo $log; // Start -> Process -> EndString Length: strlen vs mb_strlen
strlen() returns byte count; for multi-byte strings (UTF-8) use mb_strlen():
<?php
$ascii = 'Hello';
$utf8 = 'Merhaba'; // Turkish
$emoji = '😀';
echo strlen($ascii); // 5
echo mb_strlen($ascii); // 5
echo strlen($emoji); // 4 (UTF-8 bytes)
echo mb_strlen($emoji); // 1 (one character)String Repetition
Repeat a string N times with str_repeat():
<?php
echo str_repeat('ab', 3); // ababab
echo str_repeat('-', 20); // --------------------
// Build a simple separator line:
echo str_repeat('=', 40) . PHP_EOL;Case Functions
Convert string case:
<?php
$str = 'hello WORLD php';
echo strtoupper($str); // HELLO WORLD PHP
echo strtolower($str); // hello world php
echo ucfirst($str); // Hello WORLD php
echo ucwords($str); // Hello WORLD Php
// Multi-byte safe versions:
echo mb_strtoupper('istanbul'); // ISTANBULTrimming Whitespace
Remove leading/trailing whitespace or specific characters:
<?php
$raw = ' hello world ';
echo trim($raw); // 'hello world'
echo ltrim($raw); // 'hello world '
echo rtrim($raw); // ' hello world'
// Trim specific characters
echo trim('/path/to/page/', '/'); // 'path/to/page'String Comparison
Compare strings with native operators or functions:
<?php
// == uses type juggling; === is strict
var_dump('abc' === 'abc'); // true
var_dump('abc' === 'ABC'); // false
// Case-insensitive comparison
var_dump(strcasecmp('PHP', 'php') === 0); // true
// Lexicographic comparison
echo strcmp('apple', 'banana'); // negative (apple < banana)String Indexing
Access individual characters by index with bracket notation:
<?php
$str = 'PHP';
echo $str[0]; // P
echo $str[2]; // P
echo $str[-1]; // P (last char)
// Modify a character
$str[1] = 'H';
echo $str; // PHP (unchanged — strings are semi-mutable this way)Heredoc String
Write long strings with heredoc — useful for HTML generation:
<?php
$user = 'Alice';
$role = 'Admin';
$html = <<<HTML
<div class="user-card">
<h2>$user</h2>
<span class="badge">$role</span>
</div>
HTML;
echo $html;Number Formatting
Format numbers as strings with number_format and sprintf:
<?php
$amount = 1234567.891;
echo number_format($amount, 2); // 1,234,567.89
echo sprintf('%010.2f', 9.5); // 0000009.50
echo sprintf('%+d', -42); // -42
echo sprintf('%+d', 42); // +42Quick Check
Which function correctly counts the number of characters in a UTF-8 string?
Recap: String Basics
Key string fundamentals:
- Single-quoted strings are literal; double-quoted parse variables
- Concatenate with
.and append with.= - Use
mb_strlen()for multi-byte character counting strtoupper/lower,ucfirst/wordsfor case conversiontrim/ltrim/rtrimto strip whitespace
Next: searching and replacing text in strings.
Frequently asked questions
Is the “String Basics and Concatenation” lesson free?
Yes — the full text of “String Basics and Concatenation” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “String Basics and Concatenation”?
Build and join strings in PHP with . and interpolation. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “String Basics and Concatenation” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- String Basics and Concatenation
- Searching and Replacing in Strings
- Substring and Padding Functions
- Splitting and Joining Strings