Give a value a name
A variable gives a name to a value used by a program. In PHP, its name starts with $. The = operator assigns a value. Here we store a lesson count, text and a boolean:
<?php
$completed = 2;
$technology = "PHP";
$hasAccount = true;
var_dump($completed, $technology, $hasAccount);
Output
int(2)
string(3) "PHP"
bool(true)
var_dump displays type and content. The results are int(2), string(3) "PHP" and bool(true), each on its own line. A descriptive name helps explain the value's meaning, although the name alone does not enforce its type.
Understand the type of each value
An int is an integer. A float stores floating-point numbers. A string contains text, and a bool has the values true and false. The text "2" and the number 2 may look similar on screen, but they have different types.
Input examples belong in the requirements. When a form field contains a number as text, decide whether the program should validate its format and convert it. Conversions deserve attention: changing a value can lose information about what the user supplied.
Distinguish missing from false
null can represent a missing value. false is a boolean value, while zero is a number. When asking whether an answer was supplied, each can mean something different. Strict comparison with === checks both type and value.
Run this example on PHP 8 or later. The outputs are bool(false) and bool(true). Notice that == allows type conversions.
<?php
var_dump("2" === 2);
var_dump("2" == 2);
Output
bool(false)
bool(true)
Explore the topic in Kodivo's lesson about null and the PHP comparison reference. Check examples against the PHP version running your code.
Tell the agent what data to expect
Instead of asking to “handle age”, write: “Age is optional. Missing information must remain null. Supplied input must be a non-negative integer. Do not convert an empty entry to zero.” The agent receives the meaning of the data and a case it needs to preserve.
Request examples for missing input, zero and text that is not a number. Explain in your own words what should happen in each case. This is also a useful way to refresh your understanding after time away from programming.