October 9, 2018

Sreekanth B

Concentrix Most Frequently Asked PHP Latest Interview Questions Answers

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

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.
Concentrix Most Frequently Asked PHP Latest Interview Questions Answers
Concentrix Most Frequently Asked PHP Latest Interview Questions Answers

How To Include Variables In Double-quoted Strings?

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

How To Uploaded Files To A Table?

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.

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

Subscribe to get more Posts :