PHP - I UNIT
PHP - I UNIT
2 marks:
1. What does PHP stand for? Give PHP code to display “Hello World”.
PHP stands for “Hypertext Preprocessor”. The PHP code to display “Hello World” is given below:
<?php
echo "Hello World";
?>
2. List any four features of PHP.
• Open Source
• Cross-Platform Compatibility
• Database Integration
• Web Server Support
3. Expand PEAR? What it is?
PEAR stands for "PHP Extension and Application Repository." It is a framework and distribution
system for reusable PHP components. PEAR provides a structured library of code that can be used
to simplify web development tasks, offering standardized code packages for various functionalities,
from database access to authentication.
4. How do you include PHP code in an HTML file? Give example.
You can include PHP code in an HTML file using the <?php ... ?> tags.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>My First PHP Page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
5. How to use white spaces in PHP? Give example.
Whitespace doesn’t matter in a PHP program. You can spread a statement across any number of
lines, or lump a bunch of statements together on a single line. PHP ignores white spaces in code, so
you can use them for readability.
For example, this statement: add($a, $b, $c);
could just as well be written with more whitespace:
add ($a ,
$b ,
$c ,
);
6. List steps to install and configure PHP in windows.
• Download the PHP zip file from the official website.
• Extract the zip file to a directory (e.g., C:\php).
• Add the PHP directory to the Windows PATH environment variable.
• Configure the php.ini file (e.g., enable necessary extensions).
• Restart the web server (e.g., Apache, IIS).
7. What is lexical structure?
The lexical structure of a programming language is the set of basic rules that define the structure of
tokens, comments, and white spaces. It is the lowest-level syntax of the language and specifies such
things as what variable names look like, what characters are used for comments, and how program
statements are separated from each other.
8. What is constant in php. Give example.
A constant is an identifier for a simple value that cannot be changed during the execution of the
script. To create a constant, use the define() function. For example,
<?php
define("GREETING", "Welcome to PHP!");
echo GREETING;
?>
9. What are different ways of writing comments in PHP? Give example.
• Single-line comment: //
• Multi-line comment: /* ... */
• Shell-style comment: #
Example: <?php
// This is a single-line comment
# This is also a single-line comment
/* This is a multi-line comment
that spans multiple lines */
?>
10. What is the purpose of the echo statement in PHP? Give example.
The echo statement is used to output one or more strings. It is one of the most commonly used
methods for displaying text, variables, and HTML code in PHP scripts.
Example: <?php
echo "Hello World!";
?>
11. What are the main data types supported by PHP?
• String • Integer • Float (Double) • Boolean
• Array • Object • NULL • Resource
12. What are resource data type in PHP?
In PHP, a resource is a special variable, holding a reference to an external resource. Resources are
created and used by special functions within PHP to handle files, database connections, image
canvases, etc. The resource data type is unique in that it is used to represent and manipulate these
external resources.
13. Explain the function used to test whether a value is a resource in php? Give example.
The function is_resource() is used to test whether a value is a resource. This function returns true if
the variable is a resource, and false otherwise. Example:
<?php
$con = mysqli_connect("localhost", "user", "password", "database");
if (is_resource($con)) {
echo "This is a valid resource.";
} else {
echo "This is not a resource.";
} ?>
14. What are call backs? Give example.
A callback is a function that is passed as an argument to another function, which can then call the
callback function during its execution.
Example: $callback = function myCallbackFunction()
{
echo "callback achieved";
}
call_user_func($callback);
15. What is the purpose of phpinfo()?
The PHP function phpinfo() creates an HTML page full of information on how PHP was installed
and is currently configured. This function outputs a large amount of information about the current
state of PHP, including configuration settings, installed extensions, and PHP environment variables.
16. What is variable reference? Give example.
Variable reference means that different variables point to the same data.
Example: <?php
$a = "Hello";
$b = & $a;
$b = "World";
echo $a; // Outputs: World
?>
17. What is static and global scope of variable in PHP?
Static scope: A static variable retains its value between all calls to a function.
Eg: <?php
function test() {
static $count = 0;
$count++;
echo $count;
}
test(); // Outputs: 1
test(); // Outputs: 2
Global scope: A global variable is accessible in any part of the script.
Eg: $a = 3;
function foo()
{
global $a;
echo $a;
}
foo(); // Outputs: 3
18. What is operator associativity? Give example.
Operator associativity determines the order in which operators of the same precedence are
processed. Most operators have left-to-right associativity, meaning they are processed from left to
right. Example: <?php
$a = 10; $b = 5; $c = 2;
$result = $a - $b - $c; // Left-associative: (10 - 5) - 2 = 3
echo $result; // Outputs: 3
?>
19. What are auto increment and auto decrement Operators in PHP? Give example.
Auto increment (++): Increases a variable's value by one.
Auto decrement (--): Decreases a variable's value by one.
Example: <?php
$a = 5;
$a++; // $a is now 6
echo $a; // Outputs: 6
$b = 10;
$b--; // $b is now 9
echo $b; // Outputs: 9
?>
20. How do you declare and initialize a variable in PHP?
You declare and initialize a variable by using the dollar sign $ followed by the variable name and
assignment operator =.
Example: <?php
$variable = "Hello World";
echo $variable; // Outputs: Hello World
?>
21. What is type juggling in php? Write the rules for type juggling in php.
Type juggling refers to PHP's ability to automatically convert a variable from one type to another as
needed. Rules:
Type of first operand Type of second operand Conversion performed
Integer Floating point The integer is converted to a floating-point number.
The string is converted to a number; if the value after
Integer String conversion is a floating-point number, the integer is
converted to a floating-point number.
Floating point String The string is converted to a floating-point number.
The HTML structure remains the same, with a <!DOCTYPE html> declaration, and `html`, `head`,
and `body` tags. Inside the `body` tag, the PHP code is enclosed within <?php ... ?> tags. echo is a
PHP statement used to output text or HTML to the web page. The text inside echo is wrapped in
double quotes and enclosed within HTML paragraph tags <p>. When the server processes this PHP
code, it sends the resulting HTML to the client's browser. The PHP code itself is not visible to the
user, only the HTML output it generates.
5. Explain how to Sending Data to Web browser with code example.
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). The browser receives the HTML content
generated by PHP and renders it for the user.
Example: <?php
header("Content-Type: text/html");
echo "<h1>Data sent to the browser</h1>";
echo "<p>This is a paragraph sent from the server to the browser.</p>";
?>
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.
6. Explain various scalar data types in PHP with example.
Integer: An integer is a non-decimal number, either positive or negative, including zero. It can be
written in decimal, hexadecimal, or octal format.
Example: <?php
$intVar = 123;
echo $intVar; // Outputs: 123
?>
Float (Double) : A float, also known as a double, is a number with a decimal point or in exponential
form. It represents real numbers.
Example: <?php
$floatVar = 3.14;
echo $floatVar; // Outputs: 3.14
?>
String: A string is a sequence of characters enclosed in single quotes ('') or double quotes (" ").
Strings can contain letters, numbers, and special characters.
Example: <?php
$stringVar = "Hello, PHP!";
echo $stringVar; // Outputs: Hello, PHP!
?>
Boolean: A boolean represents two possible values: true or false. Booleans are often used in
conditional statements to control the flow of the program.
Example: <?php
$boolVar = true;
echo $boolVar; // Outputs: 1 (true is displayed as 1)
?>
Object: An object is an instance of a class. A class is a blueprint for objects, defining properties and
methods.
Example: <?php
class Fruit {
public $name;
function setName($name) {
$this->name = $name
}
function getName() {
return $this->name;
}
}
$apple = new Fruit();
$apple->setName("Apple");
echo $apple->getName(); // Outputs: Apple
?>
Special Data Types
NULL: The `NULL` data type represents a variable with no value. It is the only value of the
`NULL` type and can be assigned to any variable.
Example: <?php
$nullVar = NULL;
echo $nullVar; // Outputs nothing
?>
Resource: A resource is a special variable that holds a reference to an external resource, such as a
database connection or a file handle. Resources are created and used by specific functions.
Example: <?php
$handle = fopen("file.txt", "r");
if (is_resource($handle)) {
echo "This is a valid resource.";
}
?>
Global Scope: Variables declared outside of any function or class have a global scope. They can be
accessed from anywhere in the script, including inside functions and methods.
Example: <?php $globalVar = 20; // Global variable
function myFunction() {
global $globalVar;
echo $globalVar; // Output: 20
}
myFunction();
?>
Static variables: are declared inside functions but retain their values between function calls. They
are accessible only within the function where they are declared.
Example: <?php function myFunction() {
static $staticVar = 0; // Static variable
echo $staticVar;
$static Var++; // Increment the static variable
}
myFunction(); // Output: 0
myFunction(); // Output: 1
myFunction(); // Output: 2
?>
Super Global Scope: Super global variables are built-in variables that are always accessible,
regardless of scope. Examples include $_GET, $_POST, $_SESSION, and $_SERVER.
Example: <?php
echo $_SERVER['PHP_SELF']; // Outputs the filename of the currently executing script
?>
Comparison Operators: Comparison operators are used to compare two values and return a
boolean result indicating whether the comparison is true or false.
== Checks if two values are equal.
!= Checks if two values are not equal.
=== Checks if two values are equal and of the same type.
!== Checks if two values are not equal or not of the same type.
> Checks if one value is greater than another.
< Checks if one value is less than another.
>= Checks if one value is greater than or equal to another.
<= Checks if one value is less than or equal to another.
Example: <?php $a = 10;
$b = 5;
var_dump($a == $b); // Outputs: bool(false)
var_dump($a != $b); // Outputs: bool(true)
var_dump($a > $b); // Outputs: bool(true)
var_dump($a < $b); // Outputs: bool(false)
var_dump($a >= $b); // Outputs: bool(true)
var_dump($a <= $b); // Outputs: bool(false)
?>