PHP Bits
PHP Bits
i) Notepad
ii) Notepad++
iv) PDT
a) Only iv)
b) i), ii), iii) and iv)
c) i), ii) and iii)
d) Only iii)
View Answer
Answer: b
Explanation: Any of the above editors can be used to type php code and run it.
5. Which of the following must be installed on your computer so as to run PHP script?
i) Adobe Dreamweaver
ii) XAMPP
iv) IIS
i) /?
ii) //
iii) #
iv) /* */
a) Only ii)
b) i), iii) and iv)
c) ii), iii) and iv)
d) Both ii) and iv)
View Answer
Answer: c
Explanation: /* */ can also be use to comment just a single line although it is used for
paragraphs. // and # are used only for single line comment.
8. Which of the following PHP statement/statements will store 111 in variable num?
i) int $num = 111; ii) int mum = 111; iii) $num = 111; iv) 111 = $num;
1. <?php
2. $num = 1;
3. $num1 = 2;
4. print $num . "+". $num1;
5. ?>
a) 3
b) 1+2
c) 1.+.2
d) Error
View Answer
Answer: b
Explanation: .(dot) is used to combine two parts of the statement. Example ($num .
“Hello World”) will output 1Hello World.
a) 3
b) 1+2
c) Error
d) 12
View Answer
Answer: a
Explanation: The numbers inside the double quotes are considered as integers and not
string, therefore the value 3 is printed and not 1+2.
5. Which one of the following is the right description for the method getMessage()?
a) Returns the message if it is passed to the constructor
b) Returns the message if it is passed to the class
c) Returns the message if it is passed to the file
d) Returns the message if it is passed to the object
View Answer
Answer: a
Explanation: The function getMessage() gets the exception message. It returns the
message if it is passed to the constructor.
6. You can extend the exception base class, but you cannot override any of the
preceding methods because the are declared as__________
a) protected
b) final
c) static
d) private
View Answer
Answer: b
Explanation: Marking a method as final prevents it from being overridden by a
subclass.
i) BadFunctionCallException
ii) BadMethodCallException
iii) LogicException
iv) DomainException
a) Only ii)
b) Only iii)
c) i), ii), iii) and iv)
d) Only iv)
View Answer
Answer: c
Explanation: There are total 13 types of predefined exceptions SPL provide access to.
BadFunctionCallException,BadMethodCallExceptio, LogicException, DomainException
are also included in the exceptions.
i) OutOfBoundException
ii) OutOfRangeException
iii) OverflowException
iv) UnderflowException
a) i)
b) i) and iii)
c) i) and ii)
d) i), ii), iii) and iv)
View Answer
Answer: d
Explanation: The exception is thrown if a value is not a valid key. This represents errors
that cannot be detected at compile time. OutOfBoundException,
OutOfRangeException, OverflowException, UnderflowException are valid exceptions in
PHP.
3. Which type of function call is used in line 8 in the following PHP code?
1. <?php
2. function calc($price, $tax)
3. {
4. $total = $price + $tax;
5. }
6. $pricetag = 15;
7. $taxtag = 3;
8. calc($pricetag, $taxtag);
9. ?>
a) Call By Value
b) Call By Reference
c) Default Argument Value
d) Type Hinting
View Answer
Answer: a
Explanation: If we call a function by value, we actually pass the values of the arguments
which are stored or copied into the formal parameters of the function. Hence, the
original values are unchanged only the parameters inside the function changes.
1. <?php
2. function calc($price, $tax="")
3. {
4. $total = $price + ($price * $tax);
5. echo "$total";
6. }
7. calc(42);
8. ?>
a) Error
b) 0
c) 42
d) 84
View Answer
Answer: c
Explanation: You can designate certain arguments as optional by placing them at the
end of the list and assigning them a default value of nothing.
i) function()
ii) €()
iii) .function()
iv) $function()
a) Only i)
b) Only ii)
c) i) and ii)
d) iii) and iv)
View Answer
Answer: b
Explanation: A valid function name can start with a letter or underscore, followed by
any number of letters, numbers, or underscores. According to the specified regular
expression ([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*), a function name like this one is
valid.
1. <?php
2. function a()
3. {
4. function b()
5. {
6. echo 'I am b';
7. }
8. echo 'I am a';
9. }
10. a();
11. a();
12. ?>
a) I am a
b) I am bI am a
c) Error
d) I am a Error
View Answer
Answer: a
Explanation: The output will be “I am a” as we are calling a(); so the statement outside
the block of function b() will be called.
a) I am b
b) I am bI am a
c) Error
d) I am a Error
View Answer
Answer: c
Explanation: The output will be Fatal error: Call to undefined function b(). You cannot
call a function which is inside a function without calling the outside function first. It
should be a(); then b();
1. <?php
2. $op2 = "blabla";
3. function foo($op1)
4. {
5. echo $op1;
6. echo $op2;
7. }
8. foo("hello");
9. ?>
a) helloblabla
b) Error
c) hello
d) helloblablablabla
View Answer
Answer: c
Explanation: If u want to put some variables in function that was not passed by it, you
must use “global”. Inside the function type global $op2.
a) hello
b) infinite loop
c) hihello
d) error
View Answer
Answer: b
Explanation: While condition always gives 1.
1.<?php
2.$i = "";
3.while ($i = 10)
4.{
5. print "hi";
6.}
7.print "hello";
8.?>
a) hello
b) infinite loop
c) hihello
d) error
View Answer
Answer: b
Explanation: While condition always gives 1.
1.<?php
2.$i = 5;
3.while (--$i > 0)
4.{
5. $i++; print $i; print "hello";
6.}
7.?>
a) 4hello4hello4hello4hello4hello…..infinite
b) 5hello5hello5hello5hello5hello…..infinite
c) no output
d) error
View Answer
Answer: b
Explanation: i is decremented in the while loop in the condition check and then
incremented back.
1.<?php
2.$i = 5;
3.while (--$i > 0 || ++$i)
4.{
5. print $i;
6.}
7.?>
a) 54321111111….infinitely
b) 555555555…infinitely
c) 54321
d) 5
View Answer
Answer: a
Explanation: As it is || operator the second expression is not evaluated till i becomes 1
then it goes into a loop.
1.<?php
2.$i = 0;
3.while(++$i || --$i)
4.{
5. print $i;
6.}
7.?>
a) 1234567891011121314….infinitely
b) 01234567891011121314…infinitely
c) 1
d) 0
View Answer
Answer: a
Explanation: As it is || operator the second expression is not evaluated and i is always
incremented, in the first case to 1.
1.<?php
2.$i = 0;
3.while (++$i && --$i)
4.{
5. print $i;
6.}
7.?>
a) 1234567891011121314….infinitely
b) 01234567891011121314…infinitely
c) no output
d) error
View Answer
Answer: c
Explanation: The first condition itself fails thus the loop exists.
1.<?php
2.$i = 0;
3.while ((--$i > ++$i) - 1)
4.{
5. print $i;
6.}
7.?>
a) 00000000000000000000….infinitely
b) -1-1-1-1-1-1-1-1-1-1…infinitely
c) no output
d) error
View Answer
Answer: a
Explanation: (–$i > ++$i) evaluates to 0 but -1 makes it enters the loop and prints i.
1.<?php
2.$i = 2;
3.while (++$i)
4.{
5. while ($i --> 0)
6. print $i;
7.}
8.?>
a) 210
b) 10
c) no output
d) infinite loop
View Answer
Answer: a
Explanation: The loop ends when i becomes 0.
1.<?php
2.$i = 2;
3.while (++$i)
4.{
5. while (--$i > 0)
6. print $i;
7.}
8.?>
a) 210
b) 10
c) no output
d) infinite loop
View Answer
Answer: d
Explanation: The loop never ends as i is always incremented and then decremented.
1.<?php
2.define("GREETING", "PHP is a scripting language", true);
3.echo GREETING;
4.echo "<br>"
5.echo GREETING;
6.?>
GREETING
GREEtING
c) GREETING
d)
View Answer
Answer: d
Explanation: Since the third parameter is true in define(“GREETING”, “PHP is a scripting
language”, true) is true GREETING becomes case insensitive.
2. What will be the output of the following PHP code?
1.<?php
2.define("GREETING", "PHP is a scripting language");
3.echo $GREETING;
4.?>
a) $GREETING
b) no output
c) PHP is a scripting language
d) GREETING
View Answer
Answer: b
Explanation: Constants do not need a $ before them, they are referenced by their
variable names itself.
1.<?php
2.define('GREETING_TEST', 'PHP is a scripting language', true);
3.echo GREETING_TESt;
4.$changing_variable = 'test';
5.echo constant('GREETING_' . strtoupper($changing_variable));
6.?>
a)
b) GREETING_TESt
c) PHP is a scripting language
d)
GREETING_TEST
View Answer
Answer: a
Explanation: echo constant(x) output x, and x here is the concatenation of GREETING_
and $changing variable with. operator.
4. What will be the output of the following PHP code?
1.<?php
2.class Constants
3.{
4. define('MIN_VALUE', '0.0');
5. define('MAX_VALUE', '1.0');
6. public static function getMinValue()
7. {
8. return self::MIN_VALUE;
9. }
10. public static function getMaxValue()
11. {
12. return self::MAX_VALUE;
13. }
14. }
15. echo Constants::getMinValue();
16. echo Constants::getMaxValue();
17. ?>
a) 0.01.0
b) 01
c) No output
d) ERROR
View Answer
5. What will be the output of the following PHP code?
1.<?php
2.define("__LINE__", "PHP is a scripting language");
3.echo __LINE__;
4.?>
1.<?php
2.define('IF', 42);
3.echo "IF: ", IF;
4.?>
a) IF:42
b) No output
c) IF:
d) ERROR
View Answer
Answer: d
Explanation: Keyword like IF cannot be used as constant names.
1.<?php
2.define("NEW_GOOD_NAME_CONSTANT", "I have a value");
3.define("OLD_BAD_NAME_CONSTANT", NEW_GOOD_NAME_CONSTANT);
4.
5.echo NEW_GOOD_NAME_CONSTANT;
6.echo OLD_BAD_NAME_CONSTANT;
7.?>
a) I have a value
b) I have a valueI have a value
c) ERROR
d) I have a valueNEW_GOO_NAME_CONSTANTS
View Answer
Answer: b
Explanation: Constants can be set as values for other constants.
1.<?php
2.define('GOOD_OCTAL', 0700);
3.define('BAD_OCTAL', 0600);
4.print GOOD_OCTAL;
5.print '<br>';
6.print BAD_OCTAL;
7.?>
a)
448
384
b)
0700
0800
c) ERROR
d) No output
View Answer
Answer: a
Explanation: Anything starting from 0 is evaluated as an octal.
1.<?php
2.define("VAR_NAME","test");
3.${VAR_NAME} = "value";
4.echo VAR_NAME;
5.echo ${VAR_NAME};
6.?>
a) test
b) testtest
c) testvalue
d) error, constant value cannot be changed
View Answer
Answer: c
Explanation: ${VAR_NAME} creates a new variable which is not same as VAR_NAME.
1.<?php
2.class myObject { }
3.define('myObject::CONSTANT', 'test');
4.echo myObject::CONSTANT;
5.?>
a) test
b) error
c) myObject::CONSTANT
d) no output
View Answer
Answer: b
Explanation: Class constants cannot be defined outside class.
a) 5 === 5
b) Error
c) 1
d) False
View Answer
Answer: c
Explanation: === operator returns 1 if $a and $b are equivalent and $a and $b have the
same type.
1. <?php
2. $num = 10;
3. echo 'What is her age? \n She is $num years old';
4. ?>
4. Which of the conditional statements is/are supported by PHP?
i) if statements
a) Only i)
b) i), ii) and iv)
c) ii), iii) and iv)
d) i), ii), iii) and iv)
View Answer
Answer: d
Explanation: All are conditional statements supported by PHP as all are used to
evaluate different conditions during a program and take decisions based on whether
these conditions evaluate to true of false.
1. <?php
2. $team = "arsenal";
3. switch ($team) {
4. case "manu":
5. echo "I love man u";
6. case "arsenal":
7. echo "I love arsenal";
8. case "manc":
9. echo "I love manc"; }
10. ?>
a) I love arsenal
b) Error
c) I love arsenalI love manc
d) I love arsenalI love mancI love manu
View Answer
Answer: c
Explanation: If a break statement isn’t present, all subsequent case blocks will execute
until a break statement is located.
i) for loop
a) i) and ii)
b) i), ii) and iii)
c) i), ii), iii) and iv)
d) Only iv)
View Answer
Answer: c
Explanation: All are supported looping statements in PHP as they can repeat the same
block of code a given number of times, or until a certain condition is met.
1. <?php
2. $user = array("Ashley", "Bale", "Shrek", "Blank");
3. for ($x=0; $x < count($user); $x++) {
4. if ($user[$x] == "Shrek") continue;
5. printf ($user[$x]);
6. }
7. ?>
a) AshleyBale
b) AshleyBaleBlank
c) ShrekBlank
d) Shrek
View Answer
Answer: b
Explanation: The continue statement causes execution of the current loop iteration to
end and commence at the beginning of the next iteration.
9. What will be the value of $a and $b after the function call in the following PHP code?
1. <?php
2. function doSomething( &$arg ) {
3. $return = $arg;
4. $arg += 1;
5. return $return;
6. }
7. $a = 3;
8. $b = doSomething( $a );
9. ?>
a) a is 3 and b is 4
b) a is 4 and b is 3
c) Both are 3
d) Both are 4
View Answer
10. Who is the father of PHP?
a) Rasmus Lerdorf
b) Willam Makepiece
c) Drek Kolkevi
d) List Barely
View Answer
Answer: a
Explanation: PHP was originally created by Rasmus Lerdorf in 1994.
1. How many functions does PHP offer for searching and modifying strings using Perl-
compatible regular expressions.
a) 7
b) 8
c) 9
d) 10
View Answer
Answer: b
Explanation: The functions are preg_filter(), preg_grep(), preg_match(), preg_match_all(),
preg_quote(), preg_replace(), preg_replace_callback(), and preg_split().
1. <?php
2. $foods = array("pasta", "steak", "fish", "potatoes");
3. $food = preg_grep("/^s/", $foods);
4. print_r($food);
5. ?>
a) Array ( [0] => pasta [1] => steak [2] => fish [3] => potatoes )
b) Array ( [3] => potatoes )
c) Array ( [1] => steak )
d) Array ( [0] => potatoes )
View Answer
Answer: c
Explanation: This function is used to search an array for foods beginning with s.
3. Say we have two compare two strings which of the following function/functions can
you use?
i) strcmp()
ii) strcasecmp()
iii) strspn()
iv) strcspn()
a) i) and ii)
b) iii) and iv)
c) only i)
d) i), ii), iii) and iv)
View Answer
Answer: d
Explanation: All of the functions mentioned above can be used to compare strings in
some or the other way.
4. Which one of the following functions will convert a string to all uppercase?
a) strtoupper()
b) uppercase()
c) str_uppercase()
d) struppercase()
View Answer
Answer: a
Explanation: Its prototype follows string strtoupper(string str).
1. <?php
2. $title = "O'malley wins the heavyweight championship!";
3. echo ucwords($title);
4. ?>
1. <?php
2. echo str_pad("Salad", 5)." is good.";
3. ?>
a) SaladSaladSaladSaladSalad is good
b) is good SaladSaladSaladSaladSalad
c) is good Salad
d) Salad is good
View Answer
Answer: d
Explanation: The str_pad() function pads a string with a specified number of characters.
7. Which one of the following functions can be used to concatenate array elements to
form a single delimited string?
a) explode()
b) implode()
c) concat()
d) concatenate()
View Answer
Answer: b
Explanation: None.
8. Which one of the following functions finds the last occurrence of a string, returning
its numerical position?
a) strlastpos()
b) strpos()
c) strlast()
d) strrpos()
View Answer
Answer: d
Explanation: None.
1. <?php
2. $author = "nachiketh@example.com";
3. $author = str_replace("a","@",$author);
4. echo "Contact the author of this article at $author.";
5. ?>
1. <?php
2. $url = "nachiketh@example.com";
3. echo ltrim(strstr($url, "@"),"@");
4. ?>
a) nachiketh@example.com
b) nachiketh
c) nachiketh@
d) example.com
View Answer
Answer: d
Explanation: The strstr() function returns the remainder of a string beginning with the
first occurrence of a predefined string.