|
1 |
| -# php-interview-questions |
| 1 | +<div data-v-5e9078c0=""><h1 data-v-5e9078c0="">Top 82 PHP interview |
| 2 | + questions and answers in 2021.</h1> <p data-v-5e9078c0=""> |
| 3 | + You can check all |
| 4 | + 82 |
| 5 | + PHP interview questions here 👉 |
| 6 | + https://devinterview.io/dev/php-interview-questions |
| 7 | + </p> <br data-v-5e9078c0=""> <div data-v-5e9078c0="" class="unit"><div><h2>🔹 1. What is the use of ini_set()?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p>PHP allows the user to modify some of its settings mentioned in php.ini using ini_set(). This function requires two string arguments. First one is the name of the setting to be modified and the second one is the new value to be assigned to it.</p><p>Given line of code will enable the display_error setting for the script if it’s disabled.</p><p><code>ini_set('display_errors', '1');</code></p><p>We need to put the above statement, at the top of the script so that, the setting remains enabled till the end. Also, the values set via ini_set() are applicable, only to the current script. Thereafter, PHP will start using the original values from php.ini.</p></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://github.com/Bootsity/cracking-php-interviews-book" rel="noreferrer" target="_blank" title="What is the use of ini_set()? Interview Questions Source To Answer">github.com/Bootsity</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 2. What is the difference between == and ===?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><ul><li>The operator <code>==</code> casts between two different types if they are different</li><li>The <code>===</code> operator performs a '<em>typesafe comparison</em>'</li></ul><p>That means that it will only return true if both operands have the same type and the same value.</p><pre><code><span class="token cNum">1</span> <span class="token cBase">===</span> <span class="token cNum">1</span><span class="token cBase">:</span> <span class="token cMod">true</span> |
| 8 | +<span class="token cNum">1</span> <span class="token cBase">==</span> <span class="token cNum">1</span><span class="token cBase">:</span> <span class="token cMod">true</span> |
| 9 | +<span class="token cNum">1</span> <span class="token cBase">===</span> <span class="token cString">"1"</span><span class="token cBase">:</span> <span class="token cMod">false</span> <span class="token cComment">// 1 is an integer, "1" is a string</span> |
| 10 | +<span class="token cNum">1</span> <span class="token cBase">==</span> <span class="token cString">"1"</span><span class="token cBase">:</span> <span class="token cMod">true</span> <span class="token cComment">// "1" gets casted to an integer, which is 1</span> |
| 11 | +<span class="token cString">"foo"</span> <span class="token cBase">===</span> <span class="token cString">"foo"</span><span class="token cBase">:</span> <span class="token cMod">true</span> <span class="token cComment">// both operands are strings and have the same value</span></code></pre></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://stackoverflow.com/questions/80646/how-do-the-php-equality-double-equals-and-identity-triple-equals-comp" rel="noreferrer" target="_blank" title="What is the difference between == and ===? Interview Questions Source To Answer">stackoverflow.com</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 3. What is the return type of a function that doesn't return anything?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p><code>void</code> which mean nothing.</p></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://github.com/Bootsity/cracking-php-interviews-book" rel="noreferrer" target="_blank" title="What is the return type of a function that doesn't return anything? Interview Questions Source To Answer">github.com/Bootsity</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 4. What does $GLOBALS mean?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p><code>$GLOBALS</code> is associative array including references to all variables which are currently defined in the global scope of the script.</p></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://www.guru99.com/php-interview-questions-answers.html" rel="noreferrer" target="_blank" title="What does $GLOBALS mean? Interview Questions Source To Answer">guru99.com</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 5. What are the keys & values in an indexed array?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p>Consider:</p><pre><code><span class="token cVar">Array</span> <span class="token cBase">(</span> <span class="token cBase">[</span><span class="token cNum">0</span><span class="token cBase">]</span> <span class="token cBase">=</span><span class="token cBase">></span> Hello <span class="token cBase">[</span><span class="token cNum">1</span><span class="token cBase">]</span> <span class="token cBase">=</span><span class="token cBase">></span> world <span class="token cBase">[</span><span class="token cNum">2</span><span class="token cBase">]</span> <span class="token cBase">=</span><span class="token cBase">></span> It's <span class="token cBase">[</span><span class="token cNum">3</span><span class="token cBase">]</span> <span class="token cBase">=</span><span class="token cBase">></span> a <span class="token cBase">[</span><span class="token cNum">4</span><span class="token cBase">]</span> <span class="token cBase">=</span><span class="token cBase">></span> beautiful <span class="token cBase">[</span><span class="token cNum">5</span><span class="token cBase">]</span> <span class="token cBase">=</span><span class="token cBase">></span> day<span class="token cBase">)</span></code></pre><p>The keys of an indexed array are 0, 1, 2 etc. (the index values) and values are "Hello", "world", "It's", "beautiful", "day".</p></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://github.com/Bootsity/cracking-php-interviews-book" rel="noreferrer" target="_blank" title="What are the keys & values in an indexed array? Interview Questions Source To Answer">github.com/Bootsity</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 6. What is the purpose of php.ini file?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p>The PHP configuration file, <em>php.ini</em>, is the final and most immediate way to affect PHP's functionality. The php.ini file is read each time PHP is initialized.in other words, whenever httpd is restarted for the module version or with each script execution for the CGI version.</p></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://github.com/Bootsity/cracking-php-interviews-book" rel="noreferrer" target="_blank" title="What is the purpose of php.ini file? Interview Questions Source To Answer">github.com/Bootsity</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 7. How can you pass a variable by reference?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p>To be able to pass a variable by <strong>reference</strong>, we use an <em>ampersand</em> in front of it, as follows:</p><pre><code><span class="token cVar">$var1</span> <span class="token cBase">=</span> <span class="token cBase">&</span><span class="token cVar">$var2</span></code></pre></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://www.guru99.com/php-interview-questions-answers.html" rel="noreferrer" target="_blank" title="How can you pass a variable by reference? Interview Questions Source To Answer">guru99.com</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 8. Is multiple inheritance supported in PHP?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p>PHP supports only single inheritance; it means that a class can be extended from only one single class using the keyword 'extended'.</p></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://www.guru99.com/php-interview-questions-answers.html" rel="noreferrer" target="_blank" title=" Is multiple inheritance supported in PHP? Interview Questions Source To Answer">guru99.com</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 9. What is stdClass in PHP?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p><code>stdClass</code> is just a generic 'empty' class that's used when casting other types to objects. <code>stdClass</code> is <strong>not</strong> the base class for objects in PHP. This can be demonstrated fairly easily:</p><pre><code><span class="token cVar">class</span> <span class="token class-name">Foo</span><span class="token cBase">{</span><span class="token cBase">}</span> |
| 12 | +$foo <span class="token cBase">=</span> <span class="token cVar">new</span> <span class="token class-name">Foo</span><span class="token cBase">(</span><span class="token cBase">)</span><span class="token cBase">;</span> |
| 13 | +<span class="token cMod">echo</span> <span class="token cBase">(</span>$foo <span class="token cVar">instanceof</span> <span class="token class-name">stdClass</span><span class="token cBase">)</span><span class="token cBase">?</span><span class="token cString">'Y'</span><span class="token cBase">:</span><span class="token cString">'N'</span><span class="token cBase">;</span> <span class="token cComment">// outputs 'N'</span></code></pre><p>It is useful for anonymous objects, dynamic properties, etc. </p><p>An easy way to consider the <code>StdClass</code> is as an alternative to associative array. See this example below that shows how <code>json_decode()</code> allows to get an StdClass instance or an associative array. |
| 14 | +Also but not shown in this example, <code>SoapClient::__soapCall</code> returns an <code>StdClass</code> instance.</p><pre><code><span class="token cComment">//Example with StdClass</span> |
| 15 | +$json <span class="token cBase">=</span> <span class="token cString">'{ "foo": "bar", "number": 42 }'</span><span class="token cBase">;</span> |
| 16 | +$stdInstance <span class="token cBase">=</span> <span class="token cMod">json_decode</span><span class="token cBase">(</span>$json<span class="token cBase">)</span><span class="token cBase">;</span> |
| 17 | + |
| 18 | +echo $stdInstance <span class="token cBase">-</span> <span class="token cBase">></span> foo<span class="token cBase">.</span><span class="token cMod">PHP_EOL</span><span class="token cBase">;</span> <span class="token cComment">//"bar"</span> |
| 19 | +echo $stdInstance <span class="token cBase">-</span> <span class="token cBase">></span> number<span class="token cBase">.</span><span class="token cMod">PHP_EOL</span><span class="token cBase">;</span> <span class="token cComment">//42</span> |
| 20 | + |
| 21 | +<span class="token cComment">//Example with associative array</span> |
| 22 | +$array <span class="token cBase">=</span> <span class="token cMod">json_decode</span><span class="token cBase">(</span>$json<span class="token cBase">,</span> <span class="token cBool">true</span><span class="token cBase">)</span><span class="token cBase">;</span> |
| 23 | + |
| 24 | +echo $array<span class="token cBase">[</span><span class="token cString">'foo'</span><span class="token cBase">]</span><span class="token cBase">.</span><span class="token cMod">PHP_EOL</span><span class="token cBase">;</span> <span class="token cComment">//"bar"</span> |
| 25 | +echo $array<span class="token cBase">[</span><span class="token cString">'number'</span><span class="token cBase">]</span><span class="token cBase">.</span><span class="token cMod">PHP_EOL</span><span class="token cBase">;</span> <span class="token cComment">//42</span></code></pre></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://stackoverflow.com/questions/931407/what-is-stdclass-in-php" rel="noreferrer" target="_blank" title="What is stdClass in PHP? Interview Questions Source To Answer">stackoverflow.com</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 10. In PHP, objects are they passed by value or by reference?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p>In PHP, objects passed by <strong>value</strong>.</p></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://www.guru99.com/php-interview-questions-answers.html" rel="noreferrer" target="_blank" title="In PHP, objects are they passed by value or by reference? Interview Questions Source To Answer">guru99.com</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 11. What is PDO in PHP?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p><strong>PDO</strong> stands for PHP Data Object.</p><p>It is a set of PHP extensions that provide a core PDO class and database, specific drivers. It provides a vendor-neutral, lightweight, data-access abstraction layer. Thus, no matter what database we use, the function to issue queries and fetch data will be same. It focuses on data access abstraction rather than database abstraction.</p></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://github.com/Bootsity/cracking-php-interviews-book" rel="noreferrer" target="_blank" title="What is PDO in PHP? Interview Questions Source To Answer">github.com/Bootsity</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 12. Is there a difference between isset and !empty?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p><code>empty</code> is more or less shorthand for<code>!isset($foo) || !$foo</code>, and <code>!empty</code> is analogous to <code>isset($foo) && $foo</code>. <code>empty</code> is the same as <code>!$foo</code>, but doesn't throw warnings if the variable doesn't exist. That's the main point of this function: do a boolean comparison without worrying about the variable being set.</p></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty" rel="noreferrer" target="_blank" title="Is there a difference between isset and !empty? Interview Questions Source To Answer">stackoverflow.com</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 13. Differentiate between echo and print()</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p><code>echo</code> and <code>print</code> are more or less the same. They are both used to output data to the screen.</p><p>The differences are: </p><ul><li>echo has no return value while print has a return value of 1 so it can be used in expressions. </li><li>echo can take multiple parameters (although such usage is rare) while print can take one argument. </li><li>echo is faster than print.</li></ul></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://github.com/Bootsity/cracking-php-interviews-book" rel="noreferrer" target="_blank" title="Differentiate between echo and print() Interview Questions Source To Answer">github.com/Bootsity</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 14. What is the differences between $a != $b and $a !== $b?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p><code>!=</code> means <em>inequality</em> (TRUE if $a is not equal to $b) and <code>!==</code> means <em>non-identity</em> (TRUE if $a is not identical to $b).</p></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://www.guru99.com/php-interview-questions-answers.html" rel="noreferrer" target="_blank" title="What is the differences between $a != $b and $a !== $b? Interview Questions Source To Answer">guru99.com</a></span></span> </div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 15. What does the 'var' keyword mean in PHP?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p>It's for declaring class member variables in PHP4, and is no longer needed. It will work in PHP5, but will raise an <strong>E_STRICT</strong> warning in PHP from version 5.0.0 up to version 5.1.2, as of when it was deprecated. Since PHP 5.3, var has been un-deprecated and is a synonym for 'public'.</p><p>Consider:</p><pre><code><span class="token cVar">class</span> <span class="token class-name">foo</span> <span class="token cBase">{</span> |
| 26 | + <span class="token cVar">var</span> <span class="token cVar">$x</span> <span class="token cBase">=</span> <span class="token cString">'y'</span><span class="token cBase">;</span> <span class="token cComment">// or you can use public like...</span> |
| 27 | + <span class="token cVar">public</span> <span class="token cVar">$x</span> <span class="token cBase">=</span> <span class="token cString">'y'</span><span class="token cBase">;</span> <span class="token cComment">//this is also a class member variables.</span> |
| 28 | + <span class="token cVar">function</span> <span class="token cMod">bar</span><span class="token cBase">(</span><span class="token cBase">)</span> <span class="token cBase">{</span> |
| 29 | + <span class="token cBase">}</span> |
| 30 | +<span class="token cBase">}</span></code></pre></div></div><div class="row my-2"><div><span><i>Source:</i> <span><a href="https://stackoverflow.com/questions/1206105/what-does-php-keyword-var-do" rel="noreferrer" target="_blank" title="What does the 'var' keyword mean in PHP? Interview Questions Source To Answer">stackoverflow.com</a></span></span> </div></div></div></div></div> <br><br></div> <div data-v-5e9078c0="" class="end"></div> <br data-v-5e9078c0=""> |
| 31 | + Thanks 🙌 for reading and good luck on your next tech interview! |
| 32 | + <br data-v-5e9078c0=""> |
| 33 | + Explore 3800+ dev interview question here 👉 |
| 34 | + <a data-v-5e9078c0="" href="https://devinterview.io/">Devinterview.io</a></div> |
0 commit comments