Showing posts with label php tutorials. Show all posts
Showing posts with label php tutorials. Show all posts

PHP Interview questions

1. What is the difference between include and require in PHP?


They are identical in every aspect, but they perform error handling in different ways. The include() function generates a warning (which does not halt execution) while the require() function generates a fatal error (which stops execution immediately).


2. Difference between strstr and stristr in php ?


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.
PHP Interview questions



3.What are the different types of 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 behaviour.

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 behaviour is
to display them to the user when they take place.

4. Parse Error: When we make mistake in PHP code like,
missing
semicolon or any unexpected symbol in code




4. How to detect what encoding is done on string in PHP?

We use Function

mb_detect_encoding($str);



5.PHP Exception Handling


a. Try - A function using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is "thrown"

b. Throw - This is how you trigger an exception. Each "throw" must have at least one "catch"

c. Catch - A "catch" block retrieves an exception and creates an object containing the exception information

PHP Interview questions



5.What is SQL Injection? and How to Prevent it?


a.Sql Injection is used to hack a Site , it can be prevented by mysql_real_escape_string() function of PHP.



6.Difference When single quotes and double quotes are used in php?



$str = ‘hello world’;

echo ‘$str this is my world.’;

?>

Output will be:: $str this is my world.

In case of double quoted string…

$str = ‘hello world’;

echo “$str this is my world. “;

?>

Output will be:: hello world this is my world.



7. Five Array functions(excluding sort) and five String functions?



Ajax Questions



1. Full form of Ajax?

"Asynchronous JavaScript and XML"



2. What is the main strength of AJAX? Or why and when should we use AJAX in web application?

And the most common answer is – "AJAX Retrieves the value from server without without refreshing or reloading the whole page"

But, the more perfect answer is – "AJAX is an asynchronous technology where others request are synchronous."


PHP Interview questions


3. Difference Between synchronous and asynchronous ?


Cutting it Breaf, a program does not wait for response after requesting an asynchronous call whereas synchronous does so.



Here is a simple example –



function check_answer() {

var a=0;

a = getAnswer(“getanswer.php?id=10”);

if(a==1) {

alert(“present”);

} else {

alert(“not present”);

}

}




Here getAnswer() function sends a AJAX request to the server with “getanswer.php?id=10” url and the php file decides (from database may be) the answer and output/response as 1 or 0.



But, this function will not work properly. It will alert “not present” instead of “present”. And yes, that is for the asynchronous request.



The reason is – when a = getAnswer(“getanswer.php?id=10”); line is being executed program does not wait for the response of setAnswer() function. So, value of keep unchanged or set to null.



So, how should we work with asynchronous request?



Of course, using callback function. Callback function is that function which is triggered when the request completes to get the response (or as defined).





MySql Questions



1. What is Indexing ? Why is it used ?

Indexing is used to improve the speed of operations in a table.
The users are unable to see the indexes, they are just used to speed up queries
and will be used by Database Search Engine to locate records very fast.


Problem is : That when you update a table with an index, you have to update the index as well.
This means that if you are constantly updating, inserting and removing entries
in your table this could have a negative impact on performance.
PHP Interview questions



2. What is stored procedure?


A stored procedure is simply some MySQL statement. Almost any valid MySQL can go inside a stored procedure, apart from few.

Advantage :
Stored procedure increases performance of application. Once created, stored procedure is compiled and stored in the database catalog. It runs faster than uncompiled SQL commands which are sent from application.

Disadvantage :
Stored procedures make the database server high load in both memory for and processors. Instead of being focused on the storing and retrieving data, you could be asking the database server to perform a number of logical operations or a complex of business logic which is not the well designed in database server.



3. How can we remove duplicate values from a table?

Ans. Using having and Groupby function in Mysql query (Obviously with the query example)



4. How to import highly heavy database to local machine?

Ans Using command Prompt :

In Windows :

Browse the path to bin.

C:\xampp\mysql\bin> mysql -u root -p new_db_name_in_local


Note : Sql file name_of_db_to_import.sql should be placed in \mysql\bin directory



In Linux :

$ zcat database.gz | mysql -u username -p database_name




4. How to optimize mysql queries

Ans Using Explain method.




CSS Questions



1.What happens if class with same name is being called in two different Style-Sheets And an Attribute from second file is being called where we want first file to be called?

Use !important




Drupal Questions



1.Are there any challenges you face when you move your Site from local to Live Server?

Very commonly faced problem can be with regards to Clean URL which does not work.
For which we have to enable Mod ReWrite in Apache Configuration
And place Rewrite All line in httpd.conf file .




Linux Commands Questions



Rename a file in Linux Command?

mv oldfile_name newfile_name




Jquery Questions



Describe the difference between .bind() and .live() methods in jQuery?
PHP Interview questions


Both almost carry the same functionality however follow different DOM Structure.


$('a').bind('click', function() { alert("Test .bind ") });


This is the most straight forward binding method. jQuery scans the document for all $('a') elements and binds the alert function to each of their click events


$('a').live('click', function() { alert("Test . live ") });


jQuery binds the alert function to the $(document) element, along with 'click' and 'a' as parameters. Any time an event bubbles up to the document node, it checks to see if the event was a click and if the target element of that event matches the 'a' CSS selector. If both are true, the function executes.


The live method can also be bound to a specific element (or “context”) other than document, like this:


$('a', $('#container')[0]).live(...);

Article Source:www.Planetghost.com

Read More...

Introduction to PHP Difference between HTML and PHP

You are here
Home » Introduction to PHP Difference between HTML and PHP
Introduction to PHP Difference between HTML and PHP



When you know HTML which is efficient enough to make a website.
Why we need PHP?

What is PHP?

For instance, when we use google we enter the search string in
the text field it is not possible for each result to be present in that page.

The request is sent to server and the appropriate results is filtered and shown in the result page. This is not possible using only HTML.


One more example is if you are showing time or date in your website.

It is practically impossible to show updated time to the visitor
browsing your Site. However this could be made possible using a
dynamic web development language(php,asp.net,asp,jsp etc)
which automatically updates your time displayed in your Site.


PHP Code Runs in Server and not in Browser.
For Example.

Copy the Following HTML Code in text-editor (Notepad) and
save it with name hello.html

<html>
<head><title>My First Page</title></head>
<body>Hello World</body>
</html>



Make Sure its hello.html and not hello.html.txt
Now you can open this File on Browser(Internet Explorer, Mozilla , Chrome , Safari)

You will see something like this Hello World with white background

Now you can Right Click on Browser and click on "View Page Source" or "View Source" or "Source Code" As per Browser.
And you will see the above code again

<html>
<head><title>My First Page</title></head>
<body>Hello World</body>
</html>

which you had saved in text editor with .html ext.
Now we will give the same output using PHP.

Note: To Run PHP Code in your Machine you need to install PHP Server in your Machine
if you dont have it you can Learn how to Install PHP Apache Software(Xampp) and Run PHP in Windows or
Learn how to Install(LAMP) and Run PHP Code in Linux

Copy the Below Code in text-editor (Notepad) and
save it with name hello.php (Place it in your Web Server Directory "c:\\Xampp\htdocs\hello.php" (Xampp) or "/var/www/hello.php" (Lamp)
or depends on which php software you using with its particular www directory)

<html>
<head><title>My First Page</title></head>
<body><?php echo"Hello World" ; ?></body>
</html>

Now you can open Browser(Internet Explorer, Mozilla , Chrome , Safari) and type url "http://localhost/hello.php"

You will see somethig like this Hello World with white backgound again.

Now you can Right Click on Browser and click on "View Page Source" or "View Source" or "Source Code" As per Browser.

And you will see the above code which is different from the one you typed in file hello.php
this implies the code between <?php ?> is processed and run in server and then the code is thrown to browser.

<html>
<head><title>My First Page</title></head>
<body>Hello World</body>
</html>

Article Source:www.Planetghost.com

Read More...

Send Emails from localhost Xampp Wamp Lamp in PHP

How do we send emails from XAMPP or WAMP or LAMP(localhost) in PHP in Windows. And send mails using PHP scripts.


a) Open the "php.ini". For XAMPP,it is located in C:\XAMPP\php\php.ini. Find out if you are using
WAMP or LAMP server. Note : Make a backup of php.ini file

b) Search [mail function] in the php.ini file.

You can find like below.
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25


; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = postmaster@localhost


Change the localhost to the smtp server name of your ISP. No need to change the smtp_port. Leave it as 25. Change sendmail_from from postmaster@localhost to your domain email address which will be used as from address..

So for me, it will become like this.
[mail function]
; For Win32 only.
SMTP = smtp.planetghost.com
smtp_port = 25
; For Win32 only.
sendmail_from = info@planetghost.com


c) Restart the XAMPP or WAMP(apache server) so that changes will start working.

d) Now try to send the mail using the mail() function ,

mail("dineshnbhatia@gmail.com","Success","Great, Localhost Mail works");

Mail will be sent to "dineshnbhatia@gmail.com" from the localhost with Subject line "Success" and body "Great,Localhost Mail works"

Article Source:www.Planetghost.com

Read More...
 
Copyright (c) 2016 programmertrends