PHP Variables, Echo/Print and Syntax Questions and Answers

Related Topics:

PHP Programming : Php Variables and Syntax

This is PHP Programming Sections with Various Multiple choice Questions on PHP Echo/Print, Php Variables and Syntax. Generally these questions are very helpful for Web Developer Interviews.
1. Which symbol is used to declare variable in php?
[A] $
[B] %
[C] #
[D] @

Answer: A. $

Explanation:

$ symbol is used for the declaring the variable in php example:- $a = 10;
2. How to declare a string variable in php?
[A] string str = “abc”;
[B] var string = “abc”;
[C] $str = “abc”;
[D] None of the above

Answer: C. $str = “abc”;

Explanation:

Every variable is start with $ in php whatever to declare any string, number, char etc.
3. Which are invalid variable ?
[A] $name1
[B] $_name
[C] $1name
[D] All of the above

Answer: C. $1name

Explanation:

Variable name can’t start with a number.
4. $name = ’foo’; 
$$name = ’bar’; 
Result of echo $foo;
[A] foo
[B] $name
[C] $$name
[D] bar

Answer: D. bar

Explanation:

foo = $name and $foo = $$name then $$name = bar
5. $name = ’123’; $$name = ’456’; Result of echo ${’123’};
[A] 456
[B] 123
[C] 456{123}
[D] None of above

Answer: A. 456

Explanation:

using curly braces we use the variable.
6. How to define constant in php ?
[A] $
[B] define
[C] #define
[D] All of the above

Answer: B. define

Explanation:

define is used for declare a constant example:- define(‘test’, ‘my web page’);
7. Select invalid constant.
[A] Define(‘email’,’test@test.com’);
[B] Define(‘1test’,’test@test.com’);
[C] Define(‘a’,’my first alphabet’);
[D] All of the above

Answer: B. Define(‘1test’,’test@test.com’);

Explanation:

Can’t define a constant start with the numeric number.
8. $a = 10; $b = $a; $b = 20; Result of echo $a;
[A] 10
[B] 20
[C] 30
[D] None of the above

Answer: A. 10

Explanation:

value of $a = 10. Copy in the variable $b but $a value remain same.
9. $a = 10; $b = &$a; // by reference $b = 20; Result of echo $a;
[A] 10
[B] 20
[C] 30
[D] None of the above

Answer: B. 20

Explanation:

& is used with variable for the reference of variable
10. Which are the global array in php ?
[A] $_POST
[B] $_GET
[C] $_REQUEST
[D] All of the above

Answer: D. All of the above

Explanation:

php provide the above global array for handle the request.

Comments: (Your feedback is valuable to us)
;