Very
Short Answers
1.
What is string?
A string in PHP is a sequence of
characters enclosed in single or double quotes. It is used to store and
manipulate text data like names, sentences, etc.
2. Define single quoted
string?
Single quoted strings in PHP
treat content as plain text. Variables inside them are not parsed. Example:
'Hello $name' will output literally Hello $name.
3. Discuss double quoted
string?
Double quoted strings allow variable parsing and special
characters. Example: "Hello $name" will output value of $name. They
support escape sequences like
\n, \t.
4. What is heredoc?
Heredoc syntax allows creating
multi-line strings using <<<. It behaves like double quotes and
supports variables. Useful for large blocks of text or HTML.
5. What is Nowdoc?
Nowdoc
is similar to Heredoc but behaves like single quotes. It doesn’t parse
variables or escape characters.
Declared using <<<'EOT'
syntax. Best for raw text.
6. What is function?
A function is a reusable block of code that performs a
task. It is defined using function keyword and can accept parameters and return
values.
Short Answers
1. Difference between
sprintf() and print() functions in PHP? sprintf() returns a formatted
string without printing it. You can store or reuse it. print() directly outputs
a string to the browser. sprintf() is used for advanced formatting, print() for
simple output.
2. Explain preg_match() and
preg_replace() functions? preg_match() searches a string for a pattern and
returns true if matched. preg_replace() searches for a pattern and replaces it
with another string. Both use Perl-style regular expressions for pattern
matching.
3. What is POSIX regular
function?
POSIX regular functions are older
pattern matching functions in PHP using POSIX syntax. Examples include ereg(),
eregi(), ereg_replace(). They are deprecated in PHP 5.3 and removed in PHP 7.
4. Advantages of regular
expression?
Regular expressions allow
powerful pattern matching and validation. They help in form validation, data
extraction, and search-replace operations. They reduce complex string logic
into simple patterns.
5. Explain call by value and
call by reference? In call by value, a copy of the variable is passed to
the function, and changes don't affect the original. In call by reference
(&), changes inside function reflect outside too.
Long
Answer
1. Explain different types
of string functions? PHP offers many string functions:
•
strlen() – Returns length
of string.
•
strtoupper() / strtolower()
– Converts to upper/lowercase.
•
substr() – Extracts part of
a string.
•
strpos() – Finds position
of a substring.
•
str_replace() – Replaces a
part of string with another.
•
trim() – Removes whitespace
from start and end.
•
explode() – Splits string
into array.
•
implode() – Joins array
into string.
•
strrev() – Reverses a
string.
•
strcmp() – Compares two
strings.
These functions help in
manipulation, validation, formatting, and processing of strings in PHP
applications.
2. Explain various format
specifiers.
Format specifiers are used in
functions like printf() and sprintf() for formatting output. Common specifiers:
•
%s – String
•
%d – Integer
•
%f – Floating point
•
%x – Hexadecimal
•
%b – Binary
•
%c – Character Example:
$name = "John";
printf("Hello %s",
$name); // Output: Hello John
They allow structured and styled
output in web applications.
3. What is regression
function? Explain in detail. In PHP, regression functions aren't built-in,
but they can be created for statistical analysis. Regression analyzes
relationship between variables. For example, in linear regression, we predict y
from x using a formula like y = mx + c. You can write PHP code to calculate
slope, intercept, and predict values. Libraries like PHP-ML or MathPHP also
help in implementing regression.
4. Discuss various string
library functions? Common PHP string library functions:
•
addslashes() – Escapes
quotes.
•
htmlspecialchars() – Converts
special HTML characters.
•
md5() / sha1() – Hashes a
string.
•
nl2br() – Inserts
<br> before newlines.
•
ucfirst() – Capitalizes
first letter.
•
ucwords() – Capitalizes
each word.
•
wordwrap() – Wraps a string
to given width.
•
str_repeat() – Repeats
string multiple times.
These functions enhance string
formatting, security, and user input processing.
5. Explain parameterized and
recursive functions? Parameterized
functions accept input values called parameters to perform operations.
Example:
function add($a, $b) { return $a + $b;
}
Recursive
functions call themselves within the function. They solve problems like
factorial, tree traversal. Example: function factorial($n) { if ($n == 0) return 1; return $n * factorial($n - 1);
}
Both improve modularity and code
reusability in PHP.

0 Comments