0% found this document useful (0 votes)
7 views6 pages

PRQ PHP

The document outlines various practical questions and answers related to PHP programming, covering topics such as the differences between echo() and print(), variable variables, the spaceship operator, string operators, control structures, loops, and functions. It includes examples of PHP code for Fibonacci series, prime numbers, and the use of anonymous functions. Additionally, it compares built-in functions with user-defined functions and explains variable functions.

Uploaded by

Sami Md
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

PRQ PHP

The document outlines various practical questions and answers related to PHP programming, covering topics such as the differences between echo() and print(), variable variables, the spaceship operator, string operators, control structures, loops, and functions. It includes examples of PHP code for Fibonacci series, prime numbers, and the use of anonymous functions. Additionally, it compares built-in functions with user-defined functions and explains variable functions.

Uploaded by

Sami Md
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PRACTICAL RELATED QUESTIONS-

1. State the difference between echo() and print().

 echo can take mul ple parameters, while print can take only one argument.

 echo is slightly faster than print because it doesn’t return a value.

 print returns 1, so it can be used in expressions.

Example:

php

CopyEdit

echo "Hello", " World!"; // Valid

print "Hello"; // Valid

2. State the difference between $ and $$ in PHP.

 $var is a normal variable.

 $$var is a variable variable (the name of the variable is stored in another variable).

Example:

php

CopyEdit

$var = "name";

$name = "John";

echo $$var; // Outputs: John

3. State the use of spaceship operator.

 The spaceship operator (<=>) is used for comparing two expressions.

 Returns:

o -1 if le is less than right

o 0 if both are equal

o 1 if le is greater than right

Example:

php

CopyEdit

echo 5 <=> 10; // Outputs: -1

echo 10 <=> 10; // Outputs: 0


echo 15 <=> 10; // Outputs: 1

4. What are string operators available in PHP?


PHP provides two string operators:

 Concatena on (.): Combines two strings.

 Concatena on assignment (.=): Appends a string to another.

Example:

php

CopyEdit

$a = "Hello ";

$b = "World!";

echo $a . $b; // Outputs: Hello World!

$a .= $b; // $a becomes "Hello World!"

5. What is the use of var_dump() in PHP?

 var_dump() displays structured informa on about variables: type, length, and value.

 Very useful for debugging.

Example:

php

CopyEdit

$a = array(1, 2, 3);

var_dump($a);

6. Analyze the difference between programming language and scrip ng language.

Aspect Programming Language Scrip ng Language

Compila on Usually compiled (e.g., C, C++) Usually interpreted (e.g., PHP, Python)

Execu on Standalone applica ons Runs inside another program

Speed Faster due to compila on Slower due to interpreta on

Examples Java, C++, Go PHP, JavaScript, Python


PRACTICAL RELATED QUESTIONS-

1. How we use if..else and elseif statement in PHP?


These control structures allow condi onal execu on of code blocks.

Syntax Example:

php

CopyEdit

$marks = 75;

if ($marks >= 90) {

echo "Grade A";

} elseif ($marks >= 75) {

echo "Grade B";

} else {

echo "Grade C";

2. Difference between if…else and switch statement.

Aspect if…else switch

Use Case Best for range or complex condi ons Best for comparing one variable to many values

Syntax Uses logical operators

Flexibility More flexible Less flexible

3. Difference between if…else and ternary operator.

Feature if…else Ternary Operator (?:)

Syntax More verbose More concise

Readability Clearer for complex logic Best for simple condi ons

Return Value Used for control flow Used to return values

4. Why break and con nue statement used in PHP?

 break: Exits from a loop or switch case early.

 con nue: Skips current itera on and moves to the next loop cycle.
PRACTICAL RELATED QUESTIONS

1. Write PHP program to print Fibonacci series.

php

CopyEdit

<?php

$n = 10;

$first = 0;

$second = 1;

echo "Fibonacci series up to $n terms: ";

for ($i = 0; $i < $n; $i++) {

echo $first . " ";

$next = $first + $second;

$first = $second;

$second = $next;

?>

2. Write a PHP program to print prime numbers up to n.

php

CopyEdit

<?php

$n = 20;

echo "Prime numbers up to $n: ";

for ($i = 2; $i <= $n; $i++) {

$isPrime = true;

for ($j = 2; $j <= sqrt($i); $j++) {

if ($i % $j == 0) {

$isPrime = false;
break;

if ($isPrime) {

echo $i . " ";

?>

3. Difference between for and foreach loop.

Feature for Loop foreach Loop

Used for itera ng over


Usage Used for numeric itera ons
arrays/collec ons

Syntax More complex Simpler for arrays

Example for ($i = 0; $i < 5; $i++) foreach ($array as $value)

4. Difference between while and do-while loop.

Feature while Loop do-while Loop

Condi on Check Before loop execu on A er loop execu on

Yes (runs at least


Guaranteed Execu on No
once)

do { ... } while ($i <


Syntax Example while ($i < 5)
5)
PRACTICAL RELATED QUESTIONS

1. What is an anonymous func on?

An anonymous func on in PHP is a func on without a name. It is also called a closure and is o en
used for callbacks or when a short func on is needed temporarily.

Syntax Example:

php

CopyEdit

$sayHello = func on($name) {

return "Hello, $name!";

};

echo $sayHello("John"); // Outputs: Hello, John!

2. Write the difference between built-in func on and user-defined func on.

Feature Built-in Func on User-defined Func on

Defini on Provided by PHP by default Created by the programmer

Example strlen(), array_merge(), var_dump() func on greet() { echo "Hi"; }

Usage Can be used directly without defining Must be defined before use

Flexibility Limited to predefined tasks Customized for specific needs

3. What is a variable func on?

A variable func on in PHP means calling a func on using a variable that holds its name. The variable
behaves like a func on name.

Example:

php

CopyEdit

func on greet() {

echo "Hello!";

$func = "greet";

$func(); // Outputs: Hello!

This allows dynamic func on calling.

You might also like