PHP (NEP & CBCS) Questions with Answers
2 Marks (Important)
1. What is PHP?
PHP Hypertext Preprocessor is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. It allows web develop to create dynamic content that interacts with databases.
2. What is PHP? Explain Basic PHP syntax?
PHP (Hypertext Preprocessor) is a popular, server-side scripting language used for creating dynamic web applications.
Syntax:
<?php
//statements;
?>
3. Mention the advantages of PHP?
- It is open source.
- Widely used in all over the world
- Free to download
- It is executed on the server
- To execute PHP code no need compiler.
4. Explain Basic PHP syntax? And create a simple PHP Script.
PHP Syntax:
You can run php code on any web browser. PHP script is executed on the server, and the plain HTML result is sent back to the browser.
Basic Syntax of PHP
- PHP code is start with
- All PHP statements end with a semicolon (;)
- PHP code save with .php extension.
- PHP contain some HTML tag and PHP code.
- You can place PHP code anywhere in your document
Simple PHP Script:
<!DOCTYPE html>
<html>
<body>
<h1> My first PHP Page </h1>
<?php
echo "Hello World!";
?>
</body>
</html>
5. What is variable? write the syntax.
A variable in PHP is a name or identifier that holds a value, which can be a piece of data such as a string, number, array, object, etc. Variables in PHP are used to store data that can be manipulated and accessed throughout the script.
A variable in PHP is a named memory location that holds data belonging to one of the data types. In PHP, variables are declared with a $
sign followed by the variable name.
Syntax:
$variable_name = value;
Example:
<?php
$greeting = "Hello, World!"; // A string variable
$number = 42; // An integer variable
$price = 19.99; // A floating-point variable
$isActive = true; // A boolean variable
?>
6. How to store data in variables?
In PHP, you can store data in variables by assigning a value to a variable name using the assignment operator (=
). The value can be of various data types, such as a string, integer, float, boolean, array, or even an object. Once a value is assigned to a variable, you can use that variable throughout your PHP script to reference the stored data.
Steps to Store Data in a Variable:
- Declare a Variable: Start by declaring a variable with a name, using the
$
symbol. - Assign a Value: Use the assignment operator
=
to assign a value to the variable. - Use the Variable: The variable now holds the value and can be used anywhere in your script.
7. What is variables? Explain the rules for PHP Variables.
Variable is a named storage location in the computer’s memory that stores a value which can be changed during the program’s execution.
Rules for PHP variables:
- A variable starts with the $ sign, followed by the name of the variable
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive ($age and $AGE are two different variables)
8. What is Operator?
In PHP, Operators are symbols used to perform operations on variables, values, and expressions. These operations can include (Arithmetic , logical, Assignment, Comparison, Increment/Decrement, String, Array). PHP supports a wide range of operators.
9. List the types of operator.
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators
10. What is Data Type? List types of Data Types.
Data Types define the type of data a variable can store. PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types.
Categorized into 3 types:
- Scalar Types (predefined)
- Compound Types (user-defined)
- Special Types
Common Data Types:
String
Integer
Float (floating point numbers – also called double)
Boolean
Array
Object
NULL
Resource
11. What is Constant? Explain with example.
A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name).
Syntax:
define(name, value);
Example:
<?php
const SITE_URL = "https://www.ourcreativeinfo.in";
const DB_HOST = "localhost";
const DB_USER = "root"; // Using the constants
echo SITE_URL; // Outputs: https://www.ourcreativeinfo.in
echo DB_HOST; // Outputs: localhost
echo DB_USER; // Outputs: root
?>
12. What is resource?
13. What is Null value?
14. What is object?
15. What is Array?
16. Explain strings in PHP?
17. What is Control Statement in PHP.
In PHP, control statements are used to control the flow of execution of a program based on certain conditions. These statements include if…else, switch, and looping structures such as for, while, and do…while loops.
18. List out the control statement in PHP
Conditional Statements:
if
else
elseif
switch
Looping Statements:
for
while
do...while
Jump Statements:
break
continue
goto
19. What is looping in PHP
Loops are used to execute the same block of code again and again, as long as a certain condition is true. or In PHP, looping refers to the process of executing a block of code repeatedly based on certain conditions.
20. List out looping statement in PHP.
- for loop
- while loop
- do…while loop
- foreach loop
21. Define String in PHP.
In PHP, a string is a data type used to represent a sequence of characters. Strings are one of the fundamental data types in PHP and are used to store and manipulate text.
In PHP, a string is a sequence of characters, which can be letters, numbers, symbols, or spaces, enclosed within either single quotes (‘ ‘) or double quotes (” “).
22. What is an Array? How to create array in PHP?
An array is a special variable that can hold many values under a single name, and you can access the values by referring to an index number or name.
In PHP, the array() function is used to create an array: array();
Example:
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
23. List Different Types of PHP Array.
Indexed Array
Associative Array
Multidimensional Array
24. What is Indexed Array in PHP? Give Example.
The index can be assigned automatically (index always starts at 0), like this.
$cars = array("Volvo", "BMW", "Toyota");
Or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
Example:
<html>
<body>
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
</body>
</html>
25. What is Associative Array? Give simple example.
An associative array in PHP is a type of array where each element is associated with a unique key, which is a string. or An associative array in PHP is a collection of key-value pairs, where each key is associated with a specific value.
Example:
<html>
<body>
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
</body>
</html>
26. Which function is used to get length of an array? Give simple example.
In PHP, the function used to get the length of an array (i.e., the number of elements in the array) is count()
. This function returns the total number of elements present in the array.
Syntax:
count($array);
Example:
<html>
<body>
<?php
// Define an array
$fruits = array("Apple", "Banana", "Cherry", "Date");
// Get the length of the array
$length = count($fruits);
// Output the length of the array
echo "The number of elements in the array is: " . $length;
?>
</body>
</html>
27. What are PHP string functions? List any 5
PHP provides a wide range of string functions for manipulating and working with text. These functions allow you to perform various operations such as searching, replacing, and modifying strings. or PHP offers a wide range of built-in functions specifically designed for manipulating strings.
strlen()
strpos()
substr()
strtolower()
strtoupper()
Example:
<?php
const SITE_URL = "https://www.ourcreativeinfo.in";
const DB_HOST = "localhost";
const DB_USER = "root"; // Using the constants
echo SITE_URL; // Outputs: https://www.ourcreativeinfo.in
echo DB_HOST; // Outputs: localhost
echo DB_USER; // Outputs: root
?>
28. What is function in PHP?
29. What is MySQL?
30. Difference Between MySQLi and PDO
My SQLi | PDO |
Limited to MySQL databases | Supports multiple databases |
Supports both named & positional placeholders for statements | Offers full support for prepared statements |
Supports both procedural and object-oriented programming | Primarily OOP |
Supports bind parameters using “?” placeholders or named types | Provides consistent approach regardless of database |
Primarily for MySQL, restricts portability | More portable across different database systems |
31. What is laravel in PHP?
32. Mention the features of Laravel in PHP?
- Modularity
- Testability
- Routing
- Configuration Management
- Query Builder
- ORM (Object Relational Mapper)
- Schema Builder
- Authentication
- Redis
- Queues
- Event