Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print "Hello, World!" in PHP.
PHP
<?php
echo [1];
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string.
Using print() inside echo.
✗ Incorrect
The echo statement in PHP outputs the string. The string must be in quotes.
2fill in blank
mediumComplete the code to declare a variable named $name with the value "PHP".
PHP
<?php [1] = "PHP"; ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the $ before the variable name.
Using JavaScript style variable declarations.
✗ Incorrect
In PHP, variables start with a dollar sign ($).
3fill in blank
hardFix the error in the code to correctly concatenate strings in PHP.
PHP
<?php $name = "PHP"; echo "I love " [1] $name; ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of . for string concatenation.
Using commas inside echo without quotes.
✗ Incorrect
In PHP, the dot (.) operator is used to join strings.
4fill in blank
hardFill both blanks to create an associative array with keys 'language' and 'year'.
PHP
<?php $info = array([1] => "PHP", [2] => 1995); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting quotes around keys.
Using variables instead of strings as keys.
✗ Incorrect
Array keys should be strings in quotes.
5fill in blank
hardFill all three blanks to loop through the array and print each key and value.
PHP
<?php $info = array("language" => "PHP", "year" => 1995); foreach([1] as [2] => [3]) { echo "$key: $value\n"; } ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names in foreach.
Omitting the => in foreach.
✗ Incorrect
The foreach loop uses the array variable, then key and value variables.