PHP Interview Questions

Level: Freshers

Q 81 - What are the different errors in PHP?

There are 4 basically types of error.
 
Example: <ul> <li>Parse Error &ndash; Commonly caused due to syntax mistakes in codes e.g. missing semicolon, mismatch brackets.</li> <li>Fatal Error &ndash; These are basically run time errors which are caused when you try to access what can&rsquo;t be done. E.g. accessing a dead object, or trying to use a function that hasn&rsquo;t been declared.</li> <li>Warning Error &ndash; These occurs when u try to include a file that is not present, or delete a file that is not on the server. This will not halt the script; it will give the notice and continue with the next line of the script.</li> <li>Notice Error &ndash; These errors occurs when u try to use a variable that hasn&rsquo;t been declared, this will not halt the script, It will give the notice and continue with the next line of the script.</li> </ul>

Q 82 - What is an associative array?

Associative arrays are arrays that use named keys that you assign to them.
Example: &nbsp; <br /> &nbsp;&nbsp; &nbsp; <em>&lt;?php</em>&nbsp; &nbsp; $capitals=array(&quot;India&quot;=&gt;&quot;New Delhi&quot;,&quot;China&quot;=&gt;&quot;Beijing&quot;,&quot;Pakistan&quot;=&gt;&quot;Islamabad&quot;); <em>?&gt;</em> &nbsp; &nbsp; &nbsp;

Q 83 - What is PDO classes?
 

The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. It is a data-access abstraction layer, so no matter what database we use the function to issue queries and fetch data will be same. Using PDO drivers we can connect to database like DB2, Oracle, PostgreSQL etc.

Q 84 - What are the different types of PHP variables?

PHP has a total of eight data types which we use to construct our variables −
Example: <ul> <li>Integers &minus; are whole numbers, without a decimal point, like 4195.</li> <li>Doubles &minus; are floating-point numbers, like 3.14159 or 49.1.</li> <li>Booleans &minus; have only two possible values either true or false.</li> <li>NULL &minus; is a special type that only has one value: NULL.</li> <li>Strings &minus; are sequences of characters, like &#39;PHP supports string operations.&#39;</li> <li>Arrays &minus; are named and indexed collections of other values.</li> <li>Objects &minus; are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.</li> <li>Resources &minus; are special variables that hold references to resources external to PHP (such as database connections).<br /> &nbsp;</li> </ul>

Q 85 - What are the differences between PHP constants and variables?

  • There is no need to write a dollar sign ($) before a constant, where as in Variable one has to write a dollar sign.
  • Constants cannot be defined by simple assignment, they may only be defined using the define() function.
  • Constants may be defined and accessed anywhere without regard to variable scoping rules.
  • Once the Constants have been set, may not be redefined or undefined.

Q 86 - Explain the syntax for 'foreach' loop.

The foreach statement is used to loop through arrays. For each pass the value of the current array element is assigned to $value and the array pointer is moved by one and in the next pass next element will be processed.
Example: foreach (array as value) { code to be executed; } &nbsp; &nbsp; &nbsp;

Q 87 - How will you concatenate two strings in PHP?

To concatenate two string variables together, use the dot (.) operator −
Example: <em>&lt;?php</em> $string1=&quot;Hello World&quot;; $string2=&quot;1234&quot;;<br /> echo $string1 . &quot; &quot; . $string2;<br /> <em>?&gt;</em> &nbsp; This will produce following result &minus; Hello World 1234<br /> &nbsp; <br /> &nbsp; &nbsp;

Q 88 - How will you generate random numbers using PHP?

The PHP rand() function is used to generate a random number. This function can generate numbers with-in a given range. The random number generator should be seeded to prevent a regular pattern of numbers being generated. 

Q 89 - What is the purpse $_REQUEST variable?.

The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. We will discuss $_COOKIE variable when we will explain about cookies. The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.