Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

51.

Can You Define An Argument As A Reference Type?

Answer»

You can define an argument as a reference type in the FUNCTION definition. This will AUTOMATICALLY CONVERT the calling arguments into references. Here is a PHP script on how to define an argument as a reference type:
<?php
function ref_swap(&$a, &$b) {
$t = $a;
$a = $b;
$b = $t;
}
$x = "PHP";
$y = "JSP";
PRINT("Before swapping: $x, $y\n");
ref_swap($x, $y);
print("After swapping: $x, $y\n");
?&GT;
This script will print:
Before swapping: PHP, JSP
After swapping: JSP, PHP

You can define an argument as a reference type in the function definition. This will automatically convert the calling arguments into references. Here is a PHP script on how to define an argument as a reference type:
<?php
function ref_swap(&$a, &$b) {
$t = $a;
$a = $b;
$b = $t;
}
$x = "PHP";
$y = "JSP";
print("Before swapping: $x, $y\n");
ref_swap($x, $y);
print("After swapping: $x, $y\n");
?>
This script will print:
Before swapping: PHP, JSP
After swapping: JSP, PHP

52.

How To Pass Variables By References?

Answer»

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(&AMP;$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.

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.

53.

How Variables Are Passed Through Arguments?

Answer»

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.

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.

54.

How To Pass An Argument To A Function?

Answer»

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

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

55.

How To Return A Value Back To The Function Caller?

Answer»

You can return a VALUE to the function caller by USING the "return $value" statement. Execution control will be TRANSFERRED to the caller immediately after the return statement. If there are other statements in the function after the return statement, they will not be executed. Here is a PHP script example on how to return values:
<?php
function getYear() {
$year = DATE("Y");
return $year;
}
print("This year is: ".getYear()."\N");
?>
This script will print:
This year is: 2006

You can return a value to the function caller by using the "return $value" statement. Execution control will be transferred to the caller immediately after the return statement. If there are other statements in the function after the return statement, they will not be executed. Here is a PHP script example on how to return values:
<?php
function getYear() {
$year = date("Y");
return $year;
}
print("This year is: ".getYear()."\n");
?>
This script will print:
This year is: 2006

56.

How To Invoke A User Function?

Answer»

You can invoke a function by entering the function name FOLLOWED by a pair of parentheses. If NEEDED, function arguments can be specified as a LIST of expressions enclosed in parentheses. Here is a PHP script example on how to invoke a user function:
<?php
function hello($f) {
print("Hello $f!\n");
}
hello("Bob");
?&GT;
This script will print:
Hello Bob!

You can invoke a function by entering the function name followed by a pair of parentheses. If needed, function arguments can be specified as a list of expressions enclosed in parentheses. Here is a PHP script example on how to invoke a user function:
<?php
function hello($f) {
print("Hello $f!\n");
}
hello("Bob");
?>
This script will print:
Hello Bob!

57.

How To Define A User Function?

Answer»

You can DEFINE a user function ANYWHERE in a PHP SCRIPT using the function statement like this: "function name() {...}". Here is a PHP script EXAMPLE on how to define a user function:
<?php
function msg() {
print("Hello world!\n");
}
msg();
?>
This script will print:
Hello world!

You can define a user function anywhere in a PHP script using the function statement like this: "function name() {...}". Here is a PHP script example on how to define a user function:
<?php
function msg() {
print("Hello world!\n");
}
msg();
?>
This script will print:
Hello world!

58.

How To Join Multiple Strings Stored In An Array Into A Single String?

Answer»

If you multiple strings stored in an array, you can join them together into a single STRING with a given delimiter by USING the implode() function. Here is a PHP script on how to use implode():
&LT;?php
$DATE = array('01', '01', '2006');
$KEYS = array('php', 'string', 'function');
print("A formated date: ".implode("/",$date)."\n");
print("A keyword list: ".implode(", ",$keys)."\n");
?>
This script will print:
A formated date: 01/01/2006
A keyword list: php, string, function

If you multiple strings stored in an array, you can join them together into a single string with a given delimiter by using the implode() function. Here is a PHP script on how to use implode():
<?php
$date = array('01', '01', '2006');
$keys = array('php', 'string', 'function');
print("A formated date: ".implode("/",$date)."\n");
print("A keyword list: ".implode(", ",$keys)."\n");
?>
This script will print:
A formated date: 01/01/2006
A keyword list: php, string, function

59.

How To Pad An Array With The Same Value Multiple Times?

Answer»

If you want to add the same value multiple times to the end or beginning of an array, you can use the array_pad($array, $new_size, $value) FUNCTION. If the second argument, $new_size, is POSITIVE, it will pad to the end of the array. If negative, it will pad to the beginning of the array. If the absolute value of $new_size if not greater than the current size of the array, no PADDING takes place. Here is a PHP script on how to use array_pad():
&LT;?php
$array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java");
$array = array_pad($array, 6, ">>");
$array = array_pad($array, -8, "---");
print("Padded:\n");
print(join(",", array_values($array)));
print("\n");
?>
This script will print:
Padded:
---,---,PHP,Perl,Java,>>,>>,>>

If you want to add the same value multiple times to the end or beginning of an array, you can use the array_pad($array, $new_size, $value) function. If the second argument, $new_size, is positive, it will pad to the end of the array. If negative, it will pad to the beginning of the array. If the absolute value of $new_size if not greater than the current size of the array, no padding takes place. Here is a PHP script on how to use array_pad():
<?php
$array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java");
$array = array_pad($array, 6, ">>");
$array = array_pad($array, -8, "---");
print("Padded:\n");
print(join(",", array_values($array)));
print("\n");
?>
This script will print:
Padded:
---,---,PHP,Perl,Java,>>,>>,>>

60.

How To Randomly Retrieve A Value From An Array?

Answer»

If you have a list of favorite GREETING MESSAGES, and want to randomly select one of them to be used in an email, you can use the array_rand() function. Here is a PHP example script:
&LT;?php
$ARRAY = array("Hello!", "Hi!", "Allo!", "Hallo!", "Coucou!");
$key = array_rand($array);
print("Random greeting: ".$array[$key]."\n");
?>
This script will print:
Random greeting: Coucou!

If you have a list of favorite greeting messages, and want to randomly select one of them to be used in an email, you can use the array_rand() function. Here is a PHP example script:
<?php
$array = array("Hello!", "Hi!", "Allo!", "Hallo!", "Coucou!");
$key = array_rand($array);
print("Random greeting: ".$array[$key]."\n");
?>
This script will print:
Random greeting: Coucou!

61.

How To Merge Values Of Two Arrays Into A Single Array?

Answer»

You can use the array_merge() function to merge two arrays into a SINGLE array.
array_merge() APPENDS all pairs of keys and values of the second array to the end of the first array. Here is a PHP script on how to use array_merge():
<?php
$lang = array("Perl", "PHP", "Java",);
$os = array("i"=>"WINDOWS", "ii"=>"UNIX", "iii"=>"MAC");
$mixed = array_merge($lang, $os);
print("Merged:\n");
print_r($mixed);
?>
This script will print:
Merged:
Array
(
[0] => Perl
[1] => PHP
[2] => Java
[i] => Windows
[ii] => Unix
[iii] => Mac
)

You can use the array_merge() function to merge two arrays into a single array.
array_merge() appends all pairs of keys and values of the second array to the end of the first array. Here is a PHP script on how to use array_merge():
<?php
$lang = array("Perl", "PHP", "Java",);
$os = array("i"=>"Windows", "ii"=>"Unix", "iii"=>"Mac");
$mixed = array_merge($lang, $os);
print("Merged:\n");
print_r($mixed);
?>
This script will print:
Merged:
Array
(
[0] => Perl
[1] => PHP
[2] => Java
[i] => Windows
[ii] => Unix
[iii] => Mac
)

62.

How To Find A Specific Value In An Array?

Answer»

There are two FUNCTIONS can be USED to TEST if a value is defined in an ARRAY or not:

  • array_search($value, $array) - Returns the first key of the matching value in the array, if found. Otherwise, it returns false.
  • in_array($value, $array) - Returns true if the $value is defined in $array.

Here is a PHP script on how to use arrary_search():

<?php $array = array("Perl", "PHP", "Java", "PHP"); PRINT("Search 1: ".array_search("PHP",$array)."n"); print("Search 2: ".array_search("Perl",$array)."n"); print("Search 3: ".array_search("C#",$array)."n"); print("n"); ?>

This script will print:

Search 1: 1
Search 2: 0
Search 3:

There are two functions can be used to test if a value is defined in an array or not:

Here is a PHP script on how to use arrary_search():

This script will print:

Search 1: 1
Search 2: 0
Search 3:

63.

How To Get The Total Number Of Values In An Array?

Answer»

You can get the total number of values in an array by using the count() function. Here is a PHP example SCRIPT:
<?php
$array = array("PHP", "PERL", "JAVA");
print_r("Size 1: ".count($array)."\n");
$array = array();
print_r("Size 2: ".count($array)."\n");
?&GT;
This script will print:
Size 1: 3
Size 2: 0
Note that count() has an alias called sizeof().

You can get the total number of values in an array by using the count() function. Here is a PHP example script:
<?php
$array = array("PHP", "Perl", "Java");
print_r("Size 1: ".count($array)."\n");
$array = array();
print_r("Size 2: ".count($array)."\n");
?>
This script will print:
Size 1: 3
Size 2: 0
Note that count() has an alias called sizeof().

64.

How The Values Are Ordered In An Array?

Answer»

PHP says that an array is an ORDERED map. But how the VALUES are ordered in an array?
The answer is simple. Values are stored in the same order as they are INSERTED like a queue. If you want to reorder them differently, you need to use a sort function. Here is a PHP script show you the order of array values:
<?php
$mixed = array();
$mixed["Two"] = "Java";
$mixed["3"] = "C+";
$mixed["ZERO"] = "PHP";
$mixed[1] = "Perl";
$mixed[""] = "Basic";
$mixed[] = "Pascal";
$mixed[] = "FORTRAN";
$mixed["Two"] = "";
unset($mixed[4]);
print("Order of array values:\n");
print_r($mixed);
?&GT;
This script will print:
Order of array values:
Array
(
[Two] =>
[3] => C+
[Zero] => PHP
[1] => Perl
[] => Basic
[5] => FORTRAN
)

PHP says that an array is an ordered map. But how the values are ordered in an array?
The answer is simple. Values are stored in the same order as they are inserted like a queue. If you want to reorder them differently, you need to use a sort function. Here is a PHP script show you the order of array values:
<?php
$mixed = array();
$mixed["Two"] = "Java";
$mixed["3"] = "C+";
$mixed["Zero"] = "PHP";
$mixed[1] = "Perl";
$mixed[""] = "Basic";
$mixed[] = "Pascal";
$mixed[] = "FORTRAN";
$mixed["Two"] = "";
unset($mixed[4]);
print("Order of array values:\n");
print_r($mixed);
?>
This script will print:
Order of array values:
Array
(
[Two] =>
[3] => C+
[Zero] => PHP
[1] => Perl
[] => Basic
[5] => FORTRAN
)

65.

How Values In Arrays Are Indexed?

Answer»

VALUES in an array are all INDEXED their corresponding keys. Because we can use either an integer or a string as a key in an array, we can divide ARRAYS into 3 categories:

  • Numerical Array - All keys are sequential integers.
  • Associative Array - All keys are STRINGS.
  • MIXED Array - Some keys are integers, some keys are strings.

Values in an array are all indexed their corresponding keys. Because we can use either an integer or a string as a key in an array, we can divide arrays into 3 categories:

66.

How To Retrieve Values Out Of An Array?

Answer»

You can retrieve values out of ARRAYS using the array element expression $array[$key].
Here is a PHP example SCRIPT:
<?php $languages = array(); $languages["Zero"] = "PHP"; $languages["ONE"] = "Perl";
$languages["Two"] = "Java"; print("Array with inserted values:\N");
print_r($languages); ?>
This script will print:
Array with default KEYS:
The second value: Perl
Array with specified keys:
The third value: Java

You can retrieve values out of arrays using the array element expression $array[$key].
Here is a PHP example script:
<?php $languages = array(); $languages["Zero"] = "PHP"; $languages["One"] = "Perl";
$languages["Two"] = "Java"; print("Array with inserted values:\n");
print_r($languages); ?>
This script will print:
Array with default keys:
The second value: Perl
Array with specified keys:
The third value: Java

67.

How To Test If A Variable Is An Array?

Answer»

Testing if a variable is an array is easy. Just use the is_array() function. Here is a PHP script on how to use is_array():
&LT;?php
$var = array(0,0,7);
print("TEST 1: ". is_array($var)."\n");
$var = array();
print("Test 2: ". is_array($var)."\n");
$var = 1800;
print("Test 3: ". is_array($var)."\n");
$var = TRUE;
print("Test 4: ". is_array($var)."\n");
$var = NULL;
print("Test 5: ". is_array($var)."\n");
$var = "PHP";
print("Test 6: ". is_array($var)."\n");
print("\n");
?>
This script will print:
Test 1: 1
Test 2: 1
Test 3:
Test 4:
Test 5:
Test 6:

Testing if a variable is an array is easy. Just use the is_array() function. Here is a PHP script on how to use is_array():
<?php
$var = array(0,0,7);
print("Test 1: ". is_array($var)."\n");
$var = array();
print("Test 2: ". is_array($var)."\n");
$var = 1800;
print("Test 3: ". is_array($var)."\n");
$var = true;
print("Test 4: ". is_array($var)."\n");
$var = null;
print("Test 5: ". is_array($var)."\n");
$var = "PHP";
print("Test 6: ". is_array($var)."\n");
print("\n");
?>
This script will print:
Test 1: 1
Test 2: 1
Test 3:
Test 4:
Test 5:
Test 6:

68.

What Is An Array In Php?

Answer»

An array in PHP is really an ordered map of PAIRS of keys and VALUES.
Comparing with Perl, an array in PHP is not like a normal array in Perl. An array in PHP is like an ASSOCIATE array in Perl. But an array in PHP can work like a normal array in Perl.
Comparing with Java, an array in PHP is not like an array in Java. An array in PHP is like a TREEMAP class in Java. But an array in PHP can work like an array in Java.

An array in PHP is really an ordered map of pairs of keys and values.
Comparing with Perl, an array in PHP is not like a normal array in Perl. An array in PHP is like an associate array in Perl. But an array in PHP can work like a normal array in Perl.
Comparing with Java, an array in PHP is not like an array in Java. An array in PHP is like a TreeMap class in Java. But an array in PHP can work like an array in Java.

69.

How To Join Multiple Strings Into A Single String?

Answer»

If you multiple strings stored in an array, you can join them TOGETHER into a SINGLE string with a given delimiter by using the implode() function. Here is a PHP script on how to USE implode():
<?php
$date = array('01', '01', '2006');
$keys = array('php', 'string', 'function');
print("A formated date: ".implode("/",$date)."\n");
print("A keyword list: ".implode(", ",$keys)."\n");
?&GT;
This script will print:
A formated date: 01/01/2006
A keyword list: php, string, function

If you multiple strings stored in an array, you can join them together into a single string with a given delimiter by using the implode() function. Here is a PHP script on how to use implode():
<?php
$date = array('01', '01', '2006');
$keys = array('php', 'string', 'function');
print("A formated date: ".implode("/",$date)."\n");
print("A keyword list: ".implode(", ",$keys)."\n");
?>
This script will print:
A formated date: 01/01/2006
A keyword list: php, string, function

70.

How To Convert A Character To An Ascii Value?

Answer»

If you want to convert characters to ASCII values, you can USE the ORD() function, which takes the first charcter of the specified string, and returns its ASCII value in DECIMAL format. ord() COMPLEMENTS chr(). Here is a PHP script on how to use ord():
<?php
print(ord("Hello")."\n");
print(chr(72)."\n");
?>
This script will print:
72
H

If you want to convert characters to ASCII values, you can use the ord() function, which takes the first charcter of the specified string, and returns its ASCII value in decimal format. ord() complements chr(). Here is a PHP script on how to use ord():
<?php
print(ord("Hello")."\n");
print(chr(72)."\n");
?>
This script will print:
72
H

71.

How To Generate A Character From An Ascii Value?

Answer»

If you want to generate characters from ASCII values, you can USE the chr() function.
chr() takes the ASCII value in decimal format and returns the character represented by the ASCII value. chr() complements ord(). Here is a PHP script on how to use chr():
<?php
PRINT(chr(72).chr(101).chr(108).chr(108).chr(111)."\n");
print(ord("H")."\n");
?>
This script will print:
Hello
72

If you want to generate characters from ASCII values, you can use the chr() function.
chr() takes the ASCII value in decimal format and returns the character represented by the ASCII value. chr() complements ord(). Here is a PHP script on how to use chr():
<?php
print(chr(72).chr(101).chr(108).chr(108).chr(111)."\n");
print(ord("H")."\n");
?>
This script will print:
Hello
72

72.

How To Convert Strings In Hex Format?

Answer»

If you want convert a STRING into hex FORMAT, you can use the bin2hex() function. Here is a PHP script on how to use bin2hex():
<?php
$string = "Hello\tworld!\n";
PRINT($string."\n");
print(bin2hex($string)."\n");
?>
This script will print:
Hello world!
48656c6c6f09776f726c64210a

If you want convert a string into hex format, you can use the bin2hex() function. Here is a PHP script on how to use bin2hex():
<?php
$string = "Hello\tworld!\n";
print($string."\n");
print(bin2hex($string)."\n");
?>
This script will print:
Hello world!
48656c6c6f09776f726c64210a

73.

How To Convert The First Character To Upper Case?

Answer»

If you are processing an ARTICLE, you may WANT to capitalize the first CHARACTER of a SENTENCE by using the ucfirst() FUNCTION. You may also want to capitalize the first character of every words for the article title by using the ucwords() function. Here is a PHP script on how to use ucfirst() and ucwords():
<?php
$string = "php string functions are easy to use.";
$sentence = ucfirst($string);
$title = ucwords($string);
print("$sentence\n");
print("$title\n");
print("\n");
?>
This script will print:
Php string functions are easy to use.
Php String Functions Are Easy To Use.

If you are processing an article, you may want to capitalize the first character of a sentence by using the ucfirst() function. You may also want to capitalize the first character of every words for the article title by using the ucwords() function. Here is a PHP script on how to use ucfirst() and ucwords():
<?php
$string = "php string functions are easy to use.";
$sentence = ucfirst($string);
$title = ucwords($string);
print("$sentence\n");
print("$title\n");
print("\n");
?>
This script will print:
Php string functions are easy to use.
Php String Functions Are Easy To Use.

74.

How To Convert Strings To Upper Or Lower Cases?

Answer»

Converting strings to upper or lower CASES are easy. Just use STRTOUPPER() or strtolower() functions. Here is a PHP script on how to use them:
<?php
$string = "PHP string functions are easy to use.";
$lower = strtolower($string);
$upper = strtoupper($string);
print("$lower\N");
print("$upper\n");
print("\n");
?&GT;
This script will print:
php string functions are easy to use.
PHP STRING FUNCTIONS ARE EASY TO USE.

Converting strings to upper or lower cases are easy. Just use strtoupper() or strtolower() functions. Here is a PHP script on how to use them:
<?php
$string = "PHP string functions are easy to use.";
$lower = strtolower($string);
$upper = strtoupper($string);
print("$lower\n");
print("$upper\n");
print("\n");
?>
This script will print:
php string functions are easy to use.
PHP STRING FUNCTIONS ARE EASY TO USE.

75.

How To Replace A Substring In A Given String?

Answer»

If you know the POSITION of a substring in a given string, you can replace that substring by another string by using the substr_replace() function. Here is a PHP script on how to use substr_replace():
&LT;?php
$string = "Warning: System will shutdown in NN minutes!";
$pos = strpos($string, "NN");
print(substr_replace($string, "15", $pos, 2)."\N");
sleep(10*60);
print(substr_replace($string, "5", $pos, 2)."\n");
?>
This script will print:
Warning: System will shutdown in 15 minutes!
(10 minutes later)
Warning: System will shutdown in 5 minutes!
Like substr(), substr_replace() can take negative starting position counted from the END of the string.

If you know the position of a substring in a given string, you can replace that substring by another string by using the substr_replace() function. Here is a PHP script on how to use substr_replace():
<?php
$string = "Warning: System will shutdown in NN minutes!";
$pos = strpos($string, "NN");
print(substr_replace($string, "15", $pos, 2)."\n");
sleep(10*60);
print(substr_replace($string, "5", $pos, 2)."\n");
?>
This script will print:
Warning: System will shutdown in 15 minutes!
(10 minutes later)
Warning: System will shutdown in 5 minutes!
Like substr(), substr_replace() can take negative starting position counted from the end of the string.

76.

How To Take A Substring From A Given String?

Answer»

If you know the position of a substring in a given STRING, you can take the substring out by the substr() function. Here is a PHP script on how to USE substr():
<?php
$string = "beginning";
print("Position counted from LEFT: ".substr($string,0,5)."\n");
print("Position counted form right: ".substr($string,-7,3)."\n");
?>
This script will print:
Position counted from left: begin
Position counted form right: GIN
substr() can take negative starting position counted from the END of the string.

If you know the position of a substring in a given string, you can take the substring out by the substr() function. Here is a PHP script on how to use substr():
<?php
$string = "beginning";
print("Position counted from left: ".substr($string,0,5)."\n");
print("Position counted form right: ".substr($string,-7,3)."\n");
?>
This script will print:
Position counted from left: begin
Position counted form right: gin
substr() can take negative starting position counted from the end of the string.

77.

How To Remove Leading And Trailing Spaces From User Input Values?

Answer»

If you are taking input values from users with a Web form, users may enter extra SPACES at the BEGINNING and/or the END of the input values. You should always use the trim() function to remove those extra spaces as shown in this PHP script:
<?php
$name = $_REQUEST("name");
$name = trim($name);
# $name is ready to be used...
?>

If you are taking input values from users with a Web form, users may enter extra spaces at the beginning and/or the end of the input values. You should always use the trim() function to remove those extra spaces as shown in this PHP script:
<?php
$name = $_REQUEST("name");
$name = trim($name);
# $name is ready to be used...
?>

78.

How To Remove The New Line Character From The End Of A Text Line?

Answer»

If you are using fgets() to read a line from a TEXT file, you MAY want to use the CHOP() function to remove the new line character from the end of the line as shown in this PHP script:
<?php
$handle = fopen("/tmp/inputfile.txt", "r");
while ($line=fgets()) {
$line = chop($line);
# PROCESS $line here...
}
fclose($handle);
?>

If you are using fgets() to read a line from a text file, you may want to use the chop() function to remove the new line character from the end of the line as shown in this PHP script:
<?php
$handle = fopen("/tmp/inputfile.txt", "r");
while ($line=fgets()) {
$line = chop($line);
# process $line here...
}
fclose($handle);
?>

79.

How To Get The Number Of Characters In A String?

Answer»

You can use the "strlen()" FUNCTION to get the NUMBER of characters in a string. Here is a PHP SCRIPT EXAMPLE of strlen():
<?php
print(strlen('It\'s Friday!'));
?>
This script will print:
12

You can use the "strlen()" function to get the number of characters in a string. Here is a PHP script example of strlen():
<?php
print(strlen('It\'s Friday!'));
?>
This script will print:
12

80.

How To Assigning A New Character In A String?

Answer»

The string element expression, $string{index}, can also be USED at the LEFT side of an assignment statement. This allows you to assign a NEW character to any position in a string. Here is a PHP script example:
&LT;?php
$string = 'It\'s Friday?';
echo "$string\n";
$string{11} = '!';
echo "$string\n";
?>
This script will print:
It's Friday?
It's Friday!

The string element expression, $string{index}, can also be used at the left side of an assignment statement. This allows you to assign a new character to any position in a string. Here is a PHP script example:
<?php
$string = 'It\'s Friday?';
echo "$string\n";
$string{11} = '!';
echo "$string\n";
?>
This script will print:
It's Friday?
It's Friday!

81.

How To Access A Specific Character In A String?

Answer»

Any character in a string can be accessed by a special string element expression:
• $string{index} - The index is the position of the character counted from LEFT and starting from 0.
Here is a PHP script example:
<?php
$string = 'It\'s Friday!';
echo "The FIRST character is $string{0}\N";
echo "The first character is {$string{0}}\n";
?>
This script will PRINT:
The first character is It's Friday!{0}
The first character is I

Any character in a string can be accessed by a special string element expression:
• $string{index} - The index is the position of the character counted from left and starting from 0.
Here is a PHP script example:
<?php
$string = 'It\'s Friday!';
echo "The first character is $string{0}\n";
echo "The first character is {$string{0}}\n";
?>
This script will print:
The first character is It's Friday!{0}
The first character is I

82.

How To Include Variables In Double-quoted Strings?

Answer»

Variables INCLUDED in double-quoted strings will be interpolated. Their values will be concatenated into the ENCLOSING strings. For example, two statements in the following PHP script will print out the same string:
&LT;?php
$variable = "and";
ECHO "part 1 $variable part 2\n";
echo "part 1 ".$variable." part 2\n";
?&GT;
This script will print:
part 1 and part 2
part 1 and part 2

Variables included in double-quoted strings will be interpolated. Their values will be concatenated into the enclosing strings. For example, two statements in the following PHP script will print out the same string:
<?php
$variable = "and";
echo "part 1 $variable part 2\n";
echo "part 1 ".$variable." part 2\n";
?>
This script will print:
part 1 and part 2
part 1 and part 2

83.

How Many Escape Sequences Are Recognized In Double-quoted Strings?

Answer»

There are 12 escape SEQUENCES you can use in DOUBLE-quoted strings:
• \\ - Represents the back slash character.
• \" - Represents the double quote character.
• \$ - Represents the dollar SIGN.
• \N - Represents the new line character (ASCII code 10).
• \r - Represents the carriage return character (ASCII code 13).
• \t - Represents the tab character (ASCII code 9).
• \{ - Represents the open brace character.
• \} - Represents the close brace character.
• \[ - Represents the open bracket character.
• \] - Represents the close bracket character.
• \nnn - Represents a character as an octal value.
• \xnn - Represents a character as a hex value.

There are 12 escape sequences you can use in double-quoted strings:
• \\ - Represents the back slash character.
• \" - Represents the double quote character.
• \$ - Represents the dollar sign.
• \n - Represents the new line character (ASCII code 10).
• \r - Represents the carriage return character (ASCII code 13).
• \t - Represents the tab character (ASCII code 9).
• \{ - Represents the open brace character.
• \} - Represents the close brace character.
• \[ - Represents the open bracket character.
• \] - Represents the close bracket character.
• \nnn - Represents a character as an octal value.
• \xnn - Represents a character as a hex value.

84.

What Are The Special Characters You Need To Escape In Double-quoted Stings?

Answer»

There are two SPECIAL characters you need to escape in a DOUBLE-quote string: the double quote (") and the back slash (\). Here is a PHP script EXAMPLE of double-quoted strings:
<?php
echo "Hello WORLD!";
echo "TOM said: \"Who's there?\"";
echo "\\ represents an operator.";
?>
This script will print:
Hello world!Tom said: "Who's there?"\ represents an operator.

There are two special characters you need to escape in a double-quote string: the double quote (") and the back slash (\). Here is a PHP script example of double-quoted strings:
<?php
echo "Hello world!";
echo "Tom said: \"Who's there?\"";
echo "\\ represents an operator.";
?>
This script will print:
Hello world!Tom said: "Who's there?"\ represents an operator.

85.

How Many Escape Sequences Are Recognized In Single-quoted Strings?

Answer»

There are 2 ESCAPE sequences you can use in SINGLE-quoted strings:
• \\ - Represents the back slash character.
• \' - Represents the single QUOTE character.

There are 2 escape sequences you can use in single-quoted strings:
• \\ - Represents the back slash character.
• \' - Represents the single quote character.

86.

How To Process The Uploaded Files?

Answer»

How to process the uploaded FILES? The ANSWER is really depending on your application. For example:

  • You can attached the outgoing emails, if the uploaded files are email attachments.
  • You can move them to user's Web page directory, if the uploaded files are user's Web pages.
  • You can move them to a permanent directory and save the files names in the database, if the uploaded files are ARTICLES to be published on the Web site.
  • You can store them to database tables, if you don't WANT store them as files.

How to process the uploaded files? The answer is really depending on your application. For example:

87.

How To Move Uploaded Files To Permanent Directory?

Answer»

PHP stores uploaded files in a temporary directory with temporary file NAMES. You must move uploaded files to a permanent directory, if you want to keep them permanently.

PHP offers the move_uploaded_file() to help you moving uploaded files. The EXAMPLE script, processing_ uploaded_files.php, below shows a good example:

<?php
$file = '\pickzycenter\images\pickzycenter.logo';
print("<pre>\n");
move_uploaded_file($_FILES['pickzycenter_logo']['tmp_name'], $file);
print("File uploaded: ".$file."\n");
print("</pre>\n");
?>

NOTE that you need to change the permanent directory, "\pickzycenter\images\", used in this script to something else on your Web server. If your Web server is provided by a Web hosting company, you may need to ask them which DIRECTORIES you can use to store files.

If you copy both scripts, logo_upload.php and processing_uploaded_files.php, to your Web server, you can try them to upload an image file to your Web server.

PHP stores uploaded files in a temporary directory with temporary file names. You must move uploaded files to a permanent directory, if you want to keep them permanently.

PHP offers the move_uploaded_file() to help you moving uploaded files. The example script, processing_ uploaded_files.php, below shows a good example:

<?php
$file = '\pickzycenter\images\pickzycenter.logo';
print("<pre>\n");
move_uploaded_file($_FILES['pickzycenter_logo']['tmp_name'], $file);
print("File uploaded: ".$file."\n");
print("</pre>\n");
?>

Note that you need to change the permanent directory, "\pickzycenter\images\", used in this script to something else on your Web server. If your Web server is provided by a Web hosting company, you may need to ask them which directories you can use to store files.

If you copy both scripts, logo_upload.php and processing_uploaded_files.php, to your Web server, you can try them to upload an image file to your Web server.

88.

Why Do You Need To Filter Out Empty Files?

Answer»

When you are processing uploaded FILES, you need to check for empty files, because they COULD be resulted from a bad upload process but the PHP engine could still give no error.

For example, if a USER typed a bad file name in the upload field and submitted the form, the PHP engine will take it as an empty file without raising any error. The script below shows you an improved logic to process uploaded files:

<?php
$file = '\pickzycenter\images\pickzycenter.logo';
$error = $_FILES['pickzycenter_logo']['error'];
$tmp_name = $_FILES['pickzycenter_logo']['tmp_name'];
print("
\N");
if ($error==UPLOAD_ERR_OK) {
if ($_FILES['pickzycenter_logo']['size'] > 0) {
move_uploaded_file($tmp_name, $file);
print("File uploaded.\n");
} else {
print("Loaded file is empty.\n");
}
} else if ($error==UPLOAD_ERR_NO_FILE) {
print("No files specified.\n");
} else {
print("Upload faield.\n");
}
print("
\n");
?>

When you are processing uploaded files, you need to check for empty files, because they could be resulted from a bad upload process but the PHP engine could still give no error.

For example, if a user typed a bad file name in the upload field and submitted the form, the PHP engine will take it as an empty file without raising any error. The script below shows you an improved logic to process uploaded files:

<?php
$file = '\pickzycenter\images\pickzycenter.logo';
$error = $_FILES['pickzycenter_logo']['error'];
$tmp_name = $_FILES['pickzycenter_logo']['tmp_name'];
print("
\n");
if ($error==UPLOAD_ERR_OK) {
if ($_FILES['pickzycenter_logo']['size'] > 0) {
move_uploaded_file($tmp_name, $file);
print("File uploaded.\n");
} else {
print("Loaded file is empty.\n");
}
} else if ($error==UPLOAD_ERR_NO_FILE) {
print("No files specified.\n");
} else {
print("Upload faield.\n");
}
print("
\n");
?>

89.

How To Create A Table To Store Files?

Answer»

If you USING MySQL database and WANT to store files in database, you need to create BLOB columns, which can holds up to 65,535 characters. Here is a sample script that creates a table with a BLOB column to be used to store uploaded files:
<?php
$CON = mysql_connect("localhost", "", "");
mysql_select_db("pickzy");
$sql = "CREATE TABLE pickzy_files ("
. " ID INTEGER NOT NULL AUTO_INCREMENT"
. ", NAME VARCHAR(80) NOT NULL"
. ", type VARCHAR(80) NOT NULL"
. ", size INTEGER NOT NULL"
. ", content BLOB"
. ", PRIMARY KEY (id)"
. ")";
mysql_query($sql, $con);
mysql_close($con);
?>

If you using MySQL database and want to store files in database, you need to create BLOB columns, which can holds up to 65,535 characters. Here is a sample script that creates a table with a BLOB column to be used to store uploaded files:
<?php
$con = mysql_connect("localhost", "", "");
mysql_select_db("pickzy");
$sql = "CREATE TABLE pickzy_files ("
. " id INTEGER NOT NULL AUTO_INCREMENT"
. ", name VARCHAR(80) NOT NULL"
. ", type VARCHAR(80) NOT NULL"
. ", size INTEGER NOT NULL"
. ", content BLOB"
. ", PRIMARY KEY (id)"
. ")";
mysql_query($sql, $con);
mysql_close($con);
?>

90.

How To Uploaded Files To A Table?

Answer»

To store uploaded files to MySQL database, you can USE the normal SELECT statement as shown in the modified processing_uploaded_files.php listed below:
<?php
$con = mysql_connect("localhost", "", "");
mysql_select_db("pickzy");
$error = $_FILES['pickzycenter_logo']['error'];
$tmp_name = $_FILES['pickzycenter_logo']['tmp_name'];
$size = $_FILES['pickzycenter_logo']['size'];
$name = $_FILES['pickzycenter_logo']['name'];
$type = $_FILES['pickzycenter_logo']['type'];
print("
\n");
if ($error == UPLOAD_ERR_OK &AMP;& $size > 0) {
$fp = FOPEN($tmp_name, 'r');
$content = fread($fp, $size);
fclose($fp);
$content = addslashes($content);
$SQL = "INSERT INTO pickzy_files (name, type, size, content)"
. " VALUES ('$name', '$type', $size, '$content')";
mysql_query($sql, $con);
print("File stored.\n");
} else {
print("Upload faield.\n");
}
print("
\n");
mysql_close($con);
?>
Note that addslashes() is used to add backslashes to special characters that need to be protected in SQL statements.

To store uploaded files to MySQL database, you can use the normal SELECT statement as shown in the modified processing_uploaded_files.php listed below:
<?php
$con = mysql_connect("localhost", "", "");
mysql_select_db("pickzy");
$error = $_FILES['pickzycenter_logo']['error'];
$tmp_name = $_FILES['pickzycenter_logo']['tmp_name'];
$size = $_FILES['pickzycenter_logo']['size'];
$name = $_FILES['pickzycenter_logo']['name'];
$type = $_FILES['pickzycenter_logo']['type'];
print("
\n");
if ($error == UPLOAD_ERR_OK && $size > 0) {
$fp = fopen($tmp_name, 'r');
$content = fread($fp, $size);
fclose($fp);
$content = addslashes($content);
$sql = "INSERT INTO pickzy_files (name, type, size, content)"
. " VALUES ('$name', '$type', $size, '$content')";
mysql_query($sql, $con);
print("File stored.\n");
} else {
print("Upload faield.\n");
}
print("
\n");
mysql_close($con);
?>
Note that addslashes() is used to add backslashes to special characters that need to be protected in SQL statements.

91.

What Are The File Upload Settings In Configuration File?

Answer»

There are several settings in the PHP configuration file related to file UPLOADING:
• file_uploads = On/Off - Whether or not to allow HTTP file uploads.
• upload_tmp_dir = directory - The temporary directory USED for STORING FILES when doing file upload.
• upload_max_filesize = size - The maximum size of an uploaded file.

There are several settings in the PHP configuration file related to file uploading:
• file_uploads = On/Off - Whether or not to allow HTTP file uploads.
• upload_tmp_dir = directory - The temporary directory used for storing files when doing file upload.
• upload_max_filesize = size - The maximum size of an uploaded file.

92.

What Are The Advantages/disadvantages Of Mysql And Php?

Answer»

Both of them are OPEN SOURCE SOFTWARE (so free of cost), support cross platform. php is faster then ASP and ISP.

Both of them are open source software (so free of cost), support cross platform. php is faster then ASP and iSP.

93.

What Is 'float' Property In Css?

Answer»

The float property SETS where an IMAGE or a TEXT will appear in another ELEMENT.

The float property sets where an image or a text will appear in another element.

94.

Are Namespaces Are There In Javascript?

Answer»

A namespace is a container and ALLOWS you to bundle up all your FUNCTIONALITY USING a unique name. In JavaScript, a namespace is really just an object that you’ve attached all further methods, properties and objects. But it is not always NECESSARY to use namespace.

A namespace is a container and allows you to bundle up all your functionality using a unique name. In JavaScript, a namespace is really just an object that you’ve attached all further methods, properties and objects. But it is not always necessary to use namespace.

95.

Which Method Do You Follow To Get A Record From A Million Records? (searching Not From Database, From An Array In Php)?

Answer»

USE array_searchfl, array_keys, arrayyalues, array_key_exists, and in_array.

use array_searchfl, array_keys, arrayyalues, array_key_exists, and in_array.

96.

Who Is The Father Of Php And Explain The Changes In Php Versions?

Answer»

Rasmus Lerdorf is known as the father of PHP.PHP/F1 2.0 is an early and no longer supported VERSION of PHP. PHP 3 is the successor to PHP/FI 2.0 and is a lot NICER. PHP 4 is the current generation of PHP, which uses the ZEND engine under the HOOD. PHP 5 uses Zend engine 2 which, among other things, offers many additionalOOP features .

Rasmus Lerdorf is known as the father of PHP.PHP/F1 2.0 is an early and no longer supported version of PHP. PHP 3 is the successor to PHP/FI 2.0 and is a lot nicer. PHP 4 is the current generation of PHP, which uses the Zend engine under the hood. PHP 5 uses Zend engine 2 which, among other things, offers many additionalOOP features .

97.

What Is The Difference Between Group By And Order By In Sql?

Answer»

To sort a result, use an ORDER BY clause.
The most general way to satisfy a GROUP BY clause is to scan the whole table and create a new temporary table where all rows from each group are consecutive, and then use this temporary table to discover groups and apply aggregate functions (if any). ORDER BY [COL1],[col2],...[coln]; Tells DBMS according to what columns it should sort the result. If two rows will have the same value in col1 it will try to sort them according to col2 and so on.

GROUP BY [col1],[col2],...[coln]; Tells DBMS to group (aggregate) RESULTS with same value of column col1. You can use COUNT(col1), SUM(col1), AVG(col1) with it, if you want to count all items in group, sum all VALUES or view AVERAGE.

To sort a result, use an ORDER BY clause.
The most general way to satisfy a GROUP BY clause is to scan the whole table and create a new temporary table where all rows from each group are consecutive, and then use this temporary table to discover groups and apply aggregate functions (if any). ORDER BY [col1],[col2],...[coln]; Tells DBMS according to what columns it should sort the result. If two rows will have the same value in col1 it will try to sort them according to col2 and so on.

GROUP BY [col1],[col2],...[coln]; Tells DBMS to group (aggregate) results with same value of column col1. You can use COUNT(col1), SUM(col1), AVG(col1) with it, if you want to count all items in group, sum all values or view average.

98.

What Is Meant By Mime?

Answer»

MIME is MULTIPURPOSE Internet Mail Extensions is an Internet standard for the format of e-mail. However browsers also USES MIME standard to transmit FILES. MIME has a header which is added to a beginning of the data. When browser sees such header it shows the data as it would be a file (for EXAMPLE image) Some examples of MIME TYPES:

audio/x-ms-wmp
image/png
application/x-shockwave-flash

MIME is Multipurpose Internet Mail Extensions is an Internet standard for the format of e-mail. However browsers also uses MIME standard to transmit files. MIME has a header which is added to a beginning of the data. When browser sees such header it shows the data as it would be a file (for example image) Some examples of MIME types:

audio/x-ms-wmp
image/png
application/x-shockwave-flash

99.

What Are The Different Ways To Login To A Remote Server? Explain The Means, Advantages And Disadvantages?

Answer»

There is at least 3 ways to logon to a REMOTE SERVER:
Use ssh or TELNET if you concern with security
You can ALSO use RLOGIN to logon to a remote server.

There is at least 3 ways to logon to a remote server:
Use ssh or telnet if you concern with security
You can also use rlogin to logon to a remote server.

100.

Steps For The Payment Gateway Processing?

Answer»

An online PAYMENT gateway is the interface between your merchant account and your Web site. The online payment gateway ALLOWS you to immediately verify credit card transactions and authorize funds on a customer’s credit card DIRECTLY from your Web site. It then passes the TRANSACTION off to your merchant bank for processing, commonly referred to as transaction BATCHING.

An online payment gateway is the interface between your merchant account and your Web site. The online payment gateway allows you to immediately verify credit card transactions and authorize funds on a customer’s credit card directly from your Web site. It then passes the transaction off to your merchant bank for processing, commonly referred to as transaction batching.