Senior PHP Programmer

  Home  Computer Programming  Senior PHP Programmer


“Sr. PHP Programmer based Frequently Asked Questions by expert members with experience as Senior PHP Programmer. These questions and answers will help you strengthen your technical skills, prepare for the new job test and quickly revise the concepts”



202 Senior PHP Programmer Questions And Answers

101⟩ What are the rules for determine the "truth" of any value not already of the Boolean type?

Here are the rules for determine the "truth" of any value not already of the Boolean type −

☛ If the value is a number, it is false if exactly equal to zero and true otherwise.

☛ If the value is a string, it is false if the string is empty (has zero characters) or is the string "0", and is true otherwise.

☛ Values of type NULL are always false.

☛ If the value is an array, it is false if it contains no other values, and it is true otherwise. For an object, containing a value means having a member variable that has been assigned a value.

☛ Valid resources are true (although some functions that return resources when they are successful will return FALSE when unsuccessful).

☛ Don't use double as Booleans.

 207 views

102⟩ What are SQL Injections, how do you prevent them and what are the best practices?

SQL injections are a method to alter a query in a SQL statement send to the database server. That modified query then might leak information like username/password combinations and can help the intruder to further compromise the server.

To prevent SQL injections, one should always check & escape all user input. In PHP, this is easily forgotten due to the easy access to $_GET & $_POST, and is often forgotten by inexperienced developers. But there are also many other ways that users can manipulate variables used in a SQL query through cookies or even uploaded files (filenames). The only real protection is to use prepared statements everywhere consistently.

Do not use any of the mysql_* functions which have been deprecated since PHP 5.5 ,but rather use PDO, as it allows you to use other servers than MySQL out of the box. mysqli_* are still an option, but there is no real reason nowadays not to use PDO, ODBC or DBA to get real abstraction. Ideally you want to use Doctrine or Propel to get rid of writing SQL queries all together and use object-relational mapping which binds your rows from the database to objects in your application.

 272 views

103⟩ How should we parse HTML in PHP?

What exactly is meant by the sentence "For a complex resource such as an HTML document, the script would need

to parse it to find references to embedded, included resources:

javascript files, CSS files, iframes, etc."?

Well, as you probably know, an HTML page often uses other files to render the HTML page - like CSS file(s) for styling, Javascript file(s) for adding more functionality to the HTML page, and so on. But the question is how do we take an HTML page and find all of those resources in the HTML page. Of course, this is easy to do if we are reading the HTML page with the human eye. But, we want to find these resources using a program that will read the HTML for us. This is actually more complicated than it seems - and the process by which a program (like PHP) reads an HTML file and analyzes the text to extract meaningful data (like resources) is known as parsing the HTML. Any text can be parsed, but we are exclusively focused on HTML for the purpose of this interview question.

 202 views

104⟩ What is Object Oriented Programming in PHP context?

This is usually a pretty open ended question. You should understand classes (objects are instantiated classes), abstract classes, interfaces, methods, properties,inheritance, multiple inheritance as well as why OOP is helpful as compared to procedural programming.

 201 views

105⟩ How to find the download size of a file?

The question also asks us to find the total download size of a URL. But what if that URL passed into the script just points to a single file resource like a JPG file or a GIF file? Well, for a single file resource we just need to find the size of that particular file and then return it as the answer, and we are done. But, for an HTML document we will need to find the total size of all resources that are embedded and included on the page and return that as the answer - because you must remember that we want the total download size of a URL.

So, let's write a PHP function that will return the download size of a single file resource. How should we approach writing this function - what is the easiest way to find the download size of a single file resource on the web?

Well, there is an HTTP header called "Content-Length" which will actually tell us the size of a particular resource file in the HTTP response (after the resource is requested). So, all we have to do is use PHP's built in "get_headers" function, which will retrieve all the HTTP headers sent by the server in response to an HTTP request.

The get_headers function accepts a URL as an argument. So, the PHP code to retrieve the "Content-Length" header would look like this:

function get_remote_file_size($url) {

$headers = get_headers($url, 1);

if (isset($headers['Content-Length']))

return $headers['Content-Length'];

//checks for lower case "L" in Content-length:

if (isset($headers['Content-length']))

return $headers['Content-length'];

}

But, there is actually a problem with this code: you will not always receive the Content-Length header in an HTTP response. In other words, the HTTP Content-Length header is not guaranteed to be sent back by the web server hosting that particular URL, because it depends on the configuration of the server. This means that you need an alternative that always works in case the approach above fails.

 187 views

106⟩ What are getters and setters and why are they important?

Getters and setters are methods used to declare or obtain the values of variables, usually private ones. They are important because it allows for a central location that is able to handle data prior to declaring it or returning it to the developer. Within a getter or setter you are able to consistently handle data that will eventually be passed into a variable or additional functions. An example of this would be a user's name. If you are not using a setter and just declaring the $userName variable by hand you could end up with results as such: "kevin", "KEVIN", "KeViN", "", etc. With a setter you can not only adjust the value, for example, ucfirst($userName), but you can also handle situations where the data is not valid such as the example where "" is passed. The same applies to a getter - when the data is being returned, you can modify the results to include strtoupper($userName) for proper formatting further up the chain.

This is important for any developer who is looking to enter a team-based / application development job to know. Getters and setters are often used when dealing with objects, especially ones that will end up in a database or other storage medium. Because PHP is commonly used to build web applications you will run across getters and setters in more advanced environments, even as a junior developer. They are extremely powerful yet not talked about very much. You can really impress an interviewer by knowing what they are and how to use them early on.

 208 views

107⟩ What is the purpose of constant() function?

As indicated by the name, this function will return the value of the constant. This is useful when you want to retrieve value of a constant, but you do not know its name, i.e. It is stored in a variable or returned by a function.

<?php

define("GlobalGuideline", 50);

echo GlobalGuideline;

echo constant("GlobalGuideline"); // same thing as the previous line

?>

Only scalar data (boolean, integer, float and string) can be contained in constants.

 206 views

108⟩ How will you send an email using PHP?

PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters.

mail( to, subject, message, headers, parameters );

 193 views

111⟩ How split() function works?

The split() function will divide a string into various elements, the boundaries of each element based on the occurrence of pattern in string.

 192 views

115⟩ What is the use of explode() function?

Syntax : array explode ( string $delimiter , string $string [, int $limit ] );

This function breaks a string into an array. Each of the array elements is a substring of string formed by splitting it on boundaries formed by the string delimiter

 237 views

117⟩ What is the use of final keyword in PHP?

PHP 5 introduces the final keyword, which 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.

 220 views

118⟩ How to create a text file in php?

$filename = "/home/user/guest/newfile.txt";

$file = fopen( $filename, "w" );

if( $file == false )

{

echo ( "Error in opening new file" ); exit();

}

fwrite( $file, "This is a simple testing global guideline filen" );

fclose( $file );

 223 views

119⟩ How to create an array of a group of items inside an HTML form?

We can create input fields with same name for "name" attribute with squire bracket at the end of the name of the name attribute, It passes data as an array to PHP.

For instance :

<input name="MyArray[]" />

<input name="MyArray[]" />

<input name="MyArray[]" />

<input name="MyArray[]" />

 224 views