Summary - Variables
Combine variable declaration, assignment and checks to build a user info card.
Summary: Variables
In this group you built the foundation of variables. You learned:
- declaring variables with
$and=. - naming principles and case sensitivity.
- reassignment, increasing and decreasing with
++and--. - combining with
.and doing arithmetic with numbers. - checking variables with
isset()andunset().
Now you will combine all of this into a user info card.
Exercise Build a user info card
Assign "Alex" to $userName and "Berlin" to $city. On the first line, print Alex - Berlin using $userName . " - " . $city . "\n". Remove $city with unset($city). On the second line, print "City set: " . isset($city) . "\n".
Show example solution
On the first line combine $userName and $city with a dot; then unset, then isset on the second line.
Example solution:
<?php
$userName = "Alex";
$city = "Berlin";
echo $userName . " - " . $city . "\n";
unset($city);
echo "City set: " . isset($city) . "\n";
Test yourself
-
Which function is used to delete a variable?
- unset()
- isset()
- delete()
- remove()
Show answer
unset() — Correct! unset() completely destroys a variable.
-
Declare the counter
____visitors = 0;Show answer
$visitors = 0; -
Match each concept to its description
- $ sign
- starts a variable name
- ++
- increases by one
- .
- combines two values
- unset()
- deletes the variable