PHP Interview Questions & Answers for Freshers

Are you a looking for PHP Interview Questions for Freshers? Find PHP Interview Questions and Answers for beginners here. It is important to learn basic PHP concepts while doing PHP Job Preparation. These are the commonly asked Interview questions for beginners.

What is PHP?

PHP stands for: Hypertext Preprocessor. PHP is the most popular open-source programming language for web development. It is a server-side language that collects form data, sends or retrieves cookies, interact with the database, and much more. With the help of PHP, we can create static as well as dynamic websites.

Who is the father of PHP?

Rasmus Lerdorf is the father of the PHP who created PHP language in 1994.

What is the correct & common way to start and finish a PHP block of code?

The two most common ways to start and finish a PHP script are:

  1. Using standard tags: <?php PHP Code here ?>
  2. Using short tags: <? PHP Code here ?>

Example 1: Using standard tags:

This is the recommended way.

<?php
echo "I love PHP";
?>

Example 2: Using short tags:

This method is not supported by all server (depending on the PHP configuration), so always use first method.

<?
echo "I love PHP";
?>

What is Open Source Software?

Open-source software is one type of software on which the original source code is made freely available to the public. This software can also be redistributed and modified by the public.

Examples:
PHP, MySQL, and WordPress are a few examples of popular Open Source Software.

Is PHP a case sensitive language?

In PHP, variable names are case-sensitive but function names are not case sensitive.

Explanation:

  • If you defined variable in lowercase, then you need to use it in lowercase everywhere in the program. If we define a variable $name = "James"; then we must need to use $name. $NAME will not work
  • If you defined function name in lowercase, but calling them in uppercase it will work. For example, If we define function sum() {} then calling SUM() will also work.
Note: Function names are case-insensitive, though it is good to call functions in the same case as they appear in their declaration for consistency.

What is the default extension of the PHP file?

.php is the default extension of PHP file.

What is PHP Function?

PHP Function is a set of statements that perform a specific task. A function usually takes input arguments, perform a specific action on them and returns the result.


Example of function in PHP:

<?php
function msg() { //function function name
    echo "Hello world!";
}

msg(); //call to function
?>

What are the form method Attribute available for submitting?

The method attribute specifies how to send form-data. "GET" and "POST" are two methods to send form data to another page.

<form action="welcome.php" method="post">
// form elements will be placed here
</form>
<form action="welcome.php" method="get">
// form elements will be placed here
</form>

What are the different PHP functions to sort an array?

  • asort()
  • arsort()
  • ksort()
  • krsort()
  • uksort()
  • sort()
  • nasort()
  • rsort()

For more details: http://tutorialsclass.com/learn/php/php-array-functions

What is include() function in PHP?

The include() takes all the content in a specified file and includes it in the current file.

If an error occurs, the include() function generates a warning, but the script will continue execution.

What are the different types of error in PHP?

There are four basic types of run-time errors in PHP:

  1. Notices
  2. Warnings
  3. Fatal Error
  4. Parse Errors:

For more detail, please check PHP Errors

PHP’s numerically indexed array begins with which position?

PHP Indexed array begins with position 0.
For example: if $array=array(15,80,67,33); Then these elements will have following positions:
$array[0]=10 (This is beginning position 0 not 1)
$array[1]=89
$array[2]=34
$array[3]=67

Which PHP function inserts an element to the end of an array?

PHP array_push() function is used to insert one or more elements to the end of an array.

Why we use PHP String functions trim(), ltrim() and rtrim()?

PHP trim related functions are used to remove white spaces or other given characters from the beginning and end of the strings.

  • trim() removes space & predefined characters from both sides of given string.
  • ltrim() removes space & predefined characters from the left side of the sting.
  • rtrim() removes space & predefined characters from the right side of the sting.

These functions are helpful when we want to clean strings from unwanted spaces or characters.

Syntax: trim(string,charlist)

Example:

$sample_string = '  Welcome to TutorialsClass.com ';
$clean_string = trim($sample_string);

Which PHP function can be used check if variable is an array?

PHP is_array() the function is used to check if a variable is an array or not. This is_array() function will return true if a variable is an array or false if it is not.

What PHP Function is used to find position of a string?

PHP strpos() function is used to find the first occurrence of substring in the given string.

<?php
echo strpos("The color of apple is red.","apple");
?>
Tutorials Class - Output Window

13

The word 'apple' is found at the position '13'. Remember that the position of string/character start at 0, and not 1. This function is very useful to find certain patterns or words in a string.

How many types of arrays are available in PHP?

There are three types of array available in PHP.

  1. Numeric (Indexed) array
  2. Associative array
  3. Multidimensional array

Learn more about array with examples in PHP Array Tutorial

What is the use of ucwords() PHP String function?

PHP ucwords() function is used to capitalized the first letter of each word in String.

<?php
echo ucwords("welcome to the php tutorials");
?>

Run : http://ideone.com/83vvyF

What is the use of array_chunk() function in PHP?

The array_chunk() function is used to split an array into parts or chunks of new arrays. Example: array_chunk(array,size);

The first parameter specifies an array and the second parameter defines the size of each chunk.

What is the use of str_replace() PHP String function?

The PHP str_replace() function is used to search & replace some words in a strings.

Example

<?php
echo str_replace("Hello", "Hi", "Hello PHP!");
?>

In this example, "Hello" word will be replaced by "Hi" in the given string.

Run : http://ideone.com/AFs0DW