Learn/PHP/2. Values and variables
Lesson 2 of 15
Sign in

PHP, lesson 2

Values and variables

A variable is a name for a value you want to use later. In PHP every variable name starts with a dollar sign, and you create one simply by assigning to it:

There's no keyword such as let or var and no type to declare. Assigning again replaces the old value:

Variable names are case sensitive ($score and $Score are different) and may contain letters, digits and underscores, but can't start with a digit. Most modern PHP code uses $camelCase names. Choose names that describe the meaning, like $secondsLeft, not $x.

The basic types

2 exercises to try what you just read.

Exercise 1

Movie night tickets

Press Ctrl Enter or Check to test your code.

Exercise 2

Swap the cups

Press Ctrl Enter or Check to test your code.

Type
Example values
Used for
int0, -7, 545_000whole numbers
float3.14, -0.5, 2.0numbers with a fraction
string"hi", '42', ""text
booltrue, falseyes/no answers
nullnull"no value"

var_dump() shows a value together with its type, which makes it the best debugging tool you have. gettype() returns just the type name:

PHP converts between types automatically in many situations, which is called type juggling: "5" + 3 is 8. That's convenient, but it can hide mistakes, so it's better to convert on purpose with casts such as (int), (float) and (string).

Putting variables inside strings

Double-quoted strings replace variable names with their values. This is called interpolation:

Wrap the variable in {$ ... } whenever the text right after it would otherwise be read as part of the name, or when you use an array element or object property. Single-quoted strings never interpolate.

You can also build text with the . operator: "Total: " . $total.

Constants

A constant is a name for a value that never changes. Constants have no dollar sign and are written in capitals by convention. Define them with const at the top level of a file:

Trying to assign to a constant later is an error, which is exactly the point. Constants are not interpolated inside strings, so join them with . or pass them to echo separately. (define("NAME", value) also creates constants and is older; const is the usual choice.)

Swapping values

PHP can assign several variables at once with array destructuring. Because the right side is built first, this swaps two values without a temporary variable:

Further reading

  • Variables basics
  • Types
  • Constants
  • String interpolation

The starter reads a name (first line) and a number of seats (second line). A ticket costs 12. Define a constant TICKET_PRICE, compute the total and print one line in exactly this form:

Sample input:

Sample output:

The first input line is what's in the left cup, the second what's in the right cup. Swap them with one destructuring assignment, then print:

Sample input:

Sample output:

<?php

$city = "Lisbon";
$population = 545_000;     // underscores make big numbers readable
$averageTemp = 17.4;
$isCapital = true;
<?php

$score = 10;
$score = $score + 5;   // the right side is worked out first
echo $score, "\n";     // 15
<?php

var_dump(42);       // int(42)
var_dump("42");     // string(2) "42"
echo gettype(4.5), "\n";   // double  (PHP's historical name for float)
<?php

$item = "coffee";
$price = 2.5;
echo "One $item costs $price euros.\n";
echo "Two {$item}s, please.\n";   // braces mark where the name ends
<?php

const TAX_RATE = 0.23;
const MAX_PLAYERS = 4;

echo MAX_PLAYERS * 2, "\n";   // 8
<?php

$left = "cat";
$right = "dog";
[$left, $right] = [$right, $left];
echo "$left $right\n";   // dog cat
<name> booked <seats> seats for <total>.
Ana
3
Ana booked 3 seats for 36.
left: <left>
right: <right>
tea
juice
left: juice
right: tea