Unit 1
Unit 1
1.3 Decision making Control statements if, if-else, nested if, switch, break and continue statement.
History:-
PHP Wasoriginally created by RasmusLerdorf in 1994.write in programs in C with several Common Gateway
he used that PHP code to maintain his personal homepage. He extended them to work with web forms and to
communicate with databases, and called this implementation "Personal Home Page/Forms Interpreter" or
PHP/FI.
PHP/FI could be used to build simple, dynamic web applications. To accelerate bug reporting and improve the
code.
Lerdorf initially announced the release of PHP/FI as "Personal Home Page Tools (PHP Tools) version 1.0"
PHP stands for PHP: Hypertext Preprocessor.
In 1998 PHP 3 the name "Personal Home Page/Forms Interpreter changed to Hypertext Preprocessor
PHP is Open Source Programming Language.
PHP is a server - side scripting language , which means that PHP scripts, or programs, usually run on a Web
server.
scripts which are especially designed for the client scripts which are especially designed for the server
(Browser) support those scripting language are called support those scripting language are called client side
client side scripting language scripting language
This is not language Secure because source code can This is language Secure because source code can’t
be visible to the user and also we can edit the source visible to the user and also we can’t edit the source
code. code.
This will be executed by the Browser This will be executed by the server
Ex.HTML,Javascript,AJAX,Jqueryetc Ex.PHP,JAVA,.NETetc
Characteristics of PHP:-
There are many features given by PHP. All Features discussed below one by one.
1. Familiarity
2. Simplicity
3. Efficiency
4. Security
5. Flexibility
6. Open source
7. Object Oriented
1.Familiarity: -
If you are in programming background then you can easily understand the PHP syntax. And you can
write PHP script because of most of PHP syntax inherited from other languages like C or Pascal.
2.Simplicity: -
PHP provides a lot of pre-define functions to secure your data. It is also compatible with many third-
party applications, and PHP can easily integrate with other.
In PHP script there is no need to include libraries like c, special compilation directives like Java, PHP
engine starts execution from (<?) escape sequence and end with a closing escape sequence (<?). In PHP
script, there is no need to write main function. And also you can work with PHP without creating a class.
3.Efficiency: -
PHP 4.0 introduced resource allocation mechanisms and more pronounced support for object-oriented
programming, in addition to session management features. Eliminating unnecessary memory allocation.
4.Security: -
Several trusted data encryption options are supported in PHP’s predefined function set. You can use a
lot of third-party applications to secure our data, allowing for securing our application.
5.Flexibility: -
You can say that PHP is a very flexible language because of PHP is an embedded language you can
embed PHP scripts with HTML, JAVA SCRIPT, WML, XML, and many others. You can run your PHP
script any device like mobile Phone, tabs, laptops, PC and other because of PHP script execute on the server
then after sending to the browser of your device.
6.Free: -
PHP is an open source programming language so you can download freely there is no need to buy a
license or anything.
7.Object Oriented: -
PHP has added some object-oriented programming features, and Object Oriented programming became
possible with PHP 4. With the introduction of PHP 5, the PHP developers have really beefed up the object-
oriented features of PHP, resulting in both more speed and added features.
Advantages of PHP:-
Following are some important advantages of PHP which are as follows:
1. Open Source
2. Platform Independent
3. Simple and Easy
4. Database
5. Fast
6. Maintenance
7. Support:
8. Testing
9. Security
10. Stable
1. Open Source:-
PHP is open-source and free of cost, which helps developers to install it quickly and readily available for
use. There are a lot of PHP frameworks and developer can choose any of the frameworks to work. All the
features and tools will be provided to the developer for that framework easily. As it is open-source, it makes
the system ready with PHP in quick time and makes the web development faster with the help of providing
the tools and other features easily.
2. Platform Independent:-
PHP is mainly supported by all the operating systems like Windows, Unix, Linux etc. The PHP based
developed web applications can be easily run on any platform. It can be integrated with other programming
language and database easily and there is no requirement of re-development. It helps in saving a lot of effort
and cost.
3. Simple and Easy:-
This advantage of PHP is simple and easy to learn and code. It is mainly organized code and clean,
which helps the new developers also. The command functions of PHP can easily learn and understood. The
A PHP script starts with <? php and ends with ?>:
A PHP file normally contains HTML tags, and some PHP scripting code.
<?php
?>
Example:-
<html>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>
What is Variables?
It is reference name for the data
By using Variables we can store data/information to use in future program.
How to declare /Initialize variable in PHP:-
To declare we have to use $ symbol
Syntax:-$variablename;
Example:-
$a; $b; $A; $value; $Roll_no;
$Student_name; $Count;
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
A variable starts with the $ sign, followed by the name of the variable
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive ($age and $AGE are two different variables)
<?php
$a = 5;
$b = 6;
echo $txt1
$txt1;
$sum=$a + $b;
echo $sum;
?>
Program2.write
.write a program to performarithmeticoperations?
<?php
$a = 16;
$b = 8;
$="Addition
Addition of a and b ";
$sum=$a + $b;
echo $sum;
echo "<br>";
$sub=$a - $b;
echo $sub;
echo "<br>";
$mul=$a * $b;
echo $mul;
echo "<br>";
$div=$a / $b;
echo $div;
echo "<br>";
?>
Comments in PHP:-
Use // OR #
Example :-
<!DOCTYPE html>
<html>
<body>
<? php
echo "Computer Department";
</body>
</html>
/* */
Example :-
<!DOCTYPE html>
<html>
<body>
<? php
echo "Computer Department";
*/
?>
</body>
</html>
PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can
be categorized further in 3 types:
1.Scalar Types
It holds only single value. There are 4 scalar data types in PHP.
1. Boolean
2. integer
3. float
4. string
2.Compound Types
1. array
2. object
3.Special Types
1. resource
2. NULL
1.Boolean
Booleans are the simplest data type works like switch. It holds only two values: TRUE (1) or FALSE (0). It is
often used with conditional statements. If the condition is correct, it returns TRUE otherwise FALSE.
2.Integer
Integer means numeric data with a negative or positive sign. It holds only whole numbers, i.e., numbers without
fractional part or decimal points.
Example:
<?php
$dec1 = 34;
$oct1 = 0243;
$hexa1 = 0x45;
echo "Decimal number: " .$dec1. "</br>";
echo "Octal number: " .$oct1. "</br>";
echo "HexaDecimal number: " .$hexa1. "</br>";
?>
Decimal number: 34
Octal number: 163
HexaDecimal number: 69
3.Float
A floating-point number is a number with a decimal point. Unlike integer, it can hold numbers with a fractional
or decimal point, including a negative or positive sign.
Example:
<?php
$n1 = 19.34;
$n2 = 54.472;
$sum = $n1 + $n2;
echo "Addition of floating numbers: " .$sum;
?>
Output:
4.String
A string is a non-numeric data type. It holds letters or any alphabets, numbers, and even special characters.
String values must be enclosed either within single quotes or in double quotes. But both are treated differently.
To clarify this, see the example below:
Example:
<?php
$company = "SPC";
//both single and double quote statements will treat different
echo "Hello $company";
echo "</br>";
echo 'Hello $company';
?>
Hello SPC
Hello $company
1.Array
An array is a compound data type. It can store multiple values of same data type in a single variable.
Example:
<?php
var_dump($bikes);
echo "</br>";
?>
Output:
array(3) { [0]=> string(7) "Unicorn" [1]=> string(7) "Passion" [2]=> string(8) "Splender" }
Array Element1: Unicorn
Array Element2: Passion
Array Element3: Splender
2.object
Objects are the instances of user-defined classes that can store both values and functions. They must be
explicitly declared.
Example:
<?php
class bike {
function model()
{
$model_name = "Unicorn";
echo "Bike Model: ";
echo $model_name;
}
}
$obj = new bike();
$obj -> model(); //->
?>
Output:
Bike Model: Unicorn
1.Resource
Resources are not the exact data type in PHP. Basically, these are used to store some function calls or references
to external PHP resources. For example - a database call. It is an external resource.
2.Null
Null is a special data type that has only one value: NULL. There is a convention of writing it in capital letters as
it is case sensitive
The special type of data type NULL defined a variable with no value.
Example:
<?php
$nl = NULL;
echo $nl; //it will not give any output
?>
PHP Expressions
Introduction
Almost everything in a PHP script is an expression. Anything that has a value is an expression. In a typical
assignment statement ($x=100), a literal value, a function or operands processed by operators is an expression,
anything that appears to the right of assignment operator (=)
Syntax
$x=100; //100 is an expression
<?php
$x=10;
$y=$x++; //equivalent to $y=$x followed by $x=$x+1
echo "x = $x y = $y";
?>
Output
This produces following result
x = 11 y = 10
Whereas following example uses prefix increment operator in assignment
<?php
$x=10;
$y=++$x;; //equivalent to $x=$x+1 followed by $y=$x
echo "x = $x y = $y";
?>
Output
This produces following result
x = 11 y = 11
<?php
$marks=60;
$result= $marks<50 ? "fail" : "pass";
echo $result;
?>
Output
Following result will be displayed
pass
PHP Operators
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
Conditional assignment operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as
addition, subtraction, multiplication etc.
The PHP assignment operators are used with numeric values to write a value to a variable.
The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the
assignment expression on the right.
x=y x=y The left operand gets set to the value of the expression
on the right
x -= y x=x–y Subtraction
x /= y x=x/y Division
The PHP comparison operators are used to compare two values (number or string):
=== Identical $x === $y Returns true if $x is equal to $y, and they are of Try
the same type it »
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are
not of the same type
>= Greater than $x >= $y Returns true if $x is greater than or equal to $y Try
or equal to it »
<=> Spaceship $x <=> $y Returns an integer less than, equal to, or greater Try
than zero, depending on if $x is less than, equal it »
to, or greater than $y. Introduced in PHP 7.
PHP has two operators that are specially designed for strings.
The PHP conditional assignment operators are used to set a value depending on conditions:
PHP Constants
A constant is an identifier (name) for a simple value. The value cannot be changed during the script.
A valid constant name starts with a letter or underscore (no $ sign before the constant name).
Note: Unlike variables, constants are automatically global across the entire script.
Syntax:-
define (name, value, case-insensitive)
Parameters:
Program1:-
<?php
?>
Output:-
<?php
define("student_name", ["Ram","Sham","Sita","Gita"]);
echo student_name[3];
?>
Output:- Gita
1.if
2.if-else
3.netsed if
4.swtich
5.Break
6.Continue
1. The if Statement:-
Syntax
if (check condition)
Program:-
<?php
$a =10;
$b =20;
if ($a < $b)
{
echo "Value of a less then b";
}
?>
Output:-
Syntax
if (Check condition)
{
if condition is true then code to be executed;
} else
{
if condition is false then code to be executed;
Program:-
<?php
$a =50;
$b =10;
if ($a < $b)
{
echo "Value of a less then b";
}
else
{
echo "Value of a Greater then b";
}
?>
Output:-
Syntax
if (condition)
{
if condition is true then code to be executed;
} elseif (condition)
{
if first condition is false and this condition is true code to be executed;
} else
{
if all conditions are false code to be executed;
}
Program:-
<?php
$a =40;
$b =40;
if ($a < $b)
{
echo "Value of a less then b";
}
elseif($a > $b)
{
echo "Value of a Greater then b";
}
else
{
echo "Value of a and b are Equal ";
}
?>
Use the switch statement to select one of many blocks of code to be executed.
Syntax
switch (n)
{
case label1:
if n=label1code to be executed;
break;
case label2:
if n=label2 code to be executed;
break;
case label3:
if n=label3 code to be executed;
break;
...
default:
if n is different from all labels code to be executed;
}
Program:-
<?php
$student = 1;
switch ($student)
{
case "1":
echo "My Name is Ram and Roll No is 1";
break;
case "2":
echo "My Name is Sham and Roll No is 2";
Output:-
5.Break:-
You have already seen the break statement used in an earlier chapter of this tutorial. It was used to
"jump out" of a switch statement.
Program:-
<?php
if ($x == 5) {
break;
?>
6.Continue :-
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues
with the next iteration in the loop.
Program:-
<?php
$x = 1;
while($x < 10) {
if ($x == 5) {
$x++;
continue;
}
echo "Presnt Roll No is: $x <br>";
$x++;
}
?>
Output:-
Often when you write code, you want the same block of code to run over and over again a certain
number of times. So, instead of adding several almost equal code-lines in a script, we can use loops.
Loops are used to execute the same block of code again and again, as long as a certain condition is true.
1 while - loops through a block of code as long as the specified condition is true
2 do...while - loops through a block of code once, and then repeats the loop as long as the specified
condition is true
3 for - loops through a block of code a specified number of times
4 foreach - loops through a block of code for each element in an array
1.while Loop
The while loop - Loops through a block of code as long as the specified condition is true.
The while loop executes a block of code as long as the specified condition is true.
Syntax
while (condition is true) {
code to be executed;
}
Examples
Example
<?php
$x = 1;
3.do...while Loop
The do...while loop - Loops through a block of code once, and then repeats the loop as long as the specified
condition is true.
The do...while loop will always execute the block of code once, it will then check the condition, and repeat the
loop while the specified condition is true.
Syntax
do
{
code to be executed;
} while (condition is true);
Example
<?php
$x = 1;
do {
echo "Roll No is: $x <br>";
$x++;
} while ($x <= 10);
?>
Note: In a do...while loop the condition is tested AFTER executing the statements within the loop.
3.for Loop
The for loop - Loops through a block of code a specified number of times.
The for loop is used when you know in advance how many times the script should run.
Syntax
for (init counter; test counter; increment counter)
{
code to be executed for each iteration;
}
Parameters:
Example
<?php
for ($x = 0; $x <= 10; $x++) {
echo "Roll No is: $x <br>";
}
?>
4.foreach Loop
The foreach loop - Loops through a block of code for each element in an array.
The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.
Syntax
foreach ($array as $value)
{
code to be executed;
}
For every loop iteration, the value of the current array element is assigned to $value and the array pointer is
moved by one, until it reaches the last array element.
Examples
The following example will output the values of the given array ($colors):