Beruflich Dokumente
Kultur Dokumente
Name:
………………………………………………………………………………………………….
1. Which two predefined variables are used to retrieve information from forms?
a. A. $GET & $SET c. C. $__GET & $__SET
b. B. $_GET & $_SET d. D. GET & SET
2. When you use the $_GET variable to collect data, the data is visible to..
a. A. none c. C. everyone
b. B. only you d. D. selected few
3. When you use the $_POST variable to collect data, the data is visible to..
e. A. none g. C. everyone
f. B. only you h. D. selected few
4. Which of the following are correct ways of creating an array?
(i) state[0] = “karnataka”;
(ii) $state[] = array(“karnataka”);
(iii) $state[0] = “karnataka”;
(iv) $state = array(“karnataka”);
A. (iii) and (iv) C. Only (i)
B. (ii) and (iii) D. (ii), (iii) and (iv)
Question (2): What will be the output of the following PHP code? (4.0 Marks)
a.
< ?php
function a()
{
function b()
{
echo 'I am b';
}
echo 'I am a';
}
b();
a();
?>
Answer: Error
Explanation:
This will be the output- Fatal error: Call to undefined function b(). You cannot call a function which is
inside a function without calling the outside function.
b.
< ?php
$op2 = "blabla";
function foo($op1)
{
echo $op1;
echo $op2;
}
foo("hello");
?>
Answer: hello
Explanation:
If you want to put some variables in function that was not passed by it, you must use “global”. Inside the
function type global $op2.
c.
< ?php
function calc($price, $tax="")
{
$total = $price + ($price * $tax);
echo "$total";
}
calc(42);
?>
Answer: 42
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.
d.
< ?php
$states = array("karnataka" => array
( "population" => "11,35,000", "captial" => "Bangalore"),
"Tamil Nadu" => array( "population" => "17,90,000",
"captial" => "Chennai") );
echo $states["karnataka"]["population"];
?>
Answer: 11,35,000
Explanation:
Treat states as a multidimensional array and accordingly traverse it to get the value.
Write a PHP function to test whether a number is greater than 30, 20 or 10 using
ternary operator.
Solution
<?php
function trinary_Test($n){
$r = $n > 30
? "greater than 30"
: $n > 20
? "greater than 20"
: $n >10
? "greater than 10"
: "Input a number atleast greater than 10!"));
echo $n." : ".$r."\n";
}
trinary_Test(32);
trinary_Test(21);
trinary_Test(12);
trinary_Test(4);
?>