PHP Unit 1 Solved
PHP Unit 1 Solved
2 MARKS:
1. What does PHP stand for? Give PHP code to display “Hello World”
Answer:
Code:
<?php
?>
Answer:
Open Source
Cross-Platform Compatibility
Answer:
PEAR stands for "PHP Extension and Application Repository." It is a framework and
distribution system for reusable PHP components.
Answer:
You can include PHP code in an HTML file using the <?php ... ?> tags.
Example:
<!DOCTYPE html>
<html>
<body>
?>
</body>
</html>
Answer:
PHP ignores white spaces in code, so you can use them for readability.
Example:
<?php
$variable1 = 5;
$variable2 = 10;
?>
Answer:
Answer:
The lexical structure of a programming language is the set of basic rules that define the
structure of tokens, comments, and white spaces.
8. What is a constant in PHP? Give example
Answer:
A constant is an identifier for a simple value that cannot be changed during the
execution of the script.
Example:
<?php
echo GREETING;
?>
Answer:
Single-line comment: //
Shell-style comment: #
Example:
<?php
?>
10. What is the purpose of the echo statement in PHP? Give example
Answer:
Example:
<?php
?>
<?php
?>
Answer:
String
Integer
Float (Double)
Boolean
Array
Object
NULL
Resource
Answer:
13. Explain the function used to test whether a value is a resource in PHP? Give example
Answer:
Example:
<?php
if (is_resource($connection)) {
} else {
}
?>
Answer:
Example:
<?php
function my_callback_function() {
function caller($callback) {
call_user_func($callback);
?>
Answer:
The phpinfo() function outputs a large amount of information about the current state of
PHP, including configuration settings, installed extensions, and PHP environment
variables.
Answer:
Variable reference means that di erent variables point to the same data.
Example:
<?php
$a = "Hello";
$b =& $a;
$b = "World";
echo $a; // Outputs: World
?>
Answer:
Static scope: A static variable retains its value between function calls.
Example:
<?php
function testStatic() {
static $count = 0;
$count++;
echo $count;
testStatic(); // 1
testStatic(); // 2
function testGlobal() {
global $globalVar;
echo $globalVar;
?>
Answer:
Example:
<?php
$a = 10;
$b = 5;
$c = 2;
?>
19. What are auto increment and auto decrement operators in PHP? Give example
Answer:
Example:
<?php
$a = 5;
$a++; // $a is now 6
$b = 10;
$b--; // $b is now 9
?>
Answer:
You declare and initialize a variable by using the dollar sign $ followed by the variable
name and assignment operator =.
Example:
<?php
?>
21. What is type juggling in PHP? Write the rules for type juggling in PHP
Answer:
Type juggling refers to PHP's ability to automatically convert a variable from one type to
another as needed.
Rules:
Answer:
==: Equality operator. Checks if the values are equal, after type juggling.
===: Identity operator. Checks if the values are identical and of the same type.
Answer:
Ternary operator: Operates on three operands. It's a shorthand for if-else (e.g., condition
? expr1 : expr2).
Example:
<?php
$a = 10;
$b = 5;
?>
Answer:
Casting operators are used to explicitly convert a value from one type to another.
Examples:
(int) or (integer)
(float) or (double) or (real)
Example:
<?php
$number = "10.5";
?>
Answer:
Example:
<?php
define("SITE_NAME", "MyWebsite");
?>
Answer:
The backtick operator is used to execute shell commands directly from PHP.
Example:
<?php
echo "<pre>$output</pre>";
?>
Answer:
The execution operator (backticks) allows you to execute shell commands from within
PHP.
Example:
<?php
$result = `whoami`;
?>
LONG ANSWERS:
Answer:
Open Source:
PHP is free to use and open source, which means it is developed and maintained by a
worldwide community of developers who contribute to its ongoing development and
support. This allows for continuous improvement and extensive documentation.
Cross-Platform Compatibility:
Database Integration:
PHP is compatible with almost all web servers used today, including Apache, Nginx,
Microsoft IIS, and more. This allows PHP scripts to be executed on di erent server
setups, making it a flexible choice for web development.
2. Explain brief history of PHP 2.0 along with its feature list
Answer:
PHP 2.0, released in 1997, marked a significant evolution from its predecessor. It
introduced a new parsing engine, enhancing the performance and functionality of the
language.
The new parser, written by Zeev Suraski and Andi Gutmans, improved the execution
speed and expanded the language capabilities.
PHP 2.0 enabled developers to build more complex web applications compared to
earlier versions.
Improved Syntax:
Enhanced syntax and better language constructs made coding easier and more
e icient.
Extensibility:
Introduction of an extension API, allowing developers to write their own extensions and
further extend PHP's functionality.
Answer:
Download PHP:
Extract PHP:
Find the "Path" variable in the "System variables" section, select it, and click "Edit."
Configure php.ini:
Open php.ini and configure it as needed (e.g., set extension_dir to C:\php\ext and
enable necessary extensions).
Configure your web server (Apache, Nginx, IIS) to work with PHP by editing the server
configuration files to recognize PHP file extensions and process them using the PHP
executable.
Answer:
Embedding PHP within HTML allows you to create dynamic web pages that can interact
with the server and display dynamic content.
Example:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Welcome to My Website</h1>
<?php
?>
</body>
</html>
In this example, the PHP code inside <?php ... ?> tags is executed by the server, and the
result is embedded into the HTML page that is sent to the browser.
Answer:
To send data to the web browser, PHP uses the echo or print statements. These
commands output data that the server sends to the client (web browser).
Example:
<?php
header("Content-Type: text/html");
?>
In this example, header("Content-Type: text/html"); sets the content type to HTML, and
the echo statements send the HTML content to the browser.
Answer:
Scalar data types in PHP represent single values. The main scalar data types are:
Integer:
<?php
$intVar = 123;
?>
Float (Double):
<?php
$floatVar = 3.14;
?>
String:
A sequence of characters.
<?php
Boolean:
<?php
$boolVar = true;
?>
Answer:
Array:
<?php
?>
Object:
<?php
class Fruit {
public $name;
function setName($name) {
$this->name = $name
Object:
<?php
class Fruit {
public $name;
function setName($name) {
$this->name = $name
function getName() {
return $this->name;
$apple->setName("Apple");
?>
1. **NULL:**
```php
<?php
$nullVar = NULL;
?>
Resource:
<?php
if (is_resource($handle)) {
echo "This is a valid resource.";
?>
Answer:
In PHP, variable scope refers to the context within which a variable is defined and
accessible. The main types of scope are:
Local Scope:
<?php
function localScopeExample() {
localScopeExample();
?>
Global Scope:
<?php
function globalScopeExample() {
global $globalVar;
globalScopeExample();
?>
Static Scope:
<?php
function staticScopeExample() {
static $staticVar = 0;
$staticVar++;
echo $staticVar;
staticScopeExample(); // Outputs: 1
staticScopeExample(); // Outputs: 2
?>
Super global variables are built-in variables that are always accessible, regardless of
scope. Examples include $_GET, $_POST, $_SESSION, and $_SERVER.
<?php
?>
Answer:
The cycle collector periodically scans for and collects circular references, freeing up
memory used by objects that are no longer accessible.
10. Explain any two categories of Operators with example
Answer:
Arithmetic Operators:
<?php
$a = 10;
$b = 5;
?>
Comparison Operators:
<?php
$a = 10;
$b = 5;
?>
Answer:
Casting operators in PHP are used to convert a variable from one data type to another
explicitly. PHP supports several casting operators, including:
(int) or (integer):
<?php
$var = "10.5";
$intVar = (int)$var;
?>
<?php
$var = "10.5";
$floatVar = (float)$var;
?>
(string):
<?php
$var = 123;
$stringVar = (string)$var;
?>
(array):
<?php
$var = "hello";
$arrayVar = (array)$var;
print_r($arrayVar); // Outputs: Array ( [0] => hello )
?>
(object):
<?php
$var = "hello";
$objectVar = (object)$var;
?>
Answer:
<?php
echo "<pre>$output</pre>";
?>
<?php
$file = @file('non_existent_file.txt');
if (!$file) {
?>
class MyClass {}
?>
Returns its first operand if it exists and is not null; otherwise, it returns its second
operand.
<?php
$name = null;
$defaultName = "Guest";
?>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++