PHP Interview Questions and Answers PHP Or Hypertext Preprocessor Is

PHP Interview Questions and Answers PHP Or Hypertext Preprocessor Is

PHP Interview Questions and Answers PHP or Hypertext Preprocessor is a general-purpose programming language used for web development. It can perform any task related to server-side programming. A PHP file consists of tags, and it ends with the extension “.php.” PHP code can be embedded into HTML code. It can also be used with a web content management system, web frameworks, and web template systems. PHP is a simple language to work on, even a new user can work on it. At the same time, it offers multiple advanced features for professional developers. We can develop efficient codes using its distinctive features. Being a popular programming language, many organizations look out for skilled and experienced professionals. This article lists some of the most popularly asked PHP interview questions. Let’s take a look – Q1. What are the features of PHP? Ans. PHP has some unique features, which include: Open-source Platform Independent Case sensitive Flexibility Efficiency Third-party application support and security Interpreted Q2. Why do we use PHP? Ans. There are various reasons to use PHP for server-side programming: It is a free language with no licensing fees; the cost of using it is minimal. It supports multiple databases, including MySQL that is also a free source. It is easier to create a website Q3. Is PHP a loosely typed language? Ans. Yes, it is a loosely typed language. It is not required to declare a variable type to declare a variable. In PHP, if you declare a variable, then you don’t have to worry about what type of data would be stored in that variable. If you declare a variable called $String then it is not necessary to assign a string value, you can assign an integer value also. Example: $var=”Hello”: //String $var= 20; //Integer Q4. What are the different PHP Framework? Ans. Following are the popular frameworks: CodeIgniter Yii 2 CakePHP Zend Framework Symfony Phalcon PHPixie FuelPHP Q5. What are the characteristics of PHP variables? Ans. Following are the characteristics: Each variable is denoted by a dollar sign ($). The value of a variable is the value of its most recent assignment. Variables are assigned with “=” operator, where the variable is on the left-hand side and expression is on the right-hand side. Variables can be but do not need to be declared before the assignment. Variables do not have an intrinsic type; it does not know its variable type and can be assigned with any variable type. Variables that are used in the code before they are assigned, have a default value. PHP variables are Perl-like. Q6. What does PEAR mean? Ans. PEAR stands for “PHP Extension and Application Repository”. PEAR provides a structured library of open-source code to the PHP users. Q7. How do you execute a PHP script from the command line? Ans. By using the command-line interface (CLI) and specify the file name that is to be executed. Syntax: php script.php Q8. What is the difference between static and dynamic website? Ans. Static web pages are written in HTML, JavaScript, CSS, etc. In static web pages, when a server sends a request to the web page the server sends the response to the client. After that, the web pages are visible through the web browser. In static web pages, the web pages are still the same until somebody changes it manually. Dynamic pages are written in languages such as AJAX, ASP.NET, CGI, etc. There are different content for different visitors on dynamic pages. They are a bit complicated as compared to static pages, as information changes frequently in dynamic pages. Q9. Explain the main differences between PHP 4 and PHP 5. Ans. The main difference between PHP 4 and PHP 5 are: PHP 4 PHP 5 ● PHP 5 supports the OOPs concept and ● PHP 4 does not support the OOPs Zend engine 2 is used concept and Zend engine 1 is used ● Objects are passed by reference in PHP ● Objects are passed by value in PHP 4 5 ● It does not allow static methods and ● It allows static methods and properties properties in a class in a class Q10. What is the difference between echo and print? Ans. Both echo and print methods are used to print the output in the browser, but the difference between them is echo does not return any value after printing the output, and it is also faster than the print method. The print method is slower because it returns the Boolean value after printing the output. Syntax: echo “PHP Developer”; $n = print “Java Developer”; Q11. How can we execute PHP script using the command line? Ans. PHP command is used in the command line to execute a PHP script. If you are using the file name, i.e. test.php the php test.phpcommand is used to run the script from the command line. Q12. Mention the popular Content Management Systems (CMS) in PHP? Ans. Following are some popular Content Management Systems (CMS): WordPress Shopify Drupal Joomla Wix Magento Q13. How to declare an array? Ans. There are three types of arrays that you can declare i.e. numeric, multidimensional and associative arrays. 1. 1. 1. //Numeric Array 1. 1. 2. $computer = array(“Dell”, “Lenavo”, “HP”); 1. 1. 3. //Associative Array 1. 1. 4. $color = array(“Sithi”=>”Red”, “Amit”=>”Blue”, “Mahek”=>”Green”); 1. 1. 5. //Multidimensional Array 1. 1. 6. $courses = array ( array(“PHP”,50), array(“JQuery”,15), array(“AngularJS”,20) ); Q14. Mention some of the constants in PHP and their purpose? Ans. Following are some constants: _LINE_: The current line number of the file. _FILE_: It represents the full path and file name of the file. If used inside an include, it will return the name of the included file. _FUNCTION_: It returns the function name. _CLASS_: It returns the class name as it was declared. _METHOD_: It represents the method of the class. Q15. Explain the uses of explode() and implode() function? Ans. The explode() function is used to split by a specified string into pieces i.e. it breaks a string into an array. Example: If you have a string $str = “JOHN”: Now, divide string into an array $arr = explode(“,”, $str); “,” will separate the string into an array And put the resulting array into the variable “arr” Use print_r ($arr); to print the array Array( [0] => J [1] =>O [2] => H [3] => N ) which is equal to: $arr = Array (“J”,”O”,”H”,”N”) An implode() function is used to join the elements of an array to string. It returns a string from an array and it is remembered as “array to string” Take an array like this $arr = Array (“J”,”O”,”H”,”N”); Convert it into a string by putting the separator ‘-‘ between each element of the array. $str = implode(“-“,$arr); So your resulting string variable $str will contain: J-O-H-N Syntax: $text = “She is beautiful”; print_r (explode(” “,$text)); $strarr = array(‘Pen’,’Pencil’,’Eraser’); echo implode(” “,$strarr); Q15. Explain the PHP $ and $$ variables? Ans. The “$” variable is a standard variable that stores any value such as string, float, integer, etc. The “$$” variable is used to store the value of $variable inside it. <?php $name=”Joe”; ${$name}=”Merry”; ${${$name}}=”Dolcie”; echo $name. “<br>”; echo ${$name}. “<br>”; echo $Cat. “<br>”; echo ${${$name}}. “<br>”; echo $Dog. “<br>”; ?> Output: Joe Merry Merry Dolcie Dolcie Q16. Explain the difference between GET and POST methods? Ans. There are two ways to send information via browser client to the web server: The GET method The POST method GET method POST method ● The POST method has no restriction to ● The GET method produces a long string any data size to be sent that appears in the server log ● It can be used to send both binary data ● It is restricted to send only 1024 as well as ASCII data characters ● In this method, security depends on the ● It is not used to send binary data such HTTP protocol because the data sent by the as word document or images to the server POST method goes through an HTTP header. ● $_GET is used to access all the sent ● $_POST associative array is provided by information using GET method the PHP to access all the sent information using POST method Q17. Explain type casting and type juggling? Ans. Type casting is defined as the assigning particular type of data type to the variable. Example: $foo = ‘1’; echo gettype($foo); // outputs ‘string’ settype($foo, ‘integer’); echo gettype($foo); // outputs ‘integer’ In type juggling the variable type is changing automatically with the value assigned to the variable, as PHP does not support variable declaration. Example: <?php $foo = “0”; // $foo is string (ASCII 48) $foo += 2; // $foo is now an integer (2) $foo = $foo + 1.3; // $foo is now a float (3.3) ?> Q18. Can you extend a Final defined class? Ans. No, You cannot extend Final method. The final class prevents child class and method overloading. Example of Final method: <?php class ParentClass { public function test() { echo “ParentClass::test() called\n”; } final public function moreTesting() { echo “ParentClass::moreTesting() called\n”; } } class ChildClass extends ParentClass { public function moreTesting() { echo “ChildClass::moreTesting() called\n”; } } // Results in Fatal error: Cannot override final method ParentClass::moreTesting() ?> Q19. How to make a connection with MY SQL server? Ans. If you want to make a connection with MS SQL server, then you have to provide MS SQL hostname, username, and password in mysqli_connect() method. Example: <?php $serverName = “serverName\\sqlexpress”; //serverName\instanceName $connectionInfo = array( “Database”=>”dbName”, “UID”=>”userName”, “PWD”=>”password”); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn ) { echo “Connection established.<br />”; }else{ echo “Connection could not be established.<br />”; die( print_r( sqlsrv_errors(), true)); } ?> Q20.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    16 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us