What you'll learn
Programs become useful when they can remember values. You'll learn how PHP names, classifies, inspects, and converts data—and when a value should become a constant.
- Create clearly named variables and update their values
- Recognize PHP's everyday scalar and compound types
- Inspect uncertain data with
var_dump()andgettype() - Choose explicit conversion over surprising implicit coercion
- Declare values that must not change
Variables are named values
A PHP variable begins with $, followed by a case-sensitive name. Assignment uses =: the expression on the right is evaluated, then stored under the name on the left.
<?php
$customerName = "Lina";
$itemCount = 3;
$subtotal = 24.50;
$hasCoupon = true;
echo "{$customerName} has {$itemCount} items.";Tip
camelCase for local variables and choose names that reveal meaning: $subtotal is more useful than $x or $floatValue.The core PHP types
| Type | Examples | Use |
|---|---|---|
int | 42, -7 | Whole numbers |
float | 19.99, 1.5e3 | Decimal and scientific values |
string | "PHP" | Text and bytes |
bool | true, false | Yes/no decisions |
array | ["a", "b"] | Ordered maps used as lists or records |
object | new DateTime() | State and behavior packaged as a type |
null | null | Intentional absence of a value |
PHP is dynamically typed: a variable can refer to values of different types at different times. The value has a type even though the variable declaration does not spell one out.
Key idea
Inspect values while developing
var_dump() shows a value's type and detailed structure. gettype() returns a readable type name. They are development tools—don't leave raw dumps in a production response.
<?php
$quantity = 4;
$price = 7.25;
$tags = ["php", "web"];
$discount = null;
var_dump($quantity, $price, $tags, $discount);
echo gettype($tags);For boolean questions, prefer focused checks such as is_int(), is_string(), is_array(), and is_null().
Casting and type juggling
PHP sometimes coerces compatible values automatically. That convenience can hide bugs at request and persistence boundaries, so convert deliberately when a source gives you text but your domain needs a number or boolean.
<?php
$quantityText = "4";
$quantity = (int) $quantityText;
$price = (float) "7.25";
$isPublished = (bool) 1;
$total = $quantity * $price;
echo number_format($total, 2);Validate before casting
"four" to int does not produce a meaningful quantity. First validate that the input represents an allowed integer; then convert it.Constants communicate invariants
Use a constant when a name should never be reassigned during execution. const is clear for values known in source code; define() can declare one at runtime.
<?php
const TAX_RATE = 0.11;
define('APP_NAME', 'Budget Board');
$subtotal = 100;
$total = $subtotal * (1 + TAX_RATE);
echo APP_NAME . ': ' . $total;Recap & quick check
Key takeaways
- PHP variables start with $ and should be named for their meaning.
- Every value has a type even though PHP variables are dynamically typed.
- Use var_dump for detailed development inspection and is_* functions for focused checks.
- Validate external text before explicitly converting it to a domain type.
- Use constants to name values that must not change during a run.
Quick check
1. Which name follows normal PHP variable syntax?
2. Which tool shows both a value's type and structure?
3. What should happen before casting request text to an integer?