PHP 2 Marks Final Draft
PHP 2 Marks Final Draft
1. Give the syntax and example for defining a user-defined function in PHP.
Ans: To define a function, use the following syntax:
function [&] function_name([parameter[, ...]])
{
statement list
}
The statement list can include HTML. You can declare a PHP function that doesn’t
contain any PHP code. For instance, the column() function simply gives a
convenient short name to HTML code that may be needed many times throughout
the page:
6. How do you access global variables within a PHP function? Give example
Ans: If you want a variable in the global scope to be accessible from within a
function, you can use the global keyword. Its syntax is:
global var1, var2, ...
Changing the previous example to include a global keyword, we get:
Instead of creating a new variable called $a with function-level scope, PHP uses the
global $a within the function. Now, when the value of $a is displayed, it will be 5.
//MA
8. What is "pass by reference" in PHP function parameters?
Ans:
Passing by reference allows you to override the normal scoping rules and give a
function direct access to a variable. To be passed by reference, the argument must
be a variable; you indicate that a particular argument of a function will be passed by
reference by preceding the variable name in the parameter list with an ampersand
(&).
Eg:
9. What is Default Parameter in PHP functions? Give example
Ans:
Sometimes a function may need to accept a particular parameter. For example,
when you call a function to get the preferences for a site, the function may take in a
parameter with the name of the preference to retrieve. Rather than using some
special keyword to designate that you want to retrieve all of the preferences, you can
simply not supply any argument. This behavior works by using default argument
Eg:
function getPreferences($whichPreference = 'all')
{ // if $whichPreference is "all", return all prefs;
// otherwise, get the specific preference requested...
}
//AB
14. Explain the two ways to interpolate variables into strings?
= The simpler of the two ways is to put the variable name in a double-quoted string
or heredoc:
$who = 'Kilroy';
$where = 'here';
echo "$who was $where";
Kilroy was here
The other way is to surround the variable being interpolated with curly braces. Using
this syntax ensures the correct variable is interpolated. The classic use of curly
braces is to disambiguate the variable name from surrounding text:
$n = 12; echo "You are the {$n}th person";
You are the 12th person
Without the curly braces, PHP would try to print the value of the $nth variable.
PHP strings are not repeatedly processed for interpolation. Instead, any
interpolations in a double-quoted string are processed first and the result is used as
the value of the string:
$bar = 'this is not printed'; $foo = '$bar';
// single quotes print("$foo");
$bar
The print() construct sends one value (its argument) to the browser:
if (print("test")) {
print("It worked!");
}
It worked!
You can specify multiple items to print by separating them with commas using echo
echo "First", "second", "third";
//DJ
20. Explain trim function in php along with its types?
trim() returns a copy of string with whitespace removed from the beginning and the
end. ltrim() (the l is for left) does the same, but removes whitespace only from the
start of the string. rtrim() (the r is for right) removes whitespace only from the end of
the string.
21. How can you include one PHP file into another PHP file?
We can include one PHP file in another by using the include statement. Syntax is
include “filename.php”;
example if we create a file as connection.php with the code of database connection
it can be used in any other PHP file using include “connection.php”;
22. Name three commonly used PHP string manipulation functions.
substr() function
strrev() function
str_repeat() function
str_pad() function
explode() function
implode() function
23. Explain strcasecmp() with example?
Its is a variation on strcmp() , converts strings to lowercase before comparing them.
Its arguments and return values are the same as those for strcmp():
Eg: $n = strcasecmp("Fred", "frED");
Compares two strings; returns a number less than 0 if one is less than two, 0 if the
two strings are equal, and a number greater than 0 if one is greater than two. The
comparison is case insensitive—that is, “Alphabet” and “alphabet” are considered
equal.
24. Differentiate strncmp() and strncasecmp() in php?
strncmp():
$relationship = strcmp(string_1, string_2);
The function returns a number less than 0 if string_1 sorts before string_2, greater
than 0 if string_2 sorts before string_1, or 0 if they are the same
strcasecmp():
which converts strings to lowercase before comparing them. Its arguments and
return values are the same as those for strcmp():
$n = strcasecmp("Fred", "frED");
25. Write the syntax of substr_count() PHP with example
Syntax:
$number = substr_count(big_string, small_string);
//AN
26. Write the syntax of substr_replace() PHP with example
The substr_replace() function permits many kinds of string modifications:
$string = substr_replace(original, new, start [, length ]);
The function replaces the part of original indicated by the start (0 means the start of
the string) and length values with the string new. If no fourth argument is given,
substr_replace() removes the text from start to the end of the string.
For instance:
$greeting = "good morning citizen";
$farewell = substr_replace($greeting, "bye", 5, 7);
// $farewell is "good bye citizen"
27. What is the purpose of the substr() function in PHP? Give example
you can copy it out with the substr() function:
$piece = substr(string, start [, length ]);
The start argument is the position in string at which to begin copying, with 0 meaning
the start of the string. The length argument is the number of characters to copy (the
default is to copy until the end of the string). For example:
$name = "Fred Flintstone";
$fluff = substr($name, 6, 4); // $fluff is "lint"
$sound = substr($name, 11); // $sound is "tone"
29. List two PHP library function for handling date and time.
//gpt
date()
strtotime():
//VSS
32. Define overloading in the context of PHP classes.
//gemini
These methods handle interactions with properties or methods that aren't explicitly
defined within the class.
33. What is a constructor in PHP classes? How it is declared ?
A special function that initializes the properties of the class is called as constructor.
A constructor is a function in the class called __construct(). Here’s a constructor for
the Person class:
class Person
{
function __construct($name, $age)
{
$this->name = $name;
$this->age = $age;
}
}
35. How do you access object properties and methods within a class in PHP? Give
example
Once you have an object, you can use the -> notation to access methods and
properties
of the object:
$object->propertyname
$object->methodname([arg, ... ])
For example:
echo "Rasmus is {$rasmus->age} years old.\n"; // property access
$rasmus->birthday(); // method call
$rasmus->setAge(21); // method call with arguments
Methods act the same as functions (only specifically to the object in question), so
they
can take arguments and return a value:
$clan = $rasmus->family("extended");
36. What is a trait in PHP, and how does it differ from classes and interfaces?
Traits
Traits provide a mechanism for reusing code outside of a class hierarchy. Traits
allow
you to share functionality across different classes that don’t (and shouldn’t) share a
common ancestor in a class hierarchy.
//gpt
While a class can only inherit from one parent class, it can use multiple traits
simultaneously.