PHP

  Home  Computer Programming  PHP


“Learn PHP programming with Interview Questions and Answers and examples.”



139 PHP Questions And Answers

41⟩ How can I execute a PHP script using command line?

Just run the PHP CLI (Command Line Interface) program and provide the PHP script file name as the command line argument. For example, "php myScript.php", assuming "php" is the command to invoke the CLI program.

Be aware that if your PHP script was written for the Web CGI interface, it may not execute properly in command line environment.

 151 views

45⟩ How To Create a Table using PHP?

If you want to create a table, you can run the CREATE TABLE statement as shown in the following sample script:

<?php

include "mysql_connection.php";

$sql = "CREATE TABLE ggl_links ("

. " id INTEGER NOT NULL"

. ", url VARCHAR(80) NOT NULL"

. ", notes VARCHAR(1024)"

. ", counts INTEGER"

. ", time TIMESTAMP DEFAULT sysdate()"

. ")";

if (mysql_query($sql, $con)) {

print("Table ggl_links created.n");

} else {

print("Table creation failed.n");

}

mysql_close($con);

?>

Remember that mysql_query() returns TRUE/FALSE on CREATE statements. If you run this script, you will get something like this:

Table ggl_links created.

 149 views

46⟩ How can we encrypt the username and password using PHP?

Answer1

You can encrypt a password with the following Mysql>SET PASSWORD=PASSWORD("Password");

Answer2

You can use the MySQL PASSWORD() function to encrypt username and password. For example,

INSERT into user (password, ...) VALUES (PASSWORD($password”)), ...);

 145 views

50⟩ How can we send mail using JavaScript?

No. There is no way to send emails directly using JavaScript.

But you can use JavaScript to execute a client side email program send the email using the "mailto" code. Here is an example:

function myfunction(form)

{

tdata=document.myform.tbox1.value;

location="mailto:mailid@domain.com?subject=...";

return true;

}

 150 views

53⟩ What is the purpose of the following files having extensions frm, myd, and myi? What these files contain?

In MySQL, the default table type is MyISAM.

Each MyISAM table is stored on disk in three files. The files have names that begin with the table name and have an extension to indicate the file type.

The '.frm' file stores the table definition.

The data file has a '.MYD' (MYData) extension.

The index file has a '.MYI' (MYIndex) extension,

 184 views

54⟩ What is the functionality of the function strstr and stristr?

strstr() returns part of a given string from the first occurrence of a given substring to the end of the string. For example: strstr("user@example.com","@") will return "@example.com".

stristr() is idential to strstr() except that it is case insensitive.

 153 views

56⟩ How To Protect Special Characters in Query String?

If you want to include special characters like spaces in the query string, you need to protect them by applying the urlencode() translation function. The script below shows how to use urlencode():

<?php

print("<html>");

print("<p>Please click the links below"

." to submit comments about GlobalGuideLine.com:</p>");

$comment = 'I want to say: "It's a good site! :->"';

$comment = urlencode($comment);

print("<p>"

."<a href="processing_forms.php?name=Guest&comment=$comment">"

."It's an excellent site!</a></p>");

$comment = 'This visitor said: "It's an average site! :-("';

$comment = urlencode($comment);

print("<p>"

.'<a href="processing_forms.php?'.$comment.'">'

."It's an average site.</a></p>");

print("</html>");

?>

 171 views

58⟩ WHAT ARE THE DIFFERENT TYPES OF ERRORS IN PHP?

Here are three basic types of runtime errors in PHP:

1. Notices: These are trivial, non-critical errors that PHP encounters while executing a script - for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all - although you can change this default behavior.

2. Warnings: These are more serious errors - for example, attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.

3. Fatal errors: These are critical errors - for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP's default behavior is to display them to the user when they take place.

Internally, these variations are represented by twelve different error types

 158 views