Professional Documents
Culture Documents
PHP Chapter1
PHP Chapter1
PHP Chapter1
WHAT IS PHP?
PHP Stands for Hypertext PreProcessor but it’s original name, Personal
Home Page. It was developed in 1994 by Rasmus Lerdorf.
PHP open source and general purpose server side scripting language
used mainly in web development to create dynamic websites and
applications and can embedded into HTML.
ADVANTAGES OF PHP:
1. Platform Independent
2. Open source and dynamic Library support
3. Organized
4. Database Connectivity
5. Easy to understand and It helps in managing code easily.
6. Gives Web Developer More Control
7. Easy integration and consistency
8. Maintenance
9. Stability, Performance, Reliability, Scalability, Compatibility
SYNTAX OF PHP:
Syntax:
<?php(starts with)
// PHP code goes here
?>(ends with)
PHP VARIABLES
Variable are container which is used to store information. A variable
can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume).
Types of Variable:
Local, Global, Static
Rules for PHP variables:
1. A variable starts with the $ sign, followed by the name of the
variable
2. A variable name must start with a letter or the underscore
character
3. A variable name cannot start with a number
4. A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
5. Variable names are case-sensitive ($age and $AGE are two
different variables)
Remember that PHP variable names are case-sensitive!
How to write a variable name? How to print this variable?
$a=”Aditi”;
echo $a;
DATA TYPES:
$a=”Hello World”
1. Arithmetic operator:
The PHP arithmetic operators are used with numeric values to perform
common arithmetical operations, such as addition, subtraction, multiplication
etc.
x=y x=y The left operand gets set to the value of the
expression on the right
x += y x=x+y Addition
x -= y x=x-y Subtraction
x *= y x=x*y Multiplication
x /= y x=x/y Division
x %= y x=x%y Modulus
3. Comparison operators:
The PHP comparison operators are used to compare two values (number or
string).
4. Increment/Decrement operators:
The PHP increment operators are used to increment a variable's value. The
PHP decrement operators are used to decrement a variable's value.
5. Logical operators:
The PHP logical operators are used to combine conditional
statements.
6. String operators:
PHP has two operators that are specially designed for strings.
CONSTANT:
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 (cannot use $
sign before the constant name).
Constants variable are global variable.
Create a constant, use the define() function.
Syntax:
define(name, value, case-insensitive)
ex: define(num,120,true)
echo NUM;
Parameters:
1. name: Specifies the name of the constant
2. value: Specifies the value of the constant
3. case-insensitive: Specifies whether the constant name should be
case-insensitive. Default is false.
1) If Statement:
The if statement executes some code if one condition is true.
If condition false out
True
Statement
Syntax:
if (condition)
{
code to be executed if condition is true;
}
2) if-else Statement:
The if...else statement executes some code if a condition is true and
another code if that condition is false.
If condition false Statement2
True
Statement1
Syntax:
if (condition)
{
code to be executed if condition is true;
}
else
{
code to be executed if condition is false;
}
3) Nested If Statement:
We can make nesting statement by nesting the if statement. It means
when we insert a second if statement inside an if statement, we call
it nested if statement or nesting the if statement.
Syntax:
1. if (condition)
2. {
3. //code to be executed if condition is true
4. if (condition)
5. {
6. //code to be executed if condition is true
7. }
8. }
4) Switch Statement:
Use the switch statement to select one of many blocks of code to be
executed.
Syntax:
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
5) Break Statement:
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. The
break statement can also be used to jump out of a loop. This example
jumps out of the loop when x is equal to 4:
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4)
{
break;
}
echo "The number is: $x <br>";
}
?>
6) Continue Statement:
The continue statement breaks one iteration (in the loop), if a
specified condition occurs, and continues with the next iteration in
the loop.
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
}
?>
1) While Loop:
The while loop executes a block of code as long as the specified
condition is true.
Syntax:
Initialization
while (condition is true)
{
code to be executed;
increment/decrement
}
2) Do-while loop:
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:
Initialization
do
{
code to be executed;
increment/decrement
} while (condition is true);
3) For Loop:
The for loop is used when you know in advance how many times the
script should run.
Syntax:
for (Initialization; Condition; increment/decrement)
{
code to be executed for each iteration;
}
4) Foreach Loop:
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 arhray element.