Step 1 of 5 · Reading · ~4 min
Learn
Getting Started
Variables and Types
A variable in PHP is a name you stick onto a value. The name always starts with a dollar sign, you never declare it, and it exists from the moment you first assign to it.
<?php
$parcels = 3; // int
$weight = 1.75; // float
$city = "Lisbon"; // string
$express = true; // bool
$signed = null; // null — "no value yet"
How do I see what type something is?
var_dump() prints the type and the value. It is the fastest debugging tool in the language.
<?php
// continuing with the five variables assigned above
var_dump($parcels);
var_dump($weight);
var_dump($city);
var_dump($express);
var_dump($signed);
echo gettype($weight), "\n";
//> int(3)
//> float(1.75)
//> string(6) "Lisbon"
//> bool(true)
//> NULL
//> double
Two things worth noticing: string(6) also tells you the length, and gettype() calls a float a double — a leftover from C that trips everyone exactly once.
Types belong to values, not to names
Nothing stops you from reassigning a variable to a different type. $parcels = "many"; is perfectly legal. That flexibility is why PHP feels fast to write, and why a typo can travel a long way before it explodes.
Type juggling: the convenient part and the trap
PHP converts types automatically depending on the operator you use. The operator decides, not the values.
| Expression | Result | Why |
|---|---|---|
"5" + 3 | 8 | + is arithmetic, so the string is read as a number |
"5" . 3 | "53" | . is concatenation, so the number is read as text |
true + true | 2 | true counts as 1 |
"abc" + 3 | 3, plus a warning | Non-numeric text counts as 0 on PHP 7.4 |
That last row is the one to remember. On the PHP 7.4 grader this course uses, adding a word to a number gives you a warning and a silently wrong answer; PHP 8 turned the same expression into a hard TypeError. Either way it is a bug.
Rule of thumb: + means arithmetic, . means text. Reach for the wrong one and PHP will not stop you.
Reading input from standard input
Every exercise in this course feeds your program lines of text on standard input. The pattern is always the same, and the starter code hands it to you:
<?php
$a = (int)trim(fgets(STDIN));
$b = (int)trim(fgets(STDIN));
Read that from the inside out:
| Step | What it does |
|---|---|
fgets(STDIN) | Reads one line, including the trailing "\n" |
trim(...) | Strips that newline and any surrounding spaces |
(int)... | Converts the text "7" into the number 7 |
Skip the trim and your string still carries a newline on the end. Skip the (int) and you are holding text rather than a number — which matters enormously the moment you use +.
Casting explicitly
A cast is a type name in parentheses. It never throws; it just does its best with what it is given.
<?php
var_dump((int)"42");
var_dump((int)"42abc");
var_dump((int)"abc");
var_dump((float)"3.5kg");
var_dump((string)true);
var_dump((string)false);
//> int(42)
//> int(42)
//> int(0)
//> float(3.5)
//> string(1) "1"
//> string(0) ""
An (int) cast reads as many leading numeric characters as it can and then stops — so "42abc" gives 42, while "abc" gives 0. Note the last two lines too: true becomes "1", but false becomes the empty string, not "0". Echoing a boolean is a poor way to display it; use var_dump().
Constants
A constant holds a value that cannot be reassigned, and it carries no dollar sign:
<?php
const MAX_WEIGHT = 30;
define('DEPOT', 'Lisbon');
echo MAX_WEIGHT, " ", DEPOT, "\n";
//> 30 Lisbon
const is resolved at compile time and reads better; define() is the older runtime form. Both are global, and both are conventionally written in UPPER_SNAKE_CASE.
Your exercise
Sum Two Numbers reads two integers, one per line, and prints their sum and nothing else.
The starter already does the fgets / trim / (int) dance for you, so the whole job is one echo. The mistake the grader catches is reaching for . instead of +: with the first test's input of 7 and 5, concatenation prints 75 where the expected answer is 12. The hidden case feeds -3 and 3 and expects 0, so do not assume the inputs are positive and do not reach for abs(). Print the number on its own — no Sum: label — and finish the line with "\n".
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…