Challenge your understanding of PHP fundamentals by exploring variables, common operators, and core syntax rules. Efficiently review PHP basics for beginners with practical, scenario-based questions designed for foundational learning.
Which of the following correctly declares a variable assigned with the number 10 in PHP?
Explanation: In PHP, variables are declared with a dollar sign followed by the name, so '$number = 10;' is correct. The second option uses incorrect syntax by including 'int' before the variable, which is not used in PHP variable declarations. The third and fourth options use syntax from other programming languages and are not valid in PHP.
What operator is used to join two strings together in PHP, such as joining 'Hello' and 'World'?
Explanation: The dot (period) operator is used to concatenate strings in PHP. The plus sign is for arithmetic addition, not string joining. The ampersand symbol is not for concatenation, and the asterisk performs multiplication, so they are all incorrect in this context.
Which of these is a correct way to write a single-line comment in PHP?
Explanation: In PHP, double slashes '//' start a single-line comment. The second and third options use symbols that are not recognized as comment delimiters in PHP. The fourth option starts a multi-line comment, not a single-line one.
Are variable names in PHP case-sensitive, for example, is $Value considered different from $value?
Explanation: Variable names in PHP are case-sensitive, so $Value and $value would be treated as separate variables. The other options are incorrect because case sensitivity applies to all variables, not just functions or built-in variables.
What will be the value of $a after executing the code: $a = 5; $a += 3;?
Explanation: '$a += 3' adds 3 to the original value of $a, which was 5, resulting in 8. Choosing 3 ignores the initial value, while 5 would be correct only if the addition didn't occur. The option 53 incorrectly treats the values as string concatenation.
Which option correctly outputs the text Hello World to the browser in PHP?
Explanation: The correct way to output text in PHP is using 'echo' followed by the string and a semicolon. 'Echo' with curly braces is invalid syntax, and 'write' is not a PHP command. 'print' is a valid alternative but requires parentheses and a semicolon, but the option listed is missing one.
Which symbol must always terminate each PHP statement?
Explanation: Each PHP statement ends with a semicolon. Colons, commas, and periods are used for other purposes in PHP syntax but do not terminate statements. Omitting the semicolon may cause errors.
Given $x = 12 and $y = 4, what does the expression $x / $y evaluate to?
Explanation: Dividing 12 by 4 results in 3, so '3' is correct. '16' is the sum of 12 and 4, '48' is their product, and '8' is their difference, none of which match the division operation.
What is the value of an uninitialized variable in PHP when used in arithmetic, such as $total = $number + 5; where $number is not previously set?
Explanation: In PHP, an uninitialized variable used in arithmetic is assumed to have a value of zero. 'null' is a different type, 'false' is a boolean, and 'undefined' is not a value recognized by PHP. This behavior helps avoid errors but can lead to unexpected results if variables are not properly initialized.
Which variable name is valid according to PHP's naming rules?
Explanation: PHP variable names must start with a dollar sign and a letter or underscore, followed by any combination of letters, numbers, or underscores, making '$my_var123' correct. 'my-var' is missing the dollar sign and uses a dash, '$123sample' starts with a number after the dollar sign, and '$var.name' uses an invalid period character.