php language basics
Types And Values
Every value in PHP has a type. The type tells PHP what kind of value it is working with and which operations make sense.
Start with four common values from a product record:
<?php
$productName = 'Notebook';
$priceCents = 1299;
$inStock = true;
$discount = null;
var_dump($productName);
var_dump($priceCents);
var_dump($inStock);
var_dump($discount);
// Prints:
// string(8) "Notebook"
// int(1299)
// bool(true)
// NULL
var_dump() shows the type and the value. It is useful while learning and while debugging.
Common Types
string: text such as a product name.int: whole numbers such as a quantity or price in cents.float: numbers with a fractional part.bool: eithertrueorfalse.null: an intentionally missing value.array: a group of values.
<?php
$tags = ['php', 'beginner'];
$rating = 4.5;
var_dump($tags);
var_dump($rating);
// Prints:
// array(2) {
// [0]=>
// string(3) "php"
// [1]=>
// string(8) "beginner"
// }
// float(4.5)
Objects, resources, and callables are introduced later. For now, learn to recognise the type you have before trying to use it.
Values From Outside PHP Need Care
A submitted form field normally arrives as text, even when it looks like a number. The string '10' and the integer 10 are related but not identical values.
<?php
var_dump('10');
var_dump(10);
// Prints:
// string(2) "10"
// int(10)
Later lessons cover comparisons and validation. The important habit is to inspect and understand incoming values instead of assuming their type.
Practice
Inspect Product Values
Create values for a product name, price in cents, stock status, and missing discount. Use var_dump() to inspect each one.
Show solution
<?php
$name = 'Notebook';
$priceCents = 1299;
$inStock = true;
$discount = null;
var_dump($name, $priceCents, $inStock, $discount);
// Prints:
// string(8) "Notebook"
// int(1299)
// bool(true)
// NULL
Predict Two Types
Predict the var_dump() output for '25' and 25, then run the script. Explain the difference.
Show solution
<?php
var_dump('25');
var_dump(25);
// Prints:
// string(2) "25"
// int(25)
The quoted value is text. The unquoted whole number is an integer.
Build A Typed Summary
Create an array containing a product name, integer quantity, float rating, boolean stock flag, and null discount. Inspect the array with var_dump().
Show solution
<?php
$product = [
'name' => 'Notebook',
'quantity' => 3,
'rating' => 4.5,
'in_stock' => true,
'discount' => null,
];
var_dump($product);
The dump shows that one array can contain values with different types. Later lessons cover how to read those keys safely.