PHP Unit II Part I
PHP Unit II Part I
A scripting language is a language that interprets scripts at runtime. The purpose of the scripts
is usually to enhance the performance or perform routine tasks for an application.
PHP stands for Hypertext Preprocessor and it is a server side scripting language that is used to
develop Static websites or Dynamic websites or Web applications. It is the preferred scripting
language for tech giants such as Facebook, Wikipedia, and Tumblr despite full-
stack JavaScript gaining popularity with upcoming developers
PHP Float-A float or floating point number is a number with a decimal point or a
number in exponential form.
The following example takes $a as a float and the PHP var_dump() function returns the
data type and value.
<?php $a = 14.763; var_dump($a); ?>
PHP Boolean-A Boolean represents two possible states: TRUE or FALSE. They are
often used in conditional testing.
$a = true; $b = false;
PHP Object-An object is a data type which stores data and information on how to
process that data. In PHP, an object must be explicitly declared. We need to declare a
class of object using the class keyword.
<?php
class Student {
function Student() { $this->name = "AMAN"; }
}
// create an object
$LNMI =new Student(); // show object properties
echo $LNMI->name; ?>
PHP Array-An array stores multiple values in one single variable. In the following
example, the PHP var_dump() function returns the data type and value.
<?php $students = array(“AMAN”,”Ram”,”Sohan”);
var_dump($students); ?>
PHP Variables -
PHP Variables - Variables are containers for storing information. All variables in
PHP are denoted with a leading dollar sign ($). Unlike other programming languages,
PHP has no command for declaring a variable. It is created the moment you first assign a
value to it.
Declaring PHP Variables:
<?php $txt = “Hello LNMI!”;
$a = 7; $b = 11.5; ?>
The PHP echo statement is often used to output data to the screen.
PHP Variables Scope
In PHP, variables can be declared anywhere in the script. The scope of a variable is the
part of the script where the variable can be used.
In PHP we have three different variable scopes:
Local – A variable declared within a function has a LOCAL SCOPE and can only be
accessed within that function:
<?php function myTest()
{ $a = 7; // local scope
echo “<p>Variable a inside function is: $a</p>”; }
myTest(); // using x outside the function will generate an error
echo “<p>Variable a outside function is: $a</p>”; ?>
Global– A variable declared outside a function has a GLOBAL SCOPE and can only be
accessed outside a function. The global keyword is used to access a global variable from
within a function:
<?php $a = 9; // global scope function
myTest() {
// using a inside this function will generate an error echo
“<p>Variable a inside function is: $a</p>”; }
myTest(); echo “<p>Variable a outside function is: $a</p>”; ?>
Static– When a function is executed, all of its variables are deleted. But if you want any
variable not to be deleted, the static keyword is used when you first declare the variable:
<?php function
myTest() {
static $a = 0;
echo $a; $a++;
}
myTest(); myTest(); myTest(); ?>
Example:
<!DOCTYPE html>
<html> <body>
<?php
$x = 10;
$y = 6;
echo "Addition of x and y is:";
echo $x + $y;
echo " <br/> Substration of x and y is:";
echo $x - $y;
echo " <br/> Multiplication of x and y is:";
echo $x * $y;
echo " <br/> Devision of x and y is:";
echo $x / $y;
echo " <br/> Exponentiation of x and y is:";
echo $x ** $y;
?>
</body> </html>
Assignment Operators
The PHP assignment operators are used with numeric values to write a value to a
variable.
Assignment Similar to Result
The left operand gets set to the value of the expression
a=b a=b
on the right.
a += b a=a+b Addition
a -= b a=a–b Subtraction
Comparison Operators- The PHP comparison operators are used to compare two
numbers or strings
Operator Name Example
== Equal $a == $b > Greater than $a > $b
=== Identical $a === $b < Less than $a < $b
Greater than or equal
!= Not equal $a != $b >= $a >= $b
to
<> Not equal $a <> $b <= Less than or equal to $a <= $b
!== Not identical $a !== $b
Logical Operators
The PHP logical operators are used to combine conditional statements.
Operator Name Example
and And True if both $a & $b are true
or Or True if either $a or $b are true
xor Xor True if either $a or $b are true, but not both
&& And True if both $a & $b are true
|| Or True if either $a or $b are true
! Not True if $a is not true
Array Operators
The PHP array operators are used to compare arrays.
Operator Name Example
+ Union $a + $b
== Equality $a == $b
=== Identity $a === $b
!= Inequality $a != $b
<> Inequality $a <> $b
!== Non-identity $a !== $b
PHP Conditional Statements
Conditional statements are used to perform different actions for different conditions. In
PHP we have the following conditional statements:
o if statement – executes some code if one condition is true
o if…else statement – executes some code if a condition is true and another code if
that condition is false
o if…elseif…else statement – executes different codes for more than two
conditions
o switch statement – selects one of many blocks of code to be executed
The if Statement
The if statement executes some code if one condition is true.
<?php $t = date("H"); if ($t < "15") { echo "Have a good day!"; }
?>