Skip to content
Learn Netverks

Lesson

Step 7/36 19% through track

variables-scalars

Variables and scalar types

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Variables and scalar types: the syntax, APIs, and habits you need before advancing in PHP.

Teams ship Variables and scalar types on every PHP codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Variables and scalar types in contexts like: LAMP/LEMP stacks, Laravel apps, WordPress themes/plugins, and shared hosting.

Write PHP in the editor and click Run on server—the dev runner executes your script and returns stdout/stderr (set LEARNING_RUNNER_ENABLED=true locally).

When you can explain the previous lesson's ideas without copying starter code.

Variables start with $ and are loosely typed by default—PHP figures out the type from the value. PHP 7+ adds scalar type hints and PHP 8+ strengthens the type system with union types and more.

Scalar types

  • string — text in single or double quotes
  • int / float — numbers; watch division (10 / 4 is float 2.5)
  • booltrue / false

Assignment and reassignment

$count = 0;
$count = $count + 1;
$name = 'Tuto';

Unset with unset($name) when you need to remove a variable from the current scope.

Constants

define('APP_NAME', 'Tuto'); or const APP_VERSION = '1.0'; at class/top level. Constants are not prefixed with $.

Important interview questions and answers

  1. Q: Is PHP statically typed?
    A: Historically no; you can enable strict types per file with declare(strict_types=1); and use type hints for parameters and return values.
  2. Q: vs ===?
    A: == compares with type juggling; === compares value and type—prefer === unless you explicitly need coercion.

Self-check

  1. What prefix do all variables use?
  2. Name three scalar types.

Pitfall: Use === for comparisons—loose == coerces types and causes subtle bugs (0 == 'foo').

Interview prep

== vs === in PHP?

=== compares value and type; == coerces types and causes legacy surprises—prefer strict comparison in application code.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Weak typing pitfall?
  • $ vs other langs?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump