PHP Operators Questions and Answers with Examples

Related Topics:

PHP Programming : PHP Operators

PHP Operators MCQs With Answers and Examples. Operators are used to perform operations on variables and values. Whats new in PHP 5 Operators?
1. How many operator groups in PHP?
[A] 6
[B] 8
[C] 10
[D] 12

Answer: D. 12

Explanation:

12 operator groups: Operator Precedence, Arithmetic Operators, Assignment Operators, Bit wise Operators, Comparison Operators, Error Control Operators, Execution Operators, Incrementing/Decrementing Operators, Logical Operators, String Operators, Array Operators, Type Operators.
2. How many Arithmetic Operators are in PHP?
[A] 6
[B] 3
[C] 5
[D] 7

Answer: D. 7

Explanation:

7 arithmetic operators: Negation, Addition, Subtraction, Multiplication, Division, Modulus, Exponentiation.
3. Which is the Negation operator in PHP?
[A] -$a
[B] $a / $b
[C] $a % $b
[D] $a ** $b

Answer: A. -$a

Explanation:

opposite of $a. It called negation
4. Which is the Assignment operator in PHP?
[A] +=
[B] =
[C] *=
[D] All of the above

Answer: D. All of the above

Explanation:

The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right. All of the sign comes under to the assignment operator, such that +=, -=, *=, /=, %=
5. How to Use Ternary Operator in PHP
[A] if(2 == 1) ? “yes” : “no”;
[B] a+=b
[C] a+b
[D] None of the above

Answer: A. if(2 == 1) ? “yes” : “no”;

Explanation:

There are three operators that are complicated enough to get their own section, of which the first is the ternary operator. It is called the ternary operator because it takes three operands - a condition, a result for true, and a result for false.
<?php
$agestr = ($age < 16) ? 'child' : 'adult';
?>
is same as:
<?php
if ($age < 16) {
$agestr = 'child';
} else {
$agestr = 'adult';
}
?>
6. Which sign is used in PHP for the bitwise operator?
[A] <<
[B] ^
[C] >>
[D] All of the above

Answer: D. All of the above

Explanation:

Bitwise operators allow evaluation and manipulation of specific bits within an integer.
7. Which operator is used for compare two values?
[A] Assignment Operator
[B] Bitwise Operator
[C] Arithmetic Operators
[D] Comparison Operators

Answer: D. Comparison Operators

Explanation:

The PHP comparison operators are used to compare two values (number or string):

OperatorNameExample
==Equal$x == $y
===Identical$x === $y
!=Not equal$x != $y
<>Not equal$x <> $y
!==Not identical$x !== $y
>Greater than$x > $y
<Less than$x < $y
>=Greater than or equal to$x >= $y
<=Less than or equal to$x <= $y
8. Which of the following PHP Operator can control Errors?
[A] $$
[B] %%
[C] @
[D] #

Answer: C. @

Explanation:

When we prepend (@) to an expression in PHP, any error messages that might be generated by that expression will be ignored.

Example:
<?php
echo @$array['variable'];
?>

When "variable" key is now set in array "$array" then it will ignore undefined error and show empty string "".
9. How we can do Pre-Increment of a Variable?
[A] $a++
[B] $a+$a
[C] ++$a
[D] None of above

Answer: C. ++$a

Explanation:

Because pre-increment show the symbol front of variable.
10. Which of the following is PHP Logical Operator?
[A] AND
[B] OR
[C] XOR
[D] ALL of the above

Answer: D. ALL of the above

Explanation:

The PHP logical operators are used to combine conditional statements.
OperatorNameExample
andAnd$x and $y
orOr$x or $y
xorXor$x xor $y
&&And$x && $y
||Or$x || $y
!Not!$x

PHP Operators Programs/Functions


Comments: (Your feedback is valuable to us)
;