PHP QB
PHP QB
PHP QB
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
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;
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.
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!
if ($num > 0) {
echo "The number is positive";
} elseif ($num == 0) {
echo "The number is zero";
} else {
echo "The number is negative";
}
?>