Lenovo PHP Most Frequently Asked Latest Interview Questions Answers
How To Create A Table To Store Files?
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);
?>
Why Do You Need To Filter Out Empty Files?
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");
?>
How To Move Uploaded Files To Permanent Directory?
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.
How To Process The Uploaded Files?
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 Many Escape Sequences Are Recognized In Single-quoted Strings?
There are 2 escape sequences you can use in single-quoted strings:
• \\ - Represents the back slash character.
• \' - Represents the single quote character.
What Are The Special Characters You Need To Escape In Double-quoted Stings?
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.
How To Access A Specific Character In A String?
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
How To Assigning A New Character In A String?
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!
How To Get The Number Of Characters In A String?
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
How To Remove The New Line Character From The End Of A Text Line?
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);
?>
How To Remove Leading And Trailing Spaces From User Input Values?
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...
?>
How To Take A Substring From A Given 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.
How To Join Multiple Strings Into A Single String?
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
What Is An Array In Php?
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.
How To Test If A Variable Is An Array?
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:.
How To Create A Table To Store Files?
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);
?>
Lenovo PHP Most Frequently Asked Latest Interview Questions Answers |
Why Do You Need To Filter Out Empty Files?
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");
?>
How To Move Uploaded Files To Permanent Directory?
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.
How To Process The Uploaded Files?
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 Many Escape Sequences Are Recognized In Single-quoted Strings?
There are 2 escape sequences you can use in single-quoted strings:
• \\ - Represents the back slash character.
• \' - Represents the single quote character.
What Are The Special Characters You Need To Escape In Double-quoted Stings?
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.
How To Access A Specific Character In A String?
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
How To Assigning A New Character In A String?
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!
How To Get The Number Of Characters In A String?
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
How To Remove The New Line Character From The End Of A Text Line?
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);
?>
How To Remove Leading And Trailing Spaces From User Input Values?
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...
?>
How To Take A Substring From A Given 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.
How To Join Multiple Strings Into A Single String?
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
What Is An Array In Php?
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.
How To Test If A Variable Is An Array?
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:.
Post a Comment