Questions and Answers
Question: You
are committing a file with bug fixes to the SVN, there is an issue with a
conflict in the file what does this mean? How do you solve it?
Answer:
A conflict happens because changes were made to the
same parts of the file in two different branches. You solve a conflict by
editing the parts where conflict occurred, remove markers that indicate the
conflict and add whatever changes you want to apply. This means that since the
last update, some changes have been committed to the SVN (most likely when a
project has multiple developers). A simple solution is to revert the current
changes and update the copy in progress i.e.
$ svn revert README
Reverted ‘README’
$ svn update README
Second, I may choose to keep my changes and dump the
earlier saved changes. This will ensure that all special files formally
generated are cleaned up.
Lastly, I can create a new version from both
conflicting files. Here I would manually edit the readme file and add or remove
any conflicting makers. The new file will be marked as resolved to ensure that
subversion enables committing.
Question:
How would you find out what files were out of date within SVN and commit those
specific files into a revision with a log message via the command line?
Answer:
`svn st` will report which files have local changes.`svn diff` each file, this avoids accidental white-space checkins
Question:
How do you use memcached to optimize mysql queries?
Answer:
Memcache is an object caching system and as such I use
it to store queries that are small and yet return small data sets. I use it to
create caching scripts. However, based on my experience not all DB queries are
suitable for caching especially where the database server caches queries. Here
is an example code of how I utilize memcache
//declare a global variable of memcache
global $memcache;
$memcache=new MemCache;
//create a function to get the value into memcache
function getCache($cacheKey){
global $memcache;
return ($memcache) ? $memcache-> get(cacheKey) :
false;
}
function setCache($key,$object,$timeout = 60) {
global
$memcache;
return
($memcache) ? $memcache->set($key,$object,MEMCACHE_COMPRESSED,$timeout) :
false;
}
# Caching
version of mysql_query()
function
mysql_query_cache($sql,$linkIdentifier = false,$timeout = 60) {
if
(($cache = getCache(md5("mysql_query" . $sql))) !== false) {
$cache = false;
$r =
($linkIdentifier !== false) ? mysql_query($sql,$linkIdentifier) :
mysql_query($sql);
if
(is_resource($r) && (($rows = mysql_num_rows($r)) !== 0)) {
for ($i=0;$i<$rows;$i++) {
$fields = mysql_num_fields($r);
$row = mysql_fetch_array($r);
for ($j=0;$j<$fields;$j++) {
if ($i === 0) {
$columns[$j] =
mysql_field_name($r,$j);
}
$cache[$i][$columns[$j]] =
$row[$j];
}
}
if (!setCache(md5("mysql_query" . $sql),$cache,$timeout)) {
# If we get here, there isn't a memcache daemon running or responding
}
}
}
return
$cache;
}
?>
Question:
How do you iterate an array in smarty?
Answer:
The following is an iteration of an array in smarty
Array
$data = array(10,11,12);
$smarty->assign('ourname',$data);
?>
{section name=ournames loop=$ourname}
{section ournames $ourname}
id:
{$ourname[ournames]}
{/section}
{section name=foo loop=$ourname step=-1}
{section foo $ourname step=-1}
{$ourname[foo]}
{/section}
Question:
PHP errors are not displaying on the page you are developing. You only have
SFTP Access to change files. How do you display the errors?
Answer:
Evaluate files using command-line php. ie: php
file.php
Question:
What is better and more efficient in development time for a large scale
enterprise PHP / MySQL application? Using packages and open source classes or
developing your own solutions?
Answer:
Using packages and open source classes (why reinvent
the wheel?)
Question:
What are the risks of using user inputted data in SQL queries and how to
eliminate this risk ?
Answer:
It leaves you open to SQL injection attacks. Eliminate
the risk by escaping all user input data
Question:
You have been given a specification and been briefed on a project. The
specification included work that was already completed for the project but
there are many bugs and you did not write this code. What do you do?
Answer:
Study the code, evaluate how easy it is to fix the
bugs as compared to writing new code. Select whichever outweighs the other.
Question:
There is a bug in your code, what do you do?
Answer:
Debug and fix it, test everything to ensure the fix
didn't break other things (automated tests are your friend here)
Question:
Recently users are complaining they are being logged out of the site. The site
uses sessions and php. Also recently a new webserver was added, bringing the
total to 2. What could be causing this and how would it be solved?
Answer:
The
two servers may cause users to flip from one server to another. The solution is
to petition management, to get a core
library upgrade. You replace the SESSION storage mechanism with the
database-backed one so either server may load the SESSION. Also I will check all the certs and
domain paths first on the COOKIEs, to avoid future problems.
Question:
You need to create a search function for a website with over 2.000 searches per
second on a record set of 100.000.000 text records. How would you do this
assuming data source is in MySQL?
Answer:
There are two options; one, use memcache or two, set
the express caching on the MYSQL server. Whichever provides the least response
time should be adopted.
Question:
You need to integrate an external website's data that does not have an API -
describe this process.
Answer:
Have the site send you files containing the data in a
structured format such as xml, json. Parse these files to extract whatever data
you need
Question:
You've written a news service in PHP that needs to be used by many external
websites. How would you allow those websites to use your functions to display
the news on their site?
Answer:
Create a Web service (e.g. using soap protocol) that
returns xml, json etc.
Question:
You want to create an AJAX suggestion box that displays in real time
suggestions related to the content of the search text field. Describe how this
process would work.
Answer:
Implement on key release action, this ensures
that as the user types on the text field every character is parsed to the
database (using Ajax, and a return of results using json) and filtered
accordingly and the return is based on the typed characters. Similar to how
Facebook search button works. Really simple to implement.