Category Archives: Programming and Tech Stuff

Basic jQuery AJAX syntax

Here’s another one for beginners.
The basic jQuery AJAX syntax you’ll use most often:

$.ajax({
	type: "POST", // you can also use here "GET"
	url: "some-url.php",
	data: {name: "john", location: "boton"}
}).done(function(msg){
	alert("returned message: "+msg);
});

Job Interview Quick questions and answers

Here’s a list of quick questions and answers you may find when going to a PHP Job Interview.

1. $name vs $$name
Answer: $name is a variable and $$name is a reference variable.

2. What is the difference between Classes and Interfaces?
Answer: Interfaces do not contain business logic; they only contain method signatures that define a template which all the classes implementing the interface must contain.
Example:

<?php
interface animal{
	function breath();
	function eat();
}
class dog implements animal{
	function breath(){
		echo 'breath';
	}
	function eat(){
		echo 'eat';
	}
	function bark(){
		echo 'ham! ham!';
	}
}
?>

3. What is the difference between include(); and require(); ?
Answer: include(); throws a warning if the included file cannot be accessed and require(); throws a fatal error.

4. How to encrypt and decrypt data with mysql?
Answer: by using AES_ENCRYPT(); and AES_DECRYPT();

5. What is a well-formed XML document?
Answer: It’s a document which adheres to the syntax rules specified by XML 1.0:
– content is defined
– content is delimited with beginning and end tags
– content is properly nested
– contains only properly encoded legal Unicode chars
– has no overlapping tags
– tags are case-sensitive, cannot start with numbers, minus, point and cannot contain the following !”$@()+/<>=?\[]

6.What is a valid-document XML?
Answer: A valid XML is a well-formed document which has DTD (Document Type Definition)

7. Enumerate Access Modifiers
Answer:
– public => anyone can access the item
– protected => only the same class and children of it can access
– private => only the same class can access
– final => cannot be overridden in the children classes
– abstract => cannot be used directly, it must be inherited from it first

8. Give example of simple external XML DTD
Answer:
<!ELEMENT people_list(person)*>
<!ELEMENT person(name,birthdate?)>
<!ELEMENT name(#PCDATA)>
<!ELEMENT birthdate(#PCDATA)>

notes:
* => there can be zero or more person elements
? => optional elements
#PCDATA => parsed character data

9. What are objects in OOP?
Answer: Objects are instantiated classes.

10. Name couple design patterns.
Answer:
– factory
– singleton
– strategy
– decorator
– MVC

Btw, here are some great articles on design patterns: Five design patterns and Five more design patterns

11. Describe what MVC initials stand for, in short.
Answer:
model =>
– representation of actual data
– data access code
– business logic code

view =>
– contains mark-up and design code

controller =>
– controls the actions to be executed
– calls the models
– renders the views
– defines what the data is

12. What do you know about the STATIC keyword?
Answer:
Declaring class properties or methods as static makes them accessible without needing an instance of the class.
STATIC can also be used to define static variables and for late static bindings.
Because static methods are callable without an instance of the object created, the pseudo-variable $this-> is not available inside the method declared as static.

13. What are Static Variables?
Answer: A static variable exists only in a local function scope, but does not loose it’s value when the program execution leaves this scope.

14. What about Late Static Bindings?
Answer:
– reference the called class in a context of static inheritance
– available since PHP 5.3.0
– example.: static::call();

15. How php session works?
Answer:
– a php session cookie is set in client browser
– on every request, the client sends that cookie to the server
– php uses that cookie to select corresponding session information

16. Big changes in PHP lately?
Answer:
– PHP 5.0 added OOP
– PHP 5.1 added PDO
– PHP 5.3 added namespace and late static bindings

17. Differences between $_GET and $_POST
Answer:
Every HTTP request contains a method, generally GET, PUT, POST, DELETE.

HTTP is an application protocol. It functions as a request-response protocol in the client-server computing model.

HTTP defines methods to indicate the desired action to be performed on the identified resource
=> GET – requests a representation of the data
=> HEAD – asks for the same response as GET without the body
=> POST – requests the server to accept the entity enclosed as a new subordinate of the resource
=> PUT – requests the enclosed entity to be stored under the supplied URI
=> DELETE – deletes the resource

* PUT vs POST => idempotent != non-idempotent

The fundamental difference between PUT and POST is reflected in the different meaning of the Request URI.

In POST, the URI identifies the resource that will handle the enclosed entity.

In PUT, the URI identifies the entity enclosed with the request.

POST may changes other entities.
PUT may change only the enclosed entity.

PUT = idempotent, aka calling PUT multiple times with the same data does nut further changes the data.

17. What is POLYMORPHYSM?
Answer:
An object can take on many forms.
A class CAR can have 2 classes that extend it, a class Honda and a class BMW.

18. How to autoload classes in PHP?
Answer:
By using spl_autoload_register(); it allows multiples autoloaders to be registered.
Example:

<?php
function my_autoloader($class){
	include 'classes/'.$class.'.class.php';
}
spl_autoload_register('my_autoloader');
?>

19. How to do a classic, basic, file upload (no need to write security or checks)
Answer:

<?php
if($_FILES["file"]["error"] == 0){
	move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$_FILES["file"]["name"]);
}
?>

20. How to create a file in php?
Answer 1:

<?php
$filename = 'some-file.txt';
if(($file = fopen($filename,"w")) == false)
	die("error");
fwrite($file, "some content here");
fclose($file);
?>

Answer 2:

<?php
file_put_contents('filename.txt', 'some content here');
?>

21. What does SHARDING mean?
Answer:
Sharding means splitting a database into several pieces, usually to improve the speed and reliability of the application.

Sharding is a synonym for “horizontal partitioning” or dividing a table by it’s rows.

In common use, sharding refers to having data split onto multiple servers.

Partitioning a tables splits the table by rows, usually on a singe instance of schema and database server.

Sharding a table splits a table across (potentially) multiple servers (physical or logical).

22. Why would you user ORDER By NULL?
Answer:

When using GROUP BY, mysql sorts group by col1, col2 as if you’ve requested ORDER BY col1, col2.

If you want to suppress the sorting, you can specify ORDER BY NULL.

22. How to swap the content of two variables x,y with no other variable?
Answer 1:

<?php
$x=27; $y=78;
$x = $x + $y;
$y = $x - $y;
$x = $x - $y;
?>

Answer 2:

<?php
$x=27; $y=78;
$x = $x ^ $y;
$y = $x ^ $y;
$x = $x ^ $y;
?>

23. Explain the differences between MySQL JOIN types.
Answer:

INNER JOIN => an output row is produced for every pair of input rows that match on the join conditions.

LEFT JOIN => same as above, except that if there is any row for which no matching row can be found in the table on the right, an output is generated with the values from the left table, with NULL for each value in the table in the right.

RIGHT JOIN => same as LEFT JOIN, but roles reversed.

FULL JOIN => a combination of left and right; every row from both tables will appear at least once in the output

CROSS JOIN => INNER JOIN without conditions, resulting in all pairs of rows being output.

24. How to free memory in PHP? How to measure it?
Answer:
– unset(); marks a variable for garbage collection, but the stack remains at the current value.
– setting the variable to NULL drops the data and shrinks the stack memory.

– memory_get_peak_usage(); and memory_get_usage();

25. Describe database normalization
Answer:
Normalization is the process of organizing efficiently data in a database.
– eliminate redundant data
– only store related data in a table.

The normal forms (NF)

1. First Normal Form (1NF)
– eliminate duplicate columns from the same table
– create separate table for each group of related data and identify each row by PK

2. Second Normal Form (2NF)
– remove subsets of data that apply to multiple rows of a table and place them in a separate table
– create relationships between these new tables and their predecessors trough the use of FK

3. Third Normal Form (3NF)
– remove columns that are not dependent upon the primary key

4. Boyce-Cold Normal Form (3.5NF)
– every determinant must be a CK (Candidate Key)

5. Fourth Normal Form (4NF)
– a relation is in 4NF if it has no multi-valued dependencies

Notes:
a. Candidate Key
– a candidate key is a combination of attributes that can be uniquely used to identify a database record without any extraneous data.
– each table may have one ore more CK
– one of these CK is the PK

b. Multi-valued dependencies
– MV dependencies occur when the presence of one or more rows in a table implies the presence of one or more other rows in that same table.

Page 3 of 3123