Skip to content
Educora
Beginner15 min2 / 10

Variables, types and strings

Create variables, inspect PHP's basic types with var_dump, put variables inside strings, write heredocs and use handy string functions.

Check yourself
In this lesson you will learn
  • Create variables and recognise PHP's basic types
  • Tell apart single- and double-quoted strings, interpolation and heredoc
  • Process text with functions such as strlen, str_replace, sprintf and number_format

Think of a profile page on any website: name, age, balance, a “student” badge. The program has to keep this information somewhere. That is what variables are for. Since web programming is mostly about text, this lesson pays special attention to strings.

Variables and types

In PHP a variable name starts with the $ sign, and a value is assigned with =. You do not need to declare the variable or write its type in advance: PHP works out the type from the value. Such languages are called dynamically typed. Names must start with a letter or _; camelCase such as $firstName is the usual style.

PHP
<?php

$name = "Aysel";
$age = 15;
$height = 1.62;
$isStudent = true;
$phone = null;

var_dump($name);
var_dump($age);
var_dump($height);
var_dump($isStudent);
var_dump($phone);
echo gettype($age), " / ", get_debug_type($height), "\n";
Expected output
string(5) "Aysel"
int(15)
float(1.62)
bool(true)
NULL
integer / float
TypeExampleWhat it stores
int15, -3, 2026whole numbers
float1.62, 0.5, 3e2numbers with a fractional part (in code the separator is always a dot)
string"Aysel", 'PHP'text
booltrue, falsea logical value: true or false
nullnull“no value”
array[5, 4, 5]a collection of values (lesson 4)
PHP's scalar types, null and array

var_dump shows both the type and the contents of a value — it is the most useful function for debugging. For strings it also prints the length: string(5). gettype returns old names (integer, double), while get_debug_type, added in PHP 8, returns modern ones (int, float). For values that never change, create a constant: const MAX_SCORE = 100; — a constant has no $ in its name.

Single and double quotes

A single-quoted string keeps the text exactly as it is: neither variables nor special characters such as \n work there. In a double-quoted string, variables are replaced by their values — this is called interpolation — and characters like \n and \t work. To join strings, use the dot operator ., and to append to the end, use .=.

PHP
<?php

$city = "Ganja";
$year = 2026;
$fruit = "apple";

echo 'Hello from $city\n', "\n";
echo "Hello from $city in $year\n";
echo "Hello from " . $city . "!\n";
echo "I ate two {$fruit}s\n";

$message = "Good";
$message .= " luck";
echo $message, "\n";
Expected output
Hello from $city\n
Hello from Ganja in 2026
Hello from Ganja!
I ate two apples
Good luck

The curly braces in {$fruit} tell PHP where the variable name ends. Without them, "$fruits" would look for a different, non-existent variable called $fruits. Use {...} also when you put an array element or an object property into a string: "{$user['name']}".

Heredoc and nowdoc

For multi-line text (a letter, an e-mail template, a piece of HTML), heredoc is handy: it starts with <<<TEXT, ends with the same marker and fills in variables like double quotes do. Nowdoc (<<<'TEXT', with the name in quotes) keeps the text as it is, like single quotes. Since PHP 7.3 the closing marker may be indented — then the same amount of indentation is removed from every line.

PHP
<?php

$name = "Murad";
$course = "PHP";

$letter = <<<TEXT
    Dear $name,
    welcome to the {$course} course!
      Lesson 1 starts today.
    TEXT;

echo $letter, "\n";

echo <<<'RAW'
Variables like $name stay as they are.
RAW;
Expected output
Dear Murad,
welcome to the PHP course!
  Lesson 1 starts today.
Variables like $name stay as they are.

Useful string functions

PHP
<?php

$text = "  Learning PHP is fun  ";
$clean = trim($text);

echo strlen($clean), "\n";
echo strtoupper($clean), "\n";
echo str_replace("fun", "easy", $clean), "\n";
echo substr($clean, 0, 8), "\n";
var_dump(str_contains($clean, "PHP"));
echo ucfirst("baku"), "\n";
echo sprintf("%.2f AZN", 12.5), "\n";
echo number_format(1234567.891, 2), "\n";
Expected output
19
LEARNING PHP IS FUN
Learning PHP is easy
Learning
bool(true)
Baku
12.50 AZN
1,234,567.89
  • trim removes spaces at the start and end of a string — always clean form input this way.
  • substr($s, 0, 8) takes 8 characters starting at position 0; str_contains (PHP 8) checks whether a substring is present.
  • sprintf builds a string from a template: %.2f inserts a float with two decimal places, %d an integer and %s a string.
  • number_format rounds a number and separates the thousands.

Key points

  • A variable name starts with $; PHP is dynamically typed and works out the type from the value.
  • The basic types are int, float, string, bool, null and array; var_dump shows the type and the value together.
  • Double quotes process variables and \n, single quotes keep text as it is; strings are joined with ..
  • Heredoc fills variables into multi-line text, while nowdoc keeps the text unchanged.
  • Use the mb_ functions for text with Azerbaijani, Russian or Turkish letters: strlen counts bytes.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
What does var_dump(7.0); print?