0% found this document useful (0 votes)
11 views4 pages

PHP Viva Questions For Second Lab Internal

The document contains a series of PHP viva questions covering topics such as sending data to the browser, data types, keywords, variables, constants, operators, and expressions. Key differences between functions like echo and print, as well as explanations of variable scope and operator precedence, are highlighted. It serves as a study guide for understanding fundamental PHP concepts and syntax.

Uploaded by

suraiyareeha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

PHP Viva Questions For Second Lab Internal

The document contains a series of PHP viva questions covering topics such as sending data to the browser, data types, keywords, variables, constants, operators, and expressions. Key differences between functions like echo and print, as well as explanations of variable scope and operator precedence, are highlighted. It serves as a study guide for understanding fundamental PHP concepts and syntax.

Uploaded by

suraiyareeha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PHP Viva Questions for Second Lab Internal

Sending Data to your Web Browser


1. What is the difference between the echo and print statements in PHP?

echo is faster and can take multiple arguments, while print returns a value (1) and accepts only
one argument.

2. Why is echo generally preferred over print in PHP?

Because it is faster, more commonly used, and supports multiple arguments

3. What does the escape sequence \n do in PHP?

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.

2. What is a string in PHP?

A string is a sequence of characters enclosed in single or double quotes.

3. What is the difference between float and integer in PHP?

An integer has no decimal point, while a float (or double) has a decimal point, e.g., 10 (integer),
10.5 (float).

4. What is NULL in PHP?

NULL is a special data type that represents a variable with no value or an empty value.

5. What is a resource data type in PHP?

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.

2. Are PHP keywords case-sensitive?

No, PHP keywords are case-insensitive.

3. What’s the difference between include and require in PHP?

include throws a warning if the file doesn’t exist and continues execution; require throws a
fatal error and stops the script.

4. What are try, catch, and finally used for in PHP?

These are used for exception handling—try tests code, catch handles exceptions, and finally
runs code whether or not an exception occurred.

5. What is the use of the static keyword in PHP?

static allows a variable inside a function to retain its value between multiple calls.

Using Variables and Constants in PHP


1. What is a variable in PHP and how is it declared?

A variable in PHP is a container for storing data, and it is declared using a dollar sign ($)
followed by the variable name.

2. What are the rules for naming variables in PHP?

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.

5. What is a constant in PHP and how is it defined?

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.

6. What are magic constants in PHP? Give examples.

Magic constants change depending on their location in the script. Examples include __LINE__,
__FILE__, __FUNCTION__, and __CLASS__.

Operators and Expressions in PHP


1. What is an operator in PHP?

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.

2. What is an expression in PHP?

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.

3. What is chained assignment in PHP?

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.

4. What are compound assignment operators in PHP?

Compound assignment operators combine an arithmetic, bitwise, or string operation with


assignment. For example, +=, -=, *=, /=, and .= are compound assignment operators.

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).

8. What is implicit type casting in PHP?

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.

9. What is the difference between == and === in PHP?

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:

• 5 == "5" returns true because of type coercion.


• 5 === "5" returns false because the types (integer vs. string) are different.

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.

You might also like