Aug 22, 2017

What are Traits?

Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

A Trait is simply a group of methods that you want include within another class. A Trait, like an abstract class, cannot be instantiated on it’s own.
 An example of a Trait could be:
trait Sharable {
 
  public function share($item)
  {
    return 'share this item';
  }
 
You could then include this Trait within other classes like this:
class Post {
 
  use Sharable;
 
}
 
class Comment {
 
  use Sharable;
 
}
 
 

How are Traits different to Abstract classes?

A Trait is different to an Abstract class (What are Abstract classes?) because they do not rely on inheritance.
Imagine if the Post and the Comment class had to inherit from a AbstractSocial class. We are most likely going to want to do more than just share posts and comments on social media websites, so we’ll probably end up with a complicated inheritance tree like this:
?



class AbstractValidate extends AbstractCache {}
class AbstractSocial extends AbstractValidate {}
class Post extends AbstractSocial {}
 

How are Traits different to Interfaces?

Traits kind of look a lot like Interfaces. Both Traits and interfaces are usually simple, concise and not much use without an actual implemented class. However the difference between the two is important.
An interface is a contract that says “this object is able to do this thing”, whereas a Trait is giving the object the ability to do the thing.
For example:

// Interface
interface Sociable {
 
  public function like();
  public function share();
 
}
 
// Trait
trait Sharable {
 
  public function share($item)
  {
    // share this item
  }
 
}
 
// Class
class Post implements Sociable {
 
  use Sharable;
 
  public function like()
  {
    //
  }
 
}

What are the benefits of Traits?

The benefit of using Traits is that you reduce code duplication whilst preventing complicated class inheritance that might not make sense within the context of your application.
This allows you to define simple Traits that are clear and concise and then mix in that functionality where appropriate.
 

Apr 13, 2009

PHP in OOPs

Definition of Access Specifiers
Access specifiers specify the level of access that the outside world (i.e. other class objects, external functions and global level code) have on the class methods and class data members. Access specifiers can either be public, private or protected.

1. Private
A private access specifier is used to hide the data member or member function to the outside world. This means that only the class that defines such data member and member functions have access them.

2. Public
A public access specifier provides the least protection to the internal data members and member functions. A public access specifier allows the outside world to access/modify the data members directly unlike the private access specifier. Look at the example below:

3. Protected
A protected access specifier is mainly used with inheritance. A data member or member function declared as protected will be accessed by its class and its base class but not from the outside world

nth highest Salaray

Question: select nth highest Salaray...
Answer: select sal as salary from emp order by sal desc limit n-1,1;

Dec 1, 2008

Mostly Asked Question

What is functioning of ob_start() and exec()?

ob_start(): This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

exec() : executes the given command, however it does not output anything. It simply returns the last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.

Sep 15, 2008

Latest Interview Question

What does a special set of tags do in PHP?
The output is displayed directly to the browser.

How do you define a constant?
Via define() directive, like define ("MYCONSTANT", 100);

I am trying to assign a variable the value of 0123, but it keeps coming up with a different number, what’s the problem?
PHP Interpreter treats numbers beginning with 0 as octal. Look at the similar PHP interview questions for more numeric problems.

Are objects passed by value or by reference?
Everything is passed by value.

What are the differences between DROP a table and TRUNCATE a table?
DROP TABLE table_name - This will delete the table and its data.

TRUNCATE TABLE table_name - This will delete the data of the table, but not the table definition.

How do you call a constructor for a parent class?
parent::constructor($value)

Mar 15, 2008

PHP Interview Question with their Answer

What's PHP ?
Answer: The PHP Hypertext Preprocessor is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web based software applications.

What Is a Session?
A session is a logical object created by the PHP engine to allow you to preserve data across subsequent HTTP requests.

There is only one session object available to your PHP scripts at any time. Data saved to the session by a script can be retrieved by the same script or another script when requested from the same visitor.

Sessions are commonly used to store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same visitor.

What is meant by PEAR in php?
Answer1:
PEAR is the next revolution in PHP. This repository is bringing higher level programming to PHP. PEAR is a framework and distribution system for reusable PHP components. It eases installation by bringing an automated wizard, and packing the strength and experience of PHP users into a nicely organised OOP library. PEAR also provides a command-line interface that can be used to automatically install "packages" .

Answer2:
PEAR is short for "PHP Extension and Application Repository" and is pronounced just like the fruit. The purpose of PEAR is to provide:
A structured library of open-sourced code for PHP users
A system for code distribution and package maintenance
A standard style for code written in PHP
The PHP Foundation Classes (PFC),
The PHP Extension Community Library (PECL),
A web site, mailing lists and download mirrors to support the PHP/PEAR community
PEAR is a community-driven project with the PEAR Group as the governing body. The project has been founded by Stig S. Bakken in 1999 and quite a lot of people have joined the project since then.

How can we know the number of days between two given dates using PHP?
Answer : Simple arithmetic:

$date1 = date('Y-m-d');
$date2 = '2006-07-01';
$days = (strtotime() - strtotime()) / (60 * 60 * 24);
echo "Number of days since '2006-07-01': $days";

How can we repair a MySQL table?
Answer :
The syntex for repairing a mysql table is:

REPAIR TABLE tablename
REPAIR TABLE tablename QUICK
REPAIR TABLE tablename EXTENDED

This command will repair the table specified.
If QUICK is given, MySQL will do a repair of only the index tree.
If EXTENDED is given, it will create index row by row.

What is the difference between $message and $$message?
Anwser 1:
$message is a simple variable whereas $$message is a reference variable. Example:
$user = 'bob'
is equivalent to
$holder = 'user';
$$holder = 'bob';

Anwser 2:
They are both variables. But $message is a variable with a fixed name. $$message is a variable who's name is stored in $message. For example, if $message contains "var", $$message is the same as $var.

What Is a Persistent Cookie?
Answer : A persistent cookie is a cookie which is stored in a cookie file permanently on the browser's computer. By default, cookies are created as temporary cookies which stored only in the browser's memory. When the browser is closed, temporary cookies will be erased. You should decide when to use temporary cookies and when to use persistent cookies based on their differences:
*Temporary cookies can not be used for tracking long-term information.
*Persistent cookies can be used for tracking long-term information.
*Temporary cookies are safer because no programs other than the browser can access them.
*Persistent cookies are less secure because users can open cookie files see the cookie values.
What does a special set of tags do in PHP?
The output is displayed directly to the browser.

How can we know that a session is started or not?
Answer : A session starts by session_start() function.
This session_start() is always declared in header portion. it always declares first. then we write session_register().

What are the differences between mysql_fetch_array(), mysql_fetch_object(), mysql_fetch_row()?
Answer 1:
mysql_fetch_array() -> Fetch a result row as a combination of associative array and regular array.
mysql_fetch_object() -> Fetch a result row as an object.
mysql_fetch_row() -> Fetch a result set as a regular array().

Answer 2:
The difference between mysql_fetch_row() and mysql_fetch_array() is that the first returns the results in a numeric array ($row[0], $row[1], etc.), while the latter returns a the results an array containing both numeric and associative keys ($row['name'], $row['email'], etc.). mysql_fetch_object() returns an object ($row->name, $row->email, etc.).

If we login more than one browser windows at the same time with same user and after that we close one window, then is the session is exist to other windows or not? And if yes then why? If no then why?
Answer :
Session depends on browser. If browser is closed then session is lost. The session data will be deleted after session time out. If connection is lost and you recreate connection, then session will continue in the browser.

What are the MySQL database files stored in system ?
Answer :

Data is stored in name.myd
Table structure is stored in name.frm
Index is stored in name.myi

What is the difference between PHP4 and PHP5?
Answer : PHP4 cannot support oops concepts and Zend engine 1 is used.

PHP5 supports oops concepts and Zend engine 2 is used.
Error supporting is increased in PHP5.
XML and SQLLite will is increased in PHP5.
Can we use include(abc.PHP) two times in a PHP page makeit.PHP”?
Yes we can include that many times we want, but here are some things to make sure of:
(including abc.PHP, the file names are case-sensitive)
there shouldn't be any duplicate function names, means there should not be functions or classes or variables with the same name in abc.PHP and makeit.php

What is meant by nl2br()?
Anwser1:
nl2br() inserts a HTML tag
before all new line characters \n in a string.

echo nl2br("god bless \n you");

output:
god bless

you

Feb 15, 2008

PHP/MYSQL Interview Question

1. Who is the father of PHP and explain the changes in PHP versions?
2. How can we submit a form without a submit button?
3. In how many ways we can retrieve the date in the result set of mysql using PHP?
4. What is the difference between mysql_fetch_object and mysql_fetch_array.?
4. What is the difference between $message and $$message?
5. How can we extract string ‘techinterviews.com ‘from a string ‘http://www.techinterviews.com’ using regular expression of PHP?
6. How can we create a database using PHP and mysql?
7. Can we use include (”techinterviews.php”) two times in a PHP page “makeit.PHP”?
8. What are the different tables present in mysql, which type of table is generated when we are creating a table in the following syntax: create table employee(eno int(2),ename varchar(10)) ?
9. Functions in IMAP, POP3 AND LDAP?
10. How can I execute a PHP script using command line?
11.Suppose your Zend engine supports the mode Then how can u configure your PHP Zend engine to support mode ?
12. Shopping cart online validation i.e. how can we configure Paypal, etc.?
13.What is meant by nl2br()?
14.Draw the architecture of Zend engine?
15.What are the current versions of apache, PHP, and mysql?
16.What are the reasons for selecting lamp (linux, apache, mysql, PHP) instead of combination of other software programmes, servers and operating systems?
17.How can we encrypt and decrypt a data present in a mysql table using mysql?
18.How can we encrypt the username and password using PHP?
19.What are the features and advantages of object-oriented programming?
20. What are the differences between procedure-oriented languages and object-oriented languages?
21. What is the use of friend function?
22.What are the differences between public, private, protected, static, transient, final and volatile?
23.What are the different types of errors in PHP?
24.What is the functionality of the function strstr and stristr?
25.What are the differences between PHP 3 and PHP 4 and PHP 5?
26.How can we convert asp pages to PHP pages?
27.What is the functionality of the function htmlentities?
28.How can we get second of the current time using date function?
29.How can we convert the time zones using PHP?
30.What is meant by urlencode and urldocode?
31.What is the difference between the functions unlink and unset?
32.How can we register the variables into a session?
33.How can we get the properties (size, type, width, height) of an image using PHP image functions?
34.How can we get the browser properties using PHP?
35.What is the maximum size of a file that can be uploaded using PHP and how can we change this?
36.How can we increase the execution time of a PHP script?
37.How can we take a backup of a mysql table and how can we restore it. ?
38.How can we optimize or increase the speed of a mysql select query?
39.How many ways can we get the value of current session id?
40.How can we destroy the session, how can we unset the variable of a session?
41.How can we destroy the cookie?
42.How many ways we can pass the variable through the navigation between the pages?
43.What is the difference between ereg_replace() and eregi_replace()?
44.What are the different functions in sorting an array?
45.How can we know the count/number of elements of an array?
46.What is the PHP predefined variable that tells the What types of images that PHP supports?
47.How can I know that a variable is a number or not using a JavaScript?
48.List out some tools through which we can draw E-R diagrams for mysql.
49.How can I retrieve values from one database server and store them in other database server using PHP?
50.List out the predefined classes in PHP?