June 2, 2019

Srikaanth

Capita Plc PHP Interview Questions Answers

How To Pass An Argument To A Function?

To pass an argument to a function, you need to:

•Add an argument definition in the function definition.
•Add a value as an argument when invoking the function.
Here is a PHP script on how to use arguments in a function():
<?php
function f2c($f) {
return ($f - 32.0)/1.8;
}
print("Celsius: ".f2c(100.0)."\n");
print("Celsius: ".f2c(-40.0)."\n");
?>
This script will print:
Celsius: 37.777777777778
Celsius: -40
Capita Plc Most Frequently Asked PHP Latest Interview Questions Answers
Capita Plc Most Frequently Asked PHP Latest Interview Questions Answers

How Variables Are Passed Through Arguments?


Like more of other programming languages, variables are passed through arguments by values, not by references. That means when a variable is passed as an argument, a copy of the value will be passed into the function. Modipickzyng that copy inside the function will not impact the original copy. Here is a PHP script on passing variables by values:
<?php
function swap($a, $b) {
$t = $a;
$a = $b;
$b = $t;
}
$x = "PHP";
$y = "JSP";
print("Before swapping: $x, $y\n");
swap($x, $y);
print("After swapping: $x, $y\n");
?>
This script will print:
Before swapping: PHP, JSP
After swapping: PHP, JSP
As you can see, original variables were not affected.

How To Pass Variables By References?

You can pass a variable by reference to a function by taking the reference of the original variable, and passing that reference as the calling argument. Here is a PHP script on how to use pass variables by references:
<?php
function swap($a, $b) {
$t = $a;
$a = $b;
$b = $t;
}
$x = "PHP";
$y = "JSP";
print("Before swapping: $x, $y\n");
swap(&$x, &$y);
print("After swapping: $x, $y\n");
?>
This script will print:
Before swapping: PHP, JSP
After swapping: JSP, PHP
As you can see, the function modified the original variable.
Note that call-time pass-by-reference has been deprecated. You need to define arguments as references.

What Is The Scope Of A Variable Defined Outside A Function?
A variable defined outside any functions in main script body is called global variable. However, a global variable is not really accessible globally any in the script. The scope of global variable is limited to all statements outside any functions. So you can not access any global variables inside a function. Here is a PHP script on the scope of global variables:
<?php
?>
$login = "pickzycenter";
function myLogin() {
print("Defined inside the function? ". isset($login)."\n");
}
myLogin();
print("Defined outside the function? ". isset($login)."\n");
?>
This script will print:
Defined inside the function?
Defined outside the function?

How To Access A Global Variable Inside A Function?
By default, global variables are not accessible inside a function. However, you can make them accessible by declare them as "global" inside a function. Here is a PHP script on declaring "global" variables:
<?php
?>
$intRate = 5.5;
function myAccount() {
global $intRate;
print("Defined inside the function? ". isset($intRate)."\n");
}
myAccount();
print("Defined outside the function? ". isset($intRate)."\n");
?>
This script will print:
Defined inside the function? 1
Defined outside the function? 1

How To Specify Argument Default Values?
If you want to allow the caller to skip an argument when calling a function, you can define the argument with a default value when defining the function. Adding a default value to an argument can be done like this "function name($arg=expression){}. Here is a PHP script on how to specify default values to arguments:
<?php
function printKey($key="download") {
print("PHP $key\n");
}
printKey();
printKey("hosting");
print("\n");
?>
This script will print:
PHP download
PHP hosting

How To Define A Function With Any Number Of Arguments?

If you want to define a function with any number of arguments, you need to:
•Declare the function with no argument.
•Call func_num_args() in the function to get the number of the arguments.
•Call func_get_args() in the function to get all the arguments in an array.
Here is a PHP script on how to handle any number of arguments:
<?php
function myAverage() {
$count = func_num_args();
$args = func_get_args();
$sum = array_sum($args);
return $sum/$count;
}
$average = myAverage(102, 121, 105);
print("Average 1: $average\n");
$average = myAverage(102, 121, 105, 99, 101, 110, 116, 101, 114);
print("Average 2: $average\n");
?>
This script will print:
Average 1: 109.33333333333
Average 2: 107.66666666667

How To Create A Directory?

You can use the mkdir() function to create a directory. Here is a PHP script example on how to use mkdir():
<?php
if (file_exists("/temp/download")) {
print("Directory already exists.\n");
} else {
mkdir("/temp/download");
print("Directory created.\n");
}
?>
This script will print:
Directory created.
If you run this script again, it will print:
Directory already exists.

How To Remove An Empty Directory?
If you have an empty existing directory and you want to remove it, you can use the rmdir(). Here is a PHP script example on how to use rmdir():
<?php
if (file_exists("/temp/download")) {
rmdir("/temp/download");
print("Directory removed.\n");
} else {
print("Directory does not exist.\n");
}
?>
This script will print:
Directory removed.
If you run this script again, it will print:
Directory does not exist.

https://mytecbooks.blogspot.com/2018/10/capita-plc-most-frequently-asked-php.html
Subscribe to get more Posts :