PHP Viva Questions For Second Lab Internal
PHP Viva Questions For Second Lab Internal
echo is faster and can take multiple arguments, while print returns a value (1) and accepts only
one argument.
It adds a newline character in the source code (not visually in browser output unless used in pre
tag or viewed in source).
Datatypes in PHP
1. What are the main data types supported in PHP?
PHP supports 8 primary data types: String, Integer, Float (Double), Boolean, Array, Object,
NULL, and Resource.
An integer has no decimal point, while a float (or double) has a decimal point, e.g., 10 (integer),
10.5 (float).
NULL is a special data type that represents a variable with no value or an empty value.
A resource is a special variable that holds a reference to an external resource like a database
connection or a file handle.
Keywords in PHP
1. What is a keyword in PHP, and can it be used as a variable name?
A keyword is a reserved word predefined by PHP for specific functionality, and it cannot be
used as a name for variables, functions, classes, or constants.
include throws a warning if the file doesn’t exist and continues execution; require throws a
fatal error and stops the script.
These are used for exception handling—try tests code, catch handles exceptions, and finally
runs code whether or not an exception occurred.
static allows a variable inside a function to retain its value between multiple calls.
A variable in PHP is a container for storing data, and it is declared using a dollar sign ($)
followed by the variable name.
Variable names must start with $, followed by a letter or underscore; they can contain letters,
numbers, and underscores, but not spaces or special characters, and are case-sensitive.
3. What is the difference between using single quotes and double quotes with variables in
strings?
In double quotes, variables are parsed (e.g., "Hello $name" becomes Hello Alice), while in
single quotes, they are treated as plain text ('Hello $name' stays 'Hello $name').
4. What is variable scope in PHP and what are its types?
Variable scope defines where a variable can be accessed. PHP has four types: Local, Global,
Static, and Function Parameters.
A constant is a value that cannot change once defined. It is defined using define("NAME",
value); or const NAME = value;, and does not use a dollar sign.
Magic constants change depending on their location in the script. Examples include __LINE__,
__FILE__, __FUNCTION__, and __CLASS__.
An operator in PHP is a symbol or series of symbols that performs an operation on one or more
operands to produce a result. For example, the + operator adds two operands.
An expression in PHP is any combination of values, variables, functions, and operators that
produces a result. For example, $sum = 4 + 5; is an expression where 4 and 5 are operands, and
+ is the operator.
Chained assignment in PHP allows multiple variables to be assigned the same value in a single
statement. For example, $a = $b = $c = 10; assigns the value 10 to $a, $b, and $c.
5. What is the difference between the ++$a (pre-increment) and $a++ (post-increment)?
++$a increments the value of $a before using it, while $a++ uses the current value of $a before
incrementing it. For example, ++$a returns the incremented value, while $a++ returns the
original value and then increments.
6. Explain operator precedence in PHP.
Operator precedence in PHP determines the order in which operators are evaluated. Operators
with higher precedence are evaluated first. For example, in the expression 2 + 4 * 3,
multiplication is performed first because it has higher precedence than addition.
7. What is the difference between the + operator and the . operator in PHP?
The + operator is used for arithmetic addition, while the . operator is used for string
concatenation. For example, "3" + 2.74 results in 5.74 (arithmetic), while "3" . 2.74 results in
"32.74" (concatenation).
Implicit type casting, or type juggling, is the automatic conversion of data types when
performing arithmetic operations. For example, when adding a string "10" and an integer 5,
PHP converts the string to an integer, resulting in 15.
The == operator compares values after converting them to the same type (type coercion), while
the === operator checks both the value and the type of the operands. For example:
10. How do you use the xor operator in PHP, and when is it useful?
The xor (exclusive OR) operator in PHP returns true if exactly one of the two conditions is true,
but not both. It is useful when you want to ensure that only one condition is true, not both.