Oct 25, 2014

What is Full form of PHP ? Who is the father or inventor of PHP ?

Rasmus Lerdorf is known as the father of PHP that started development of PHP in 1994
for their own Personal Home Page (PHP) and they released PHP/FI (Forms Interpreter) version 1.0 publicly on 8 June 1995 But in 1997 two Israeli developers named Zeev Suraski and Andi Gutmans rewrote the parser that formed the base of PHP 3 and then changed the language's name to the PHP: Hypertext Preprocessor.

Use of array_merge() in PHP ?

The array_merge() function merges one or more arrays into one array.
Tip: You can assign one array to the function, or as many as you like.
Note: If two or more array elements have the same key, the last one overrides the others.
Note: If you assign only one array to the array_merge() function, and the keys are integers, the function returns a new array with integer keys starting at 0 and increases by 1 for each value (See Example 1 below).
Tip: The difference between this function and the array_merge_recursive() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive() function makes the value as an array.


array_merge(array1,array2,array3...)


ParameterDescription
array1Required. Specifies an array
array2Optional. Specifies an array
array3,...Optional. Specifies an array

<?php
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
?>

What is array_key_exists() in PHP ?

The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.
Tip: Remember that if you skip the key when you specify an array, an integer key is generated, starting at 0 and increases by 1 for each value. (See example 2)


array_key_exists(key,array)

ParameterDescription
keyRequired. Specifies the key
arrayRequired. Specifies an array

<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5");
if (array_key_exists("Volvo",$a))
  {
  echo "Key exists!";
  }
else
  {
  echo "Key does not exist!";
  }
?>

What is array_filter() in PHP ?

The array_filter() function filters the values of an array using a callback function.
This function passes each value of the input array to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.


array_filter(array,callbackfunction);

ParameterDescription
arrayRequired. Specifies the array to filter
callbackfunctionRequired. Specifies the callback function to use


<?php
function test_odd($var)
{
return($var & 1);
}

$a1=array("a","b",2,3,4);
print_r(array_filter($a1,"test_odd"));
?>

PHP str_replace() Function

The str_replace() function replaces some characters with some other characters in a string.
This function works by the following rules:
  • If the string to be searched is an array, it returns an array
  • If the string to be searched is an array, find and replace is performed with every array element
  • If both find and replace are arrays, and replace has fewer elements than find, an empty string will be used as replace
  • If find is an array and replace is a string, the replace string will be used for every find value
Note: This function is case-sensitive. Use the str_ireplace() function to perform a case-insensitive search.

Note: This function is binary-safe.

Syntax : str_replace(find,replace,string,count)

 <?php
echo str_replace("world","Peter","Hello world!");
?>

Oct 16, 2014

What is urlencode and urldecode in PHP ?

Urlencode can be used to encode a string that can be used in a url. It encodes the same way posted data from web page is encoded. It returns the encoded string.Syntax: urlencode (string $str )Urldecode can be used to decode a string. Decodes any %## encoding in the given string (Inserted by urlencode)Syntax: urldecode (string $str )

urlencode () is the function that can be used conveniently to encode a string before using in a query part of a URL. This is a convenient way for passing variables to the next page.
urldecode() is the function that is used to decode the encoded string.        

How can we convert the time zones using PHP?

By using date_default_timezone_get and
date_default_timezone_set function on PHP 5.1.0
<?php
// Discover what 8am in Tokyo relates to on the East Coast of the US   

// Set the default timezone to Tokyo time:
date_default_timezone_set('Asia/Tokyo');   

// Now generate the timestamp for that particular timezone, on Jan 1st, 2000
$stamp = mktime(8, 0, 0, 1, 1, 2000);   

// Now set the timezone back to US/Eastern
date_default_timezone_set('US/Eastern');   

// Output the date in a standard format (RFC1123), this will print:
// Fri, 31 Dec 1999 18:00:00 EST
echo '<p>', date(DATE_RFC1123, $stamp) ,'</p>';?>

How can we get second of the current time using date function?

<?php
 $second = date(“s”);
?>

What is the functionality of the function htmlentities?


Convert all applicable characters to HTML entities
This function is identical to htmlspecialchars() in all ways, except
with htmlentities(), all characters which have HTML character entity
equivalents are translated into these entities.

How can we convert asp pages to PHP pages?


there are lots of tools available for asp to PHP conversion. you can
search Google for that. the best one is available at

http://asp2php.naken.cc./

What are the differences between PHP3, PHP4, PHP5, and PHP6 ?

There are lot of difference among PHP3 and PHP4 and PHP5 version of php as PHP6 is till in under development process so we are geting Difference mean oldest version have less functionality as compare to new one like below points
  • A> PHP3 is oldest stable version and it was pure procedural language constructive like C
  • B> Where as PHP4 have some OOPs concept added like class and object with new functionality
  • C> and in PHP5 approximately all major oops functionality has been added along with below thing
  • 1. Implementation of exceptions and exception handling
  • 2. Type hinting which allows you to force the type of a specific argument
  • 3. Overloading of methods through the __call function
  • 4. Full constructors and destructors etc through a __constuctor and __destructor function
  • 5. __autoload function for dynamically including certain include files depending on the class you are trying to create.
  • 6 Finality : can now use the final keyword to indicate that a method cannot be overridden by a child. You can also declare an entire class as final which prevents it from having any children at all.
  • 7 Interfaces & Abstract Classes
  • 8 Passed by Reference :
  • 9 An __clone method if you really want to duplicate an object
  • 10 Numbers of Functions Deprecated or removed in PHP 5.x like ereg,ereg_replace,magic_quotes, session_register,register_globals, split(), call_user_method() etc   

What is the functionality of the function strstr and stristr?


strstr() and stristr both are used to find the first occurence of the string.
stristr( ) is case insensitive and strstr( ) is case sensitive.

string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )

haystack
The input string.
needle
If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
before_needle
If TRUE, strstr() returns the part of the haystack before the first occurrence of the needle (excluding the needle). By default it is FALSE.
strstr() – Find first occurrence of a string
Syntax:
strstr ($string, string)

Example:

<?php
$email = 'siddharth@yaHoo.com';
$domain_name = strstr($email, 'H');
echo $domain_name;
?>

Output:Hoo.com
stristr() - Find first occurrence of a string (Case-insensitive)
Syntax:
stristr ($string, string)

Example:

<?php
$email = 'siddharth@yaHoo.com';
$domain_name = stristr($email, 'H');
echo $domain_name;
?>

Output: harth@yaHoo.com
If we want to return the string before the haystack then we need to write the code as:

<?php
$email = 'siddharth@yaHoo.com';
$domain_name = stristr($email, 'H', TRUE);
echo $domain_name;
?>

Output: sidd

What are the different types of errors in PHP?



Three are three types of errors: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, as you will
see, 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.

What are the differences between public, private, protected, static, transient, final and volatile?


Public:
 Public declared items can be accessed everywhere.

Protected: Protected limits access to inherited and parent classes (and to the class that defines the item).
Private: Private limits visibility only to the class that defines
the item.

Static: A static variable exists only in a local function scope,
but it does not lose its value when program execution leaves this scope.

Final: Final keyword prevents child classes from overriding a
method by prefixing the definition with final. If the class itself is
being defined final then it cannot be extended.

transient: A transient variable is a variable that may not
be serialized. 
volatile: a variable that might be concurrently modified by multiple
threads should be declared volatile. Variables declared to be volatile
will not be optimized by the compiler because their value can change at
any time.

What is the use of friend function?


Sometimes a function is best shared among a number of different
classes. Such functions can be declared either as member functions of
one class or as global functions. In either case they can be set to be
friends of other classes, by using a friend specifier in the class that
is admitting them. Such functions can use all attributes of the class
which names them as a friend, as if they were themselves members of that
class.
A friend declaration is essentially a prototype for a member function,
but instead of requiring an implementation with the name of that class
attached by the double colon syntax, a global function or member
function of another class provides the match.

What are the differences between procedure-oriented languages and object-oriented languages?


Traditional programming has the following characteristics:Functions are written sequentially, so that a change in programming can
affect any code that follows it.
If a function is used multiple times in a system (i.e., a piece of code
that manages the date), it is often simply cut and pasted into each
program (i.e., a change log, order function, fulfillment system, etc).
If a date change is needed (i.e., Y2K when the code needed to be changed
to handle four numerical digits instead of two), all these pieces of
code must be found, modified, and tested.
Code (sequences of computer instructions) and data (information on which
the instructions operates on) are kept separate. Multiple sets of code
can access and modify one set of data. One set of code may rely on data
in multiple places. Multiple sets of code and data are required to work
together. Changes made to any of the code sets and data sets can cause
problems through out the system.Object-Oriented programming takes a radically different approach:Code and data are merged into one indivisible item – an object (the
term “component” has also been used to describe an object.) An object is
an abstraction of a set of real-world things (for example, an object may
be created around “date”) The object would contain all information and
functionality for that thing (A date
object it may contain labels like January, February, Tuesday, Wednesday.
It may contain functionality that manages leap years, determines if it
is a business day or a holiday, etc., See Fig. 1). Ideally, information
about a particular thing should reside in only one place in a system.
The information within an object is encapsulated (or hidden) from the
rest of the system.
A system is composed of multiple objects (i.e., date function, reports,
order processing, etc., See Fig 2). When one object needs information
from another object, a request is sent asking for specific information.
(for example, a report object may need to know what today’s date is and
will send a request to the date object) These requests are called
messages and each object has an interface that manages messages.
OO programming languages include features such as “class”, “instance”,
“inheritance”, and “polymorphism” that increase the power and
flexibility of an object.

What are the features and advantages of object-oriented programming?


One of the main advantages of OO programming is its ease of
modification; objects can easily be modified and added to a system there
by reducing maintenance costs. OO programming is also considered to be
better at modeling the real world than is procedural programming. It
allows for more complicated and flexible interactions. OO systems are
also easier for non-technical personnel to understand and easier for
them to participate in the maintenance and enhancement of a system
because it appeals to natural human cognition patterns.
For some systems, an OO approach can speed development time since many
objects are standard across systems and can be reused. Components that
manage dates, shipping, shopping carts, etc. can be purchased and easily
modified for a specific system

Oct 15, 2014

What is meant by nl2br()?


Inserts HTML line breaks (<BR />) before all newlines in a string
string nl2br (string); Returns string with ” inserted before all
newlines. For example: echo nl2br(“god bless\n you”) will output “god
bless <br /> you” to your browser.

What are the differences between require and include, include_once and require_once?


The include() statement includes
and evaluates the specified file.The documentation below also applies to
require(). The two constructs
are identical in every way except how they handle
failure.
 include() produces a
Warning while
 require() results
in a Fatal Error. In other words, use
require() if you want a missing
file to halt processing of the page.
 
include() does not behave this way, the script will
continue regardless.
The include_once()
statement includes and evaluates the
specified file during the execution of
the script. This is a behavior similar
to the
 include()
statement, with the only difference
being that if the code from a file has
already been included, it will not be
included again. As the name suggests, it
will be included just once.
include_once()
should be used in cases where the same
file might be included and evaluated
more than once during a particular
execution of a script, and you want to
be sure that it is included exactly once
to avoid problems with function
redefinitions, variable value
reassignments, etc.
require_once()
should be used in cases where the same
file might be included and evaluated
more than once during a particular
execution of a script, and you want to
be sure that it is included exactly once
to avoid problems with function
redefinitions, variable value
reassignments, etc.

How can we create a database using PHP and MySQL?


We can create MySQL database with the use of
mysql_create_db(“Database Name”)

What is the difference between $message and $$message?

owing example.$message = “Mizan”;$$message = “is a moderator of PHPXperts.”;$message is a simple PHP variable that we are used to. But the
$$message is not a very familiar face. It creates a variable name $mizan
with the value “is a moderator of PHPXperts.” assigned. break it like
this${$message} => $mizanSometimes it is convenient to be able to have variable variable
names. That is, a variable name which can be set and used dynamically.

What is the difference between mysql_fetch_object and mysql_fetch_array?


mysql_fetch_object() is similar tomysql_fetch_array(), with one difference -
an object is returned, instead of an array. Indirectly, that means that
you can only access the data by the field names, and not by their
offsets (numbers are illegal property names).

In how many ways we can retrieve the data in the result set of MySQL using PHP?


You can do it by 4 Ways1. mysql_fetch_row.
2. mysql_fetch_array
3. mysql_fetch_object
4. mysql_fetch_assoc

How can we submit a form without a submit button?

The main idea behind this is to use Java script submit() function in
order to submit the form without explicitly clicking any submit button.
You can attach the document.formname.submit() method to onclick,
onchange events of different inputs and perform the form submission. you
can even built a timer function where you can automatically submit the
form after xx seconds once the loading is done (can be seen in online
test sites).

Who is the father of PHP and explain the changes in PHP versions?

Rasmus Lerdorf is known as the father of PHP.PHP/FI 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

What are the differences between Get and post methods in form submitting. give the case where we can use get and we can use post methods?


When to use GET or POST
The HTML 2.0 specification says, in section Form
Submission (and the HTML 4.0 specification repeats this with minor
stylistic changes):
–>If the processing of a form is idempotent
(i.e. it has no lasting observable effect on the state of the
world), then the form 
method should be GET. Many database searches
have no visible side-effects and make ideal applications of query
forms.

–>If the service associated with the processing of a form has side
effects (for example, modification of a database or subscription to
a service), the method should be POST.
How the form data is transmitted?
quotation from the HTML 4.0 specification
–> If the method is “get” – -, the user agent
takes the value of action, appends a ? to it, then appends the form
data set, encoded using the application/x-www-form-urlencoded
content type. The user agent then traverses the link to this URI. In
this scenario, form data are restricted to ASCII codes.
–> If the method is “post” –, the user agent conducts an HTTP post
transaction using the value of the action attribute and a message
created according to the content type specified by the enctype
attribute.
Quote from CGI FAQ
Firstly, the the HTTP protocol specifies
differing usages for the two methods. GET requests should always be
idempotent on the server. This means that whereas one GET request
might (rarely) change some state on the Server, two or more
identical requests will have no further effect.
This is a theoretical point which is also good
advice in practice. If a user hits “reload” on his/her browser, an
identical request will be sent to the server, potentially resulting
in two identical database or
guestbook entries, counter increments, etc. Browsers may reload a
GET URL automatically, particularly if cacheing is disabled (as is
usually the case with CGI output), but will typically prompt the
user before
re-submitting a POST request. This means you’re far less likely to
get inadvertently-repeated entries from POST.
GET is (in theory) the preferred method for
idempotent operations, such as querying a database, though it
matters little if you’re using a form. There is a further practical
constraint that many systems have built-in limits to the length of a
GET request they can handle: when the total size of a request (URL+params)
approaches or exceeds 1Kb, you are well-advised to use POST in any
case.
I would prefer POST when I don’t want the status to
be change when user resubmits. And GET
when it does not matter.

Explain the difference between include and require.

Require () and include () are the same with respect to handling failures. However, require () results in a fatal error and does not allow the processing of the page. i.e. include will allow the script to continue. 

What is Type juggle in php?

 Type Juggling means dealing with a variable type. In PHP a variables type is determined by the context in which it is used. If an integer value is assigned to a variable, it becomes an integer.
E.g. $var3= $var1 + $var2
Here, if $var1 is an integer. $var2 and $var3 will also be treated as integers.  

. How to set cookies in PHP?

Cookies are often used to track user information.
Cookies can be set in PHP using the setcookie() function.
Parameters are : name of the cookie, Value of cookie, time for expiry of cookie, path of the cookies location on server, domain, secure (TRUE or FALSE) indication whether the cookie is passed over a secure HTTPS, http only (TRUE) which will make the cookie accessible only through HTTP.
Returns TRUE or FALSE depending on whether the cookie was executed or not.

How can we increase the execution time of a php script?

Default time allowed for the PHP scripts to execute is 30s defined in the php.ini file. The function used is set_time_limit(int seconds). If the value passed is '0', it takes unlimited time. It should be noted that if the default timer is set to 30 sec and 20 sec is specified in set_time_limit(), the script will run for 45 secs.

What are the functions for IMAP?


IMAP is used for communicate with mail servers. It has a number of functions. Few of them are listed below:
Imap_alerts – Returns all the imap errors occurred
Imap_body – Reads the message body
Imap_check – Reads the current mail box
Imap_clearflag_full – Clears all flags
Imap_close – close and IMAP stream
Imap_delete – Delete message from current mailbox
Imap_delete_mailbox – Deletes a mailbox
Imap_fetchbody – Fetches body of message
Imap_fetchheader – Fetches header of message
Imap_headers – Returns headers for ALL messages
Imap_mail : send a mail
Imap_sort- Sorts imap messages

What are the functions for IMAP?

IMAP is used for communicate with mail servers. It has a number of functions. Few of them are listed below:
Imap_alerts – Returns all the imap errors occurred
Imap_body – Reads the message body
Imap_check – Reads the current mail box
Imap_clearflag_full – Clears all flags
Imap_close – close and IMAP stream
Imap_delete – Delete message from current mailbox
Imap_delete_mailbox – Deletes a mailbox
Imap_fetchbody – Fetches body of message
Imap_fetchheader – Fetches header of message
Imap_headers – Returns headers for ALL messages
Imap_mail : send a mail
Imap_sort- Sorts imap messages

Explain how to submit form without a submit button ?

A form data can be posted or submitted without the button in the following ways:
1. On OnClick event of a label in the form, a JavaScript function can be called to submit the form
e.g. document.form_name.submit()
2. Using a Hyperlink: On clicking the link, JavaScript function can be called
e.g <a.href=" javascript:document.MyForm.submit();">

Explain how to submit form without a submit button ?

A form data can be posted or submitted without the button in the following ways:
1. On OnClick event of a label in the form, a JavaScript function can be called to submit the form
e.g. document.form_name.submit()
2. Using a Hyperlink: On clicking the link, JavaScript function can be called
e.g <a.href=" javascript:document.MyForm.submit();">

What are the different types of errors in PHP?

Different types of errors are:
E_ERROR: A fatal error that causes script termination
E_WARNING: Run-time warning that does not cause script termination
E_PARSE: Compile time parse error.
E_NOTICE: Run time notice caused due to error in code
E_CORE_ERROR: Fatal errors that occur during PHP's initial startup (installation)
E_CORE_WARNING: Warnings that occur during PHP's initial startup 
E_COMPILE_ERROR: Fatal compile-time errors indication problem with script.
E_USER_ERROR: User-generated error message. 
E_USER_WARNING: User-generated warning message. 
E_USER_NOTICE: User-generated notice message. 
E_STRICT: Run-time notices.
E_RECOVERABLE_ERROR: Catchable fatal error indicating a dangerous error
E_ALL: Catches all errors and warnings 

What is urlencode and urldecode ?

URL encode can be used to encode a string that can be used in a url. It encodes the same way posted data from web page is encoded. It returns the encoded string.
Syntax: urlencode (string $str )
Urldecode can be used to decode a string. Decodes any %## encoding in the given string (Inserted by urlencode)
Syntax: urldecode (string $str )

Explain the differences between require and include, include_once() ?

Include () will include the file specified.
Include_once () will include the file only once even if the code of the file has been included before.
Require () and include () are the same with respect to handling failures. However, require () results in a fatal error and does not allow the processing of the page.

What is a Persistent Cookie?

Cookies are used to remember the users. Content of a Persistent cookie remains unchanged even when the browser is closed. ‘Remember me’ generally used for login is the best example for Persistent Cookie

Explain the difference between $message and $$message.

$message is used to store variable data. $$message can be used to store variable of a variable. Data stored in $message is fixed while data stored in $$message can be changed dynamically.
E.g. $var1 = 'Variable 1'
$$var1= 'variable2'
This can be interpreted as $ Variable 1='variable2';
For me to print value of both variables, I will write
$var1 $($var1)

What Is a Session in PHP?

A PHP session is no different from a normal session. It can be used to store information on the server for future use. However this storage is temporary and is flushed out when the site is closed. Sessions can start by first creating a session id (unique) for each user.
Syntax : session_start()
E.g. storing a customer's information.

Cakephp


Cakephp is a rapid development framework for PHP that provides an extensible architecture for developing, maintaining, and deploying applications. it uses commonly known design patterns like MVC,ORM within the convention over configuration paradigm, It also reduces development costs and helps developers write less code.

What is PHP ?

Ans. PHP (Hyper text Pre Processor) is a scripting language commonly used for web applications. PHP can be easily embedded in HTML. PHP generally runs on a web server. It is available for free and can be used across a variety of servers, operating systems and platforms.