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.

or

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:

  1. Declare a Variable: Start by declaring a variable with a name, using the $ symbol.
  2. Assign a Value: Use the assignment operator = to assign a value to the variable.
  3. 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:

  1. Scalar Types (predefined)
  2. Compound Types (user-defined)
  3. 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?
A resource in PHP is a special variable that holds a reference to an external resource, such as a file, database connection, or stream. 
or
In PHP, Resource  is a special data type that refers to any external resource. A resource variable acts as a reference to external source of data such as stream, file, database etc.
13. What is Null value?
The is_null() function checks whether a variable is NULL or not. 
In PHP, a variable with no value is said to be of null data type. Such a variable has a value defined as NULL. A variable can be explicitly assigned NULL or its value been set to null by using unset() function.
14. What is object?
An Object is an individual instance of the data structure defined by a class. We define a class once and then make many objects that belong to it. Objects are also known as instances.
15. What is Array?
An array is a special variable that can hold many values under a single name, and the values by referring to an index number or name.
16. Explain strings in PHP?
A string in PHP is a sequence of characters, where each character is essentially a byte. Strings are one of the most commonly used data types in PHP and are used to store and manipulate text. They can contain letters, numbers, special characters, and even escape sequences
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:

  1. if

  2. else

  3. elseif

  4. switch

Looping Statements:

  1. for

  2. while

  3. do...while

Jump Statements:

  1. break

  2. continue

  3. 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.
  1. for loop
  2. while loop
  3. do…while loop
  4. 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.

or

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.
  1. Indexed Array

  2. Associative Array

  3. 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.

  1. strlen()

  2. strpos()

  3. substr()

  4. strtolower()

  5. 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?
PHP function is a piece of code that can be reused many times. It can take input as argument list and return value. There are thousands of built-in functions in PHP.
29. What is MySQL?
MySQL is an open-source relational database management system (RDBMS). It is the most popular database system used with PHP. MySQL is developed, distributed, and supported by Oracle Corporation.
30. Difference Between MySQLi and PDO
My SQLiPDO
Limited to MySQL databasesSupports multiple databases
Supports both named & positional placeholders for statementsOffers full support for prepared statements
Supports both procedural and object-oriented programmingPrimarily 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?
Laravel is an open-source PHP framework, which is robust and easy to understand. It follows a model-view-controller design pattern. Laravel reuses the existing components of different frameworks which helps in creating a web application.
32. Mention the features of Laravel in PHP?
  • Modularity
  • Testability
  • Routing
  • Configuration Management
  • Query Builder
  • ORM (Object Relational Mapper)
  • Schema Builder
  • E-mail
  • Authentication
  • Redis
  • Queues
  • Event

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    error: Content is protected !!