0
0
PHPprogramming~10 mins

Why state management is needed in PHP - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a session in PHP.

PHP
<?php
[1]();
?>
Drag options to blanks, or click blank then click option'
Ainit_session
Bstart_session
Cbegin_session
Dsession_start
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect function names like start_session() or init_session() which do not exist.
2fill in blank
medium

Complete the code to store a user's name in the session.

PHP
<?php
session_start();
$_SESSION['username'] = [1];
?>
Drag options to blanks, or click blank then click option'
AJohn
B"John"
C'username'
Dusername
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the string value, causing syntax errors.
3fill in blank
hard

Fix the error in the code to correctly check if a session variable exists.

PHP
<?php
session_start();
if (isset([1]['username'])) {
    echo "User is logged in.";
}
?>
Drag options to blanks, or click blank then click option'
A$_SESSION
B$_session
CSESSION
D$SESSION
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or missing the underscore in the session superglobal variable.
4fill in blank
hard

Fill both blanks to correctly remove a session variable and destroy the session.

PHP
<?php
session_start();
unset([1]['username']);
session_[2]();
?>
Drag options to blanks, or click blank then click option'
A$_SESSION
Bdestroy
C$SESSION
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names or wrong function names like session_close().
5fill in blank
hard

Fill all three blanks to create a session-based counter that increases on each page load.

PHP
<?php
session_start();
if (!isset([1]['count'])) {
    [1]['count'] = 1;
} else {
    [1]['count'] [2] [3] 1;
}
echo "Page views: " . [1]['count'];
?>
Drag options to blanks, or click blank then click option'
A$_SESSION
B+=
C+
D$SESSION
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names or operators like =+ which is incorrect.