[go: up one dir, main page]

0% found this document useful (0 votes)
96 views5 pages

PHP QB

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

 Explain any three data types used in PHP.

Answer:
Variables can store data of different types, and different data types can do different
things.
PHP supports the following data types:
 String
 Integer
 Float (floating point numbers - also called double)
 Boolean
 Array
 Object
 NULL
 Resource

PHP Integer
An integer data type is a non-decimal number between -2,147,483,648 and
2,147,483,647.
Rules for integers:
 An integer must have at least one digit
 An integer must not have a decimal point
 An integer can be either positive or negative
example: <?php
$x = 5985;
var_dump($x);
?>

PHP Float
A float (floating point number) is a number with a decimal point or a number in
exponential form.
In the following example $x is a float. The PHP var_dump() function returns the data
type and value:
<?php
$x 10.365;
var_dump($x);
?>

PHP Boolean
A Boolean represents two possible states: TRUE or FALSE.
$x = true;
$y = false

b) Explain Indexed and Associative arrays with suitable example.


Indexed
An array is a data structure that stores one or more similar type of values in a single
indexed arrays the value is accessed using indexes 0,1,2 etc.
 These types of arrays can be used to store any type of elements,
but an index is always a number. By default, the index starts at
zero.
Array initialization
First method

ii)Associative array
Associative arrays are used to store key value pairs.
Associative arrays have strings as keys and behave more like two-column tables.
The first column is the key, which is used to access the value.
?php
/* First method to create an associate array. */
$student_one = array("Maths"=>95, "Physics"=>90,
"Chemistry"=>96, "English"=>93,
"Computer"=>98);
Second method to create an associate array.
$student_two["Maths"] = 95;
$student_two["Physics"] = 90;
$student_two["Chemistry"] = 96;
$student_two["English"] = 93;
$student_two["Computer"] = 98;

 Write different operators available in PHP?


Comparison Operator
Bitwise Operator
Logical Operator
Assignment Operator
Miscellaneous Operator
1)Error Suppression(@)
2) Execution(‘…’)
3)Conditional Operator(?:)

 Explain any four string functions in PHP with example.


1. strlen() function : This function is used to find number of characters in a string .
While counting number characters from string, function also considers spaces
between words.
syntax : strlen(string);
- string specify name of the string from which characters have to be counted.
Example :
<?php
$str3="Hello,welcome to WBP";
echo "<br> Number of characters in a string '$str3' = ".strlen($str3);
?>

2. strrev() function : This function accepts string as an argument and returns a


reversed copy of it.
Syntax : $strname=strrev($string variable/String );
Example :
<?php
$str4="Polytechnic";
$str5=strrev($str4);
echo "Orginal string is '$str4' and reverse of it is '$str5'";
?>

3)ucwords() function: This function is used to convert first character of each word
from the string into uppercase.
Syntax : $variable=ucwords($Stringvar);
Example :
<?php
$str9="welcome to poly for web based development";
echo ucwords($str9);
?>

4. strtoupper() function :This function is used to convert any character of string into
uppercase.
Syntax : $variable=strtoupper($stringvar);
Example:
<?php
$str9="POLYtechniC";
echo strtoupper($str9);
?>
5) strtolower() function : This function is used to convert any character of string into
lowercase.
Syntax: $variable=strtolower($stringvar);
Example :
<?php
$str9="POLYtechniC";
echo strtolower($str9);
?>

 Define function. How to define user defined function? Explain with example.

A function is an independent named block of code that is defined to perform a


specific task.
A function can be called from another part of the program, and may or may not
return a value. Code inside a function are not executed until the function is called.

Besides the built-in PHP functions, it is possible to create out own functions,
according to requirements known as user defined functions.
A user defined function is a block of statements that can be used repeatedly in a
program.
A user-defined function declaration starts with the key word ‘function’ followed by
name as the function.
The function name can be any string that starts with a letter or underscore followed
by zero or more letters, underscores, and digits.
Syntax:
function functionName()
{
//code to be executed;
}
Example: For user defined functions.
<?php
function writeMsg()
echo "Hello world!";
writeMsg();
?>
Output:
Hello world!

 Write a program to check whether given number is positive and negative.


<?php
$num = 10

if ($num > 0) {
echo "The number is positive";
} elseif ($num == 0) {
echo "The number is zero";
} else {
echo "The number is negative";
}
?>

 Write PHP program to print Fibonacci series


<?php  
$num = 0;  
$n1 = 0;  
$n2 = 1;  
echo "<h3>Fibonacci series for first 12 numbers: </h3>";  
echo "\n";  
echo $n1.' '.$n2.' ';  
while ($num < 10 )  
{  
    $n3 = $n2 + $n1;  
    echo $n3.' ';  
    $n1 = $n2;  
    $n2 = $n3;  
    $num = $num + 1;  
?>  

 Write a program to create PDF document in PHP


To create a PDF document in PHP, you can use a third-party library such as FPDF.

Download FPDF library from https://www.fpdf.org/ and extract it to your PHP


project directory.
<?php
require('fpdf/fpdf.php');

$pdf = new FPDF();


$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(40, 10, 'Hello World!');
$pdf->Output();
?>

You might also like