PHP Interview Questions For Beginners
Tags: phppractices, php, phpinterview, phpinterviewquestion
Published: 2022-08-26 17:52:06

---
title: PHP Interview Questions For Beginners
published: true
date: 2022-07-25 04:42:18 UTC
tags: PHPPractices,php,phpinterview,phpinterviewquestion
canonical_url: https://larachamp.com/php-interview-questions-for-beginners/
---

PHP is a backend language that we use to develop web applications and mainly for dynamic work. As I also develop websites in PHP but the thing is that when I was learning PHP I wasn’t good at theory and explaining things in PHP. But at the same time I was really good at doing practical things like logic building which is the main part of building any software. Now, when I am preparing for an interview I thought that I should share the questions that I have read for the interview. Let’s learn the fundamental of functions and PHP topics that we know or don’t know.

## What is PHP stands for?

At first, when PHP language was created it was known as Personal Home Page, but it now stands for **Hypertext Preprocessor**.

## What is a Constant in PHP?

A constant is a simple name that starts with an underscore or latter that also stores values as variables do. We can’t change the value of a constant in the entire script. We can globally access a constant in the whole script easily. And the main thing is that to define a constant we don’t use the ‘$’ sign. We define constant with a function called `define()`.

```
define("karela", 100); //set the value

echo karela; //to get the value
```

## What is PDO in PHP?

PDO stands for **PHP Data Object**. [Read PDO](https://www.javatpoint.com/php-pdo)

## How to connect with Database in PHP?

When we do want to connect PHP with MySQL database then we should have three data variables. First the server name,username,and the password for connect to that particular server. It helps to connect with server database. You also can see in the code below.

```
<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
```

## What is Session in PHP?

I see session as a person who knows our biodata on server. We can store any kind of information in session. Session ends when you ends using application.

`$_SESSION['name'] = "Permjeet"; //this is how we can define a session variable`

<mark style="background-color:rgba(0, 0, 0, 0)"><code>&lt;?php<br>// remove all session variables<br>session_unset();<br><br>// destroy the session<br>session_destroy();<br>?&gt;</code></mark>

We can use session in our entire script but for that you have to start session in the beginning of your script using `session_start()` function.

## Name and Explain 5 string functions in PHP?

1. **echo()** we use echo() function to print output of a variable we can’t print an array with echo.
2. **crypt()** we use crypt() function to convert a string into hash.
3. **explode()** we use explode() function to break sting into arrays. //explode(_separator,string,limit_)
4. **str\_replace()** we use str\_replace() function to change the string with new string or replace with new. // str\_replace(_find,replace,string,count_)
5. **strpos()** we use strpost() function to find a passed string position e.g strpost(‘weather’,’a’) ans:will be 2 it starts from 0 index. //stripos(_string,find,start_)

## What is break and Continue statement in PHP?

The Continue statement goes back to the loop when loop starts then it returns the loop again on the top. But on the other hand break; statement used for when we want to get out of the loop.

## Explain move\_uploaded\_file function?

move\_uploaded\_file function used to move a uploaded file to a location or destination. If this destination already exists then it does overwrite the file.

## Explain unlink() function?

unlink() function is used to delete the files from destination. We do pass the filepath/filename in this function to delete any file from directories.

## Explain basename() function?

basename() function helps to get the name of the file from the path.

## Difference between require and include function in PHP?

One thing that I would like to clear that is both of the function used to get another PHP file in a PHP file. But there’s little difference between these to functions.

1. require() function means if passed filename doesn’t exists on the destination it will stop the execution and will throw a fatal error.
2. include() function also used to include the file but it doesn’t stop the execution it shows a warning and it doesn’t affect the execution of the file.

## What does mysqli\_num\_rows() function do?

We use the mysqli\_num\_rows() function to count the rows we are getting in the result. It also helps to validate the condition if the result exists or not.

## What is the difference between explode() and split()?

Both of the functions look and feel the same but they are not because maybe you can get the same result but the split() function takes much more execution time than explode() function. the split() function uses a pattern to split the data and on the other hand explode() function uses a string separator.

## Name and Explain 5 Array functions in PHP?

Arrays are used to store multiple values in just a single variable e.g [‘first’,’second’,’third’];

1. **array\_chunk()** function splits an Array into chunks of the array. // array\_chunk(array, size, preserve\_key)
2. **array\_combine()** function is used to combine to an array but the condition is one array consists of “keys” and in second “values”.
3. **array\_diff()** function returns the difference between two or more arrays. //array\_diff(array1,array2,array3….)
4. **merge\_array()** function merges two or more arrays in one array. // merge\_array(array1,array2,…)
5. **array\_search()** function search into an array and returns the key position. // array\_search(‘value’,$array); 

## PEAR In PHP?

PEAR is a framework and repository for reusable PHP components. It contains PHP libraries and code snippets.

## How to execute SQL query and fetch the result?

For selecting the data from the database we have to use a select query. Then we have to pass a function called “mysql\_query(‘select statement’). After this, we have to fetch the data with mysql\_fetch\_array() _we have multiple functions to fetch data from the database_.

```
$mquery = mysql_query("SELECT * FROM users where `userid` = 1");
$result = mysql_fetch_arrray($mquery);
echo $result['name'];
```

## How to download file in PHP?

When we do upload file in php in the same way sometimes we need to download the file from storage. For download the file from storage in php we can use readfile() function that helps to download the file.

## What was the old name of PHP?

Personal Home Page.

## How many database PDO can work with?

If we will work with in PHP using PDO then it can connect with 10+ databases.

## Conclusion

Overall we have covered some basic questions related to PHP. But I haven’t covered the any question of OOP’s here which you can see in the same category. I’ll be uploading that one also. I hope you will learn from this article. Thanks

Would you like to read about [Laravel Tips.](https://larachamp.com/category/laravel/laravel-tips/)

The post [PHP Interview Questions For Beginners](https://larachamp.com/php-interview-questions-for-beginners/) first appeared on [larachamp.com](https://larachamp.com).

The post [PHP Interview Questions For Beginners](https://larachamp.com/php-interview-questions-for-beginners/) appeared first on [larachamp.com](https://larachamp.com).