Basic Syntax and Structure
1.
Which of the following is the correct way to start a PHP block of code?
a) <php>
b) <script>
c) <?php
d) <?script
Answer: c) <?php
2.
How do you output text in PHP?
a) echo "Hello, World!";
b) print "Hello, World!";
c) printf("Hello, World!");
d) All of the above
Answer: d) All of the above
3.
What is the correct way to declare a variable in PHP?
a) $variable = 10;
b) int variable = 10;
c) declare variable = 10;
d) var variable = 10;
Answer: a) $variable = 10;
4.
What symbol is used to concatenate two strings in PHP?
a) +
b) &
c) .
d) ,
Answer: c) .
5.
Which of the following is the correct way to comment a single line in
PHP?
a) /* This is a comment */
b) // This is a comment
c) # This is a comment
d) Both b and c
1.
What will be the output of the following PHP code?
$a = 5;
$b = 10;
echo $a +
$b;
a) 15
b) 510
c) 5 + 10
d) 5-10
Answer: a) 15
2.
Which PHP function is used to include and evaluate a specified file?
a) include()
b) require()
c) include_once()
d) All of the above
Answer: d) All of the above
3.
What will the following PHP code snippet output?
$text =
"Hello, World!";
echo
strlen($text);
a) 13
b) 12
c) Hello, World!
d) Error
Answer: a) 13
4.
Which PHP operator is used for equality comparison?
a) ==
b) =
c) ===
d) Both a and c
Answer: d) Both a and c
5.
Which of the following will correctly output the value of a PHP variable
named $username?
a) echo $username;
b) print $username;
c) printf($username);
d) All of the above
Answer: d) All of the above
1.
What will be the output of the following PHP code?
$a = 7;
$b = 3;
$result = $a
% $b;
echo
$result;
a) 2
b) 1
c) 0
d) 3
Answer: b) 1
2.
How can you define a constant in PHP?
a) define('CONSTANT_NAME', 'value');
b) const CONSTANT_NAME = 'value';
c) Both a and b
d) constant('CONSTANT_NAME', 'value');
Answer: c) Both a and b
3.
Which operator is used for string concatenation in PHP?
a) +
b) .
c) &
d) :
Answer: b) .
4.
What is the output of the following PHP code?
$num = 5;
$num++;
echo $num;
a) 5
b) 6
c) 7
d) Error
Answer: b) 6
5.
What will be the output of the following PHP code?
$text =
"PHP";
$text .=
" is fun!";
echo $text;
a) PHP
b) PHP is fun!
c) PHPisfun!
d) Error
1.
What is the correct way to declare a constant in PHP using the const
keyword?
a) const PI = 3.14;
b) define('PI', 3.14);
c) const PI = '3.14';
d) Both a and b
Answer: a) const PI = 3.14;
2.
What will be the output of the following PHP code?
$x = 10;
$y =
"10";
var_dump($x
== $y);
a)
bool(true)
b) bool(false)
c) int(10)
d) string(2)
Answer: a) bool(true)
3.
Which operator is used for strict comparison in PHP, which checks both
the value and the type of the operands?
a) ==
b) ===
c) !==
d) !=
Answer: b) ===
4.
What will be the result of the following PHP code?
$text =
"Hello";
$text .=
" World!";
echo $text;
a) Hello
World!
b) HelloWorld!
c) Hello World
d) Hello
Answer: a) Hello World!
5.
Which PHP function is used to check whether a file or directory exists?
a) file_exists()
b) is_file()
c) is_dir()
d) exists()
Answer: a) file_exists()
1.
What will be the result of the following PHP code?
$a = 5;
$b = 3;
$result = $a
** $b;
echo
$result;
a) 125
b) 15
c) 8
d) Error
Answer: a) 125
2.
How do you correctly define an associative array in PHP?
a) $array = array("key" => "value");
b) $array = ["key" => "value"];
c) $array = array("key", "value");
d) Both a and b
Answer: d) Both a and b
3.
Which PHP function is used to remove whitespace from the beginning and
end of a string?
a) trim()
b) strip()
c) clean()
d) sanitize()
Answer: a) trim()
4.
What will be the output of the following PHP code?
$arr = [1,
2, 3];
echo
count($arr);
a) 3
b) 2
c) 1
d) Error
Answer: a) 3
5.
Which of the following is used to access a class property in PHP?
a) ->
b) .
c) ::
d) []
Answer: a) ->
1.
What will be the output of the following PHP code?
$num = 1;
while ($num
<= 5) {
echo $num++;
}
a) 12345
b) 12346
c) 23456
d) 1234
Answer: a) 12345
2.
What is the correct way to define a constant array in PHP?
a) const MY_ARRAY = [1, 2, 3];
b) define('MY_ARRAY', [1, 2, 3]);
c) define('MY_ARRAY', array(1, 2, 3));
d) Both b and c
Answer: d) Both b and c
3.
Which function in PHP is used to split a string by a specified delimiter?
a) split()
b) explode()
c) implode()
d) str_split()
Answer: b) explode()
4.
Which of the following methods is used to access the value of a key in an
associative array?
a) $array(key)
b) $array->key
c) $array['key']
d) $array.key
Answer: c) $array['key']
5.
What is the correct way to comment a block of code in PHP?
a) /* Comment here */
b) <!-- Comment here -->
c) # Comment here
d) // Comment here
Answer: a) /* Comment here */
1.
What will be the result of the following PHP code?
$a =
"5";
$b = 10;
$result = $a
+ $b;
echo
$result;
a) 15
b) 510
c) 5
d) Error
Answer: a) 15
2.
Which PHP function is used to remove whitespace from the beginning and
end of a string?
a) trim()
b) strip()
c) clean()
d) sanitize()
Answer: a) trim()
3.
How do you define a variable in PHP?
a) $variableName = value;
b) var variableName = value;
c) variableName = value;
d) def variableName = value;
Answer: a) $variableName = value;
4.
Which of the following will correctly display the value of a PHP variable
named $age in a browser?
a) echo $age;
b) print($age);
c) printf($age);
d) All of the above
Answer: d) All of the above
5.
What will be the output of the following PHP code?
$arr =
array(1, 2, 3);
$arr[] = 4;
echo
count($arr);
a) 3
b) 4
c) 5
d) Error
1.
What will be the output of the following PHP code?
$x = 10;
$y = 20;
$x += $y;
echo $x;
a) 10
b) 20
c) 30
d) Error
Answer: c) 30
2.
How do you access a static property of a class in PHP?
a) ClassName::property
b) ClassName->property
c) ClassName::get_property()
d) ClassName->get_property()
Answer: a) ClassName::property
3.
What will be the output of the following PHP code?
$a =
"PHP";
$b = "
is awesome!";
$result = $a
. $b;
echo
strlen($result);
a) 10
b) 15
c) 12
d) 14
Answer: d) 14
4.
Which function would you use to check if a variable is of type integer in
PHP?
a) is_int()
b) is_integer()
c) is_numeric()
d) check_integer()
Answer: a) is_int()
5.
What is the correct way to declare a PHP array with keys and values?
a) $array = array("key1" => "value1", "key2"
=> "value2");
b) $array = ["key1" => "value1", "key2" =>
"value2"];
c) Both a and b
d) $array = ("key1" => "value1", "key2" =>
"value2");