0% found this document useful (0 votes)
346 views

PHP Bits

PHP is a widely-used open source scripting language that is especially suited for web development and can be embedded into HTML. The document provided information about PHP including: 1. It defined what PHP stands for and the correct file extension for PHP files. PHP stands for Hypertext Preprocessor and the default file extension is .php. 2. It covered PHP syntax and tags, and listed code editors that can be used for PHP development. The correct PHP syntax uses <?php ?> tags. Notepad++, Adobe Dreamweaver, and PDT are all valid PHP code editors. 3. It explained what is required to run PHP scripts, including installing a web server like Apache or IIS

Uploaded by

karunakar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
346 views

PHP Bits

PHP is a widely-used open source scripting language that is especially suited for web development and can be embedded into HTML. The document provided information about PHP including: 1. It defined what PHP stands for and the correct file extension for PHP files. PHP stands for Hypertext Preprocessor and the default file extension is .php. 2. It covered PHP syntax and tags, and listed code editors that can be used for PHP development. The correct PHP syntax uses <?php ?> tags. Notepad++, Adobe Dreamweaver, and PDT are all valid PHP code editors. 3. It explained what is required to run PHP scripts, including installing a web server like Apache or IIS

Uploaded by

karunakar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

1. What does PHP stand for?

i) Personal Home Page

ii) Hypertext Preprocessor

iii) Pretext Hypertext Processor

iv) Preprocessor Home Page

a) Both i) and iii)


b) Both ii) and iv)
c) Only ii)
d) Both i) and ii)
View Answer
Answer: d
Explanation: PHP previously stood for Personal Home Page now stands for Hypertext
Preprocessor.

2. PHP files have a default file extension of_______


a) .html
b) .xml
c) .php
d) .ph
View Answer
Answer: c
Explanation: To run a php file on the server, it should be saved as AnyName.php

3. What should be the correct syntax to write a PHP code?


a) < php >
b) < ? php ?>
c) <? ?>
d) <?php ?>
View Answer
Answer: c
Explanation: Every section of PHP code starts and ends by turning on and off PHP tags
to let the server know that it needs to execute the PHP in between them.

4. Which of the following is/are a PHP code editor?

i) Notepad
ii) Notepad++

iii) Adobe Dreamweaver

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

iii) Apache and PHP

iv) IIS

a) i), ii), iii) and iv)


b) Only ii)
c) ii) and iii)
d) ii), iii) and iv)
View Answer
Answer: d
Explanation: To run PHP code you need to have PHP and a web server, both IIS and
Apache are web servers. You can choose either one according to your platform.

6. Which version of PHP introduced Try/catch Exception?


a) PHP 4
b) PHP 5
c) PHP 6
d) PHP 5 and later
View Answer
Answer: d
Explanation: PHP 5 version and later versions added support for Exception Handling.
7. How should we add a single line comment in our PHP code?

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;

a) Both i) and ii)


b) i), ii), iii) and iv)
c) Only iii)
d) Only i)
View Answer
Answer: c
Explanation: You need not specify the datatype in php.

9. What will be the output of the following PHP code?

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.

10. What will be the output of the following PHP code?

1. <?php $num = "1"; $num1 = "2"; print $num+$num1; ?>

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.

1. Which version of PHP was added with Exception handling?


a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 6
View Answer
Answer: b
Explanation: Exception handling was added to PHP with the version 5 release, and
further enhanced with version 5.3.

2. How many methods are available for the exception class?


a) 5
b) 6
c) 7
d) 8
View Answer
Answer: c
Explanation: The seven methods are: getCode(), getFile(), getLine(), getMessage(),
getPrevious(), getTrace(), getTraceAsString().

3. Which version added the method getPrevious()?


a) PHP 4
b) PHP 5
c) PHP 5.1
d) PHP 5.3
View Answer
Answer: d
Explanation: The function getPrevious() returns previous exception.

4. Which of the following statements invoke the exception class?


a) throws new Exception();
b) throw new Exception();
c) new Exception();
d) new throws Exception();
View Answer
Answer: b
Explanation: throw new Exception(); trigger an exception and each “throw” must have
at least one “catch”.

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.

7. What does SPL stand for?


a) Standard PHP Library
b) Source PHP Library
c) Standard PHP List
d) Source PHP List
View Answer
Answer: a
Explanation: The standard PHP library(SPL) extends PHP by offering ready-made
solutions to commonplace tasks such as file access, iteration of various sorts etc.

8. How many predefined exceptions does SPL provide access to?


a) 13
b) 14
c) 15
d) 16
View Answer
Answer: a
Explanation: It provides 13 exceptions: BadFunctionCallException,
BadMethodCallException, DomainException, InvalidArgumentException,
LengthException, LogicException, OutOfBoundsException, OutOfRangeException,
OverflowException, RangeException, RuntimeException, UnderflowException,
UnexpectedValueException.

9. Which of the following is/are an exception?

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.

10. Which of the following is/are an exception?

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.

4. What will be the output of the following PHP code?

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.

5. Which of the following are valid function names?

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.

6. What will be the output of the following PHP code?

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.

7. What will be the output of the following PHP code?

1. <?php function a() { function b()


2. {
3. echo 'I am b';
4. }
5. echo 'I am a';
6. }
7. b();
8. a();
9. ?>

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();

8. What will be the output of the following PHP code?

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.

1. What will be the output of the following PHP code?


1.<?php
2.$i = 0;
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.

2. What will be the output of the following PHP code?

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.

3. What will be the output of the following PHP code?

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.

5. What will be the output of the following PHP code?

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.

6. What will be the output of the following PHP code?

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.

7. What will be the output of the following PHP code?

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.

8. What will be the output of the following PHP code?

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.

9. What will be the output of the following PHP code?

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.

10. What will be the output of the following PHP code?

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. What will be the output of the following PHP code?

1.<?php
2.define("GREETING", "PHP is a scripting language", true);
3.echo GREETING;
4.echo "<br>"
5.echo GREETING;
6.?>

a) PHP is a scripting language


b)

GREETING

GREEtING

c) GREETING
d)

PHP is a scripting language

PHP is a scripting language

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.

3. What will be the output of the following PHP code?

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)

PHP is a scripting language

PHP is a scripting language

b) GREETING_TESt
c) PHP is a scripting language
d)

PHP is a scripting language

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

a) PHP is a scripting language


b) __LINE__
c) 2
d) ERROR
View Answer
Answer: c
Explanation: __LINE__ is a magical constant that gives the current line number and
cannot be used a variable/constant name.
6. What will be the output of the following PHP code?

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.

7. What will be the output of the following PHP code?

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.

8. What will be the output of the following PHP code?

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.

9. What will be the output of the following PHP code?

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.

10. What will be the output of the following PHP code?

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.

1. What will be the output of the following PHP code?


1. <?php
2. $a = 5;
3. $b = 5;
4. echo ($a === $b);
5. ?>

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.

2. Which of the below symbols is a newline character?


a) \r
b) \n
c) /n
d) /r
View Answer
Answer: b
Explanation: PHP treats \n as a newline character.

3. What will be the output of the following PHP code?

1. <?php
2. $num = 10;
3. echo 'What is her age? \n She is $num years old';
4. ?>

a) What is her age? \n She is $num years old


b)

What is her age?

She is $num years old

c) What is her age? She is 10 years old


d)

What is her age?

She is 10 years old


View Answer
Answer: a
Explanation: When a string is enclosed within single quotes both variables and escape
sequences will not be interpreted when the string is parsed.

 
 
4. Which of the conditional statements is/are supported by PHP?

i) if statements

ii) if-else statements

iii) if-elseif statements

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

5. What will be the output of the following PHP code?

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.

6. Which of the looping statements is/are supported by PHP?

i) for loop

ii) while loop

iii) do-while loop

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

7. What will be the output of the following PHP code?

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.

8. If $a = 12 what will be returned when ($a == 12) ? 5 : 1 is executed?


a) 12
b) 1
c) Error
d) 5
View Answer
Answer: d
Explanation: ?: is known as ternary operator. If condition is true then the part just after
the ? is executed else the part after : .

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

2. What will be the output of the following PHP code?

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

5. What will be the output of the following PHP code?

1. <?php
2. $title = "O'malley wins the heavyweight championship!";
3. echo ucwords($title);
4. ?>

a) O’Malley Wins The Heavyweight Championship!


b) O’malley Wins The Heavyweight Championship!
c) O’Malley wins the heavyweight championship!
d) o’malley wins the heavyweight championship!
View Answer
Answer: d
Explanation: The ucwords() function capitalizes the first letter of each word in a string.
Its prototype follows: string ucwords(string str).

6. What will be the output of the following PHP code?

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.

9. What will be the output of the following PHP code?

1. <?php
2. $author = "nachiketh@example.com";
3. $author = str_replace("a","@",$author);
4. echo "Contact the author of this article at $author.";
5. ?>

a) Contact the author of this article at nachiketh@ex@mple.com


b) Cont@ct the @uthor of this @rticle @t n@chiketh@ex@mple.com
c) Contact the author of this article at n@chiketh@ex@mple.com
d) Error
View Answer
Answer: c
Explanation: The str_replace() function case sensitively replaces all instances of a string
with another.

10. What will be the output of the following PHP code?

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.

You might also like