0% found this document useful (0 votes)
5 views

Php Important

Php

Uploaded by

rtaru99
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Php Important

Php

Uploaded by

rtaru99
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PHP IMPORTANT

1. What is an indexed array in PHP?


An indexed array in PHP is a type of array where the elements are assigned numeric
indices automatically. These indices start from 0 and increment by 1 for each
subsequent element. Indexed arrays are commonly used when you don't need to
specify custom keys. You can access the elements using their index numbers.

2. What is the difference between echo and print in PHP?

• echo: It is a language construct in PHP used to output one or more strings. It is


slightly faster than print because it does not return any value and can handle
multiple arguments (although it is mostly used with one argument). Example:

• print: It is also a language construct used to output a single string. The key
difference is that print always returns 1, so it can be used in expressions.
Example:

3. Explain the krsort() function in PHP.

The krsort() function in PHP is used to sort an associative array in reverse order
according to its keys. It maintains the relationship between keys and their values while
sorting in descending order based on the keys.
4. What is the use of a sticky form in PHP?
A sticky form is a form that retains the user’s input even after the form has been
submitted and the page is reloaded. This is especially useful in cases where form
validation fails, and you want to preserve the values already entered by the user. The
technique is typically implemented by repopulating the form fields with the values
submitted via the $_POST or $_GET superglobals.

The setcookie() function in PHP is used to send a cookie from the server to the client’s
browser. Cookies are small pieces of data that are stored on the client’s machine and
can be retrieved later to maintain state across different pages or sessions.

The basic syntax of setcookie():


setcookie(name, value, expire, path, domain, secure, httponly);
• name: The name of the cookie.

• value: The value of the cookie.

• expire: The expiration time of the cookie (in seconds from the current time).

• path: The path for which the cookie is available.

• domain: The domain to which the cookie is available.

1
• secure: If true, the cookie will only be sent over secure connections (HTTPS).

• httponly: If true, the cookie will be accessible only through the HTTP protocol
(not JavaScript).

6. What is $_SESSION in PHP?

$_SESSION is a PHP superglobal array used to store session variables. Session


variables are stored on the server and are used to maintain user-specific data across
multiple pages. This allows you to store and retrieve user information, such as login
status or preferences, between different pages of the website without requiring the
user to re-enter the information.

7. Explain the var_dump() function in PHP.

The var_dump() function is used to display structured information (type and value)
about a variable. It is often used for debugging purposes to inspect the contents of
arrays, objects, and other complex data types. It provides more detailed information
than echo or print.
8. What is a ternary operator in PHP?
The ternary operator is a shorthand for if-else statements. It allows you to evaluate a
condition and return one of two values based on whether the condition is true or false.
The syntax is as follows:
condition ? expr1 : expr2;
• If the condition evaluates to true, expr1 is returned.
• If the condition evaluates to false, expr2 is returned.

9. Explain the array_slice() function in PHP.

The array_slice() function is used to extract a portion of an array. It returns a new array
containing elements starting from a specified index and optionally with a specified
length.

Syntax:
array_slice(array, start, length, preserve_keys);
• array: The input array.

• start: The starting index from which the extraction will begin.

• length: (Optional) The number of elements to extract.

• preserve_keys: (Optional) Whether to preserve the keys of the extracted


elements.

2
10. Explain the mysqli_connect() function in PHP.

The mysqli_connect() function is used to establish a connection to a MySQL database


in PHP. It requires the host, username, password, and database name as parameters.

Syntax:
mysqli_connect(host, username, password, database);

11. List the types of arrays in PHP.


In PHP, there are three main types of arrays:

• Indexed Arrays: These arrays use numeric indices (starting from 0 by default)
to access elements. The array can be either automatically or manually indexed.

• Associative Arrays: These arrays use named keys (strings) instead of numeric
indices to store and retrieve values.
• Multidimensional Arrays: These are arrays that contain one or more arrays
within them. You can think of them as arrays of arrays.
12. What are the different arithmetic operators in PHP?
Arithmetic operators are used to perform basic mathematical operations. In PHP, the
following arithmetic operators are available:

• Addition (+): Adds two values.

• Subtraction (-): Subtracts one value from another.

• Multiplication (*): Multiplies two values.

• Division (/): Divides one value by another.

• Modulus (%): Returns the remainder of a division.

• Exponentiation (**): Raises a number to the power of another.


13. What is an abstract class in PHP?
An abstract class in PHP is a class that cannot be instantiated on its own and must be
extended by other classes. It can contain abstract methods (methods without
implementation) as well as concrete methods (with implementation). Any subclass
that extends the abstract class must implement the abstract methods.
14. Define a sticky form in PHP.
A sticky form is a form where the previously entered data remains populated in the
form fields even after the form is submitted, particularly in case of validation errors.
This can be achieved by storing the form values in the $_POST superglobal and
redisplaying them in the form fields.

3
15. What is validation in PHP?
Validation in PHP refers to the process of ensuring that user input meets specific
criteria before processing it. This is commonly done for form submissions to ensure
the data is valid, such as checking if required fields are filled, if the input matches the
correct format (like email or phone number), and if the data doesn't contain harmful
content (like SQL injection or XSS).
16. What are the databases supported by PHP?
PHP supports several databases, both relational and non-relational. The most
commonly used databases with PHP are:

• MySQL: The most popular relational database management system, typically


used with PHP for web applications.

• PostgreSQL: Another relational database system known for its features and
standards compliance.
• SQLite: A serverless, self-contained database engine commonly used for
smaller applications.

• MariaDB: A fork of MySQL, often used as a drop-in replacement.

• Oracle: A powerful relational database system, though it requires the


installation of Oracle client libraries for PHP to interact with it.

• MongoDB: A NoSQL database, often used for handling large-scale,


unstructured data.
17. What is the use of a session in PHP?
A session in PHP is a way to store user data across multiple pages on a website. Unlike
cookies, session data is stored on the server and is accessible through a session ID that
is sent to the client via a cookie. Sessions are commonly used for user authentication,
shopping carts, and maintaining user-specific data between page loads.

To start a session, use the session_start() function, and you can store data in
the $_SESSION superglobal array.

18. Which attribute is used for multiple selections in the <select> tag in HTML?

The multiple attribute in the <select> tag is used to allow multiple selections of
options. When this attribute is added, users can select more than one option in a
dropdown list.
19. What is the purpose of the break statement in PHP?
The break statement in PHP is used to exit from a loop or switch statement. When
executed, it immediately terminates the loop or switch and transfers the control to the
next statement after the loop or switch.

4
• In Loops: It can be used to exit from for, foreach, while, and do-while loops.

• In Switch Statements: It can be used to exit a switch statement after a match is


found.
20. Explain the difference between static and dynamic websites.
• Static Websites: These websites consist of fixed content that doesn’t change
unless manually updated by the website developer. Every user sees the same
content when they visit the page. Static websites are faster, simpler to create,
and cheaper to host. They are typically written in HTML, CSS, and JavaScript.

Example: A basic informational website or portfolio.

• Dynamic Websites: These websites generate content in real-time, often based


on user input or external data sources like databases. The content of a dynamic
website can change depending on factors such as the user’s location,
preferences, or actions. Dynamic websites are often powered by server-side
scripting languages like PHP, JavaScript (Node.js), Python, and are typically
more complex to develop.
21. How to declare a variable in PHP?
In PHP, a variable is declared by prefixing the variable name with a dollar sign ($).
The variable name must begin with a letter or an underscore, followed by any
combination of letters, numbers, or underscores.

22. What is the use of count() in PHP?

The count() function in PHP is used to count the number of elements in an array or
properties in an object. It can also be used to count the number of items in a
multidimensional array if the second argument COUNT_RECURSIVE is specified.
23. What is a session in PHP?
A session in PHP is a way to store user information on the server for later use across
multiple pages. Sessions help in retaining user data like login information and
preferences during the user’s visit to the website. Each session is assigned a unique
session ID, which is typically stored as a cookie in the user's browser.

To start a session, use the session_start() function, and session variables can be stored
in the $_SESSION superglobal array.
24. What is a cookie in PHP?
A cookie is a small piece of data stored in the user's browser that is sent to the server
with every request. Cookies are commonly used to store user preferences, session
identifiers, and other data that persists across multiple pages or visits. Cookies are set
using the setcookie() function.

5
25. Explain the PHP explode() function.

The explode() function in PHP is used to split a string into an array based on a
specified delimiter. The function returns an array of strings, and the delimiter is used
to separate the parts.
26. How to concatenate two strings in PHP?
In PHP, strings can be concatenated using the dot (.) operator. You can concatenate
multiple strings together.
27. What is PHP?
PHP (Hypertext Preprocessor) is a widely-used, open-source server-side scripting
language designed for web development. It can be embedded into HTML and is
primarily used for creating dynamic web pages. PHP is capable of handling tasks like
form processing, session management, and interacting with databases.

• PHP is free and can run on various platforms (Windows, Linux, macOS).

• PHP code is processed on the server, and the result is sent to the browser as
plain HTML.

28. Explain $_SERVER in PHP.

$_SERVER is a superglobal array in PHP that contains information about the server
environment, such as headers, paths, and script locations. It also provides data related
to the current request, like the request method, client information, and more.
29. Describe the echo statement in PHP.
echo is a language construct in PHP used to output data to the browser. It can display
strings, variables, and HTML content. echo can accept multiple parameters and does
not require parentheses.

30. What is the use of the isset() function in PHP?

The isset() function is used to check if a variable is set and is not NULL. It
returns true if the variable exists and has a value other than NULL; otherwise, it
returns false.
31. Which are the methods to submit a form in PHP?
There are two common methods to submit a form in PHP: GET and POST.

• GET Method: The data is appended to the URL in the form of a query string.
It is visible in the URL, making it less secure. It is used for retrieving data, such
as search queries.

• POST Method: The data is sent through the HTTP request and is not visible in
the URL. It is more secure than GET and is commonly used for submitting form
data like login credentials, passwords, etc.

6
32. Explain the split() function in PHP.

The split() function in PHP was used to split a string into an array based on a regular
expression pattern. However, as of PHP 5.3.0, it has been deprecated and replaced
by preg_split(). It's recommended to use preg_split() instead of split().
33. What does PEAR stand for?
PEAR stands for PHP Extension and Application Repository. It is a framework and
distribution system for reusable PHP components and libraries. PEAR is a collection
of PHP classes, modules, and functions that are bundled into packages for easy
installation and use.

34. What is the use of print_r() in PHP?

The print_r() function in PHP is used to display human-readable information about a


variable, typically an array or object. It provides a more structured output
than echo and is particularly useful for debugging purposes.

35. What does the unset() function mean in PHP?

The unset() function in PHP is used to destroy a variable, making it no longer


available for use. It can also be used to remove elements from an array.

You might also like