0
0
PHPprogramming~10 mins

Why variables do not persist between requests 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'
Asession_start
Bbegin_session
Cstart_session
Dinit_session
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong function name like start_session()
Forgetting the parentheses () after the function name
2fill in blank
medium

Complete the code to store a value in a session variable.

PHP
<?php
session_start();
$_SESSION[[1]] = 'blue';
?>
Drag options to blanks, or click blank then click option'
A'_color_'
Bcolor
C'color'
D"color"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the session key
Using double quotes instead of single quotes (both work but single quotes are common)
3fill in blank
hard

Fix the error in the code to access a session variable.

PHP
<?php
session_start();
echo $_SESSION[1];
?>
Drag options to blanks, or click blank then click option'
A[color]
B(color)
C{color}
D['color']
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or curly braces instead of square brackets
Not quoting the key inside the brackets
4fill in blank
hard

Fill both blanks to check if a session variable exists and then print it.

PHP
<?php
session_start();
if (isset($_SESSION[1])) {
    echo $_SESSION[2];
}
?>
Drag options to blanks, or click blank then click option'
A['user']
B['username']
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys in the two blanks
Not quoting the keys
5fill in blank
hard

Fill all three blanks to correctly start a session, set a variable, and then print it.

PHP
<?php
[1]();
$_SESSION[2] = 'admin';
echo $_SESSION[3];
?>
Drag options to blanks, or click blank then click option'
Asession_start
B['role']
Dstart_session
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong function to start session
Using different keys for setting and getting session variable