INTRODUCTION TO
PHP
■ Origins and uses of PHP
■ Overview of PHP
■ General Syntactic Characteristics
■ Primitives
■ Operations and Expressions
■ Output
■ Control Statements
Origins and uses of PHP
• PHP was developed by Rasmus Lerdorf a member
of the Apache Group.
• In 1995 Lerdorf originally named it Personal Home
Page (PHP) but currently is known asHypertext
Preprocessor.
• PHP is a server-side, scripting language executed on
the server.
• PHP is a widely used, open-source scripting language .
Overview of PHP
PHP processor has two modes of operations:
1. Copy mode :
On the server side when the PHP processor finds XHTML code in the input
file, it simply copies it to the output file.
2. Interpret mode:
On the server side when it encounters a PHP script in the input file, it
interprets it and sendsany output of the script to the output file.
General Syntactic Characteristics
• PHP scripts can be embedded within the XHTML document by enclosing
it between the <?php and ?> tags.
• Example:
<!DOCTYPE html>
<html>
<body>
<? php
// single-line comment
# single-line comment
/*
multiple-lines
comment block
*/
echo “Hello World”;
?>
</body>
</html>
PHP allows comments to be specified in three different ways:
• Single-line comments can be specified either with # or with //.
• Multiple-line comments are delimited with /* and */ , as in many other
programming languages.
• PHP statements are terminated with semicolons(;).
• All variables in PHP begin with dollar sign($) followed by the name of the
variable. The variable names are case sensitive, $age and $Age are not same.
• Variables are not declared in PHP; they have a value and the type of the value.
• A variable name cannot start with a number. A variable name can only contain
alpha-numeric characters and underscores (A-z, 0-9, and _ ).
• A variable name must start with a letter or the underscore character.
• Reserved words of PHP
• Although variable names in PHP are case sensitive, neither reserved words nor
function names are
• For example,
there is no difference between while, WHILE, While, wHile.
Primitives, operations and expressions
• PHP has four scalar data types:
-Boolean
- Integer
-Double and
-String
• PHP has two compound data types:
- Array
- Object
• PHP has two special types:
-Resource
-NULL
• Integer type:
-PHP integer is corresponds to long in C, it is usually 32 bits.
• Double type:
-PHP's double type corresponds to C's double type.
-It can have a decimal point, exponent or both.
-There does not need to be any digits before or after the decimal point, both
are legal. EX: .345 345.
• String type:
-String literals are defined with either single (’) or double quotes (")
delimiters.
• In single quoted string literals, escape characters such as n, are not
recognized as anythingspecial and the values of embedded variables are
not substituted.
• In double-quoted string literals, escape sequences are recognized and
embedded variables arereplaced by their current values.
• Ex:
• Boolean type
Boolean values are either TRUE or FALSE, both of which are case
insensitive.
Output
• echo and print are both used to output data to the screen.
• echo has no return value and can take multiple parameters. print has return value
1 and cannot take multiple parmeters.
Ex:
Control Statements
• The control structures in PHP are very similar to C/C++/Java.
• The control statements include:
- if and if-else
-switch
- while and do-while
- for and foreach
If-elseif-else
• This statement executes different codes for more
than two conditions.
• EX: <?php
if ($x > $y)
{ echo "x is bigger than y"; }
elseif ($x == $y)
{ echo "x is equal to y"; }
else
{ echo "x is smaller than y"; }
?>
switch statement
• The switch statement is used to perform different action based on different
conditions.
• Example:
<?php
$x = 2;
switch ($x) {
case 1:printf("Choice is 1");
break;
case 2:printf("Choice is 2");
break;
case 3:printf("Choice is 3");
break;
default:printf("Choice other than 1, 2 and 3");
}
?>
While loop
• The while loop excute the
statement until the specified
condition is true.
• Ex:
<html>
<body>
<?php
$x = 1;
while($x <= 5) {
echo " $x ";
$x++;
}
?>
</body>
</html>
Do while loop
• It excutes the block of the code
and then check for the condition.
• Ex:
<html>
<body>
<?php
$x = 1;
do {
echo " $x ";
$x++;
} while ($x <= 5);
?>
</body>
</html>
For loop
• For loop is a control structure that repeats a block of code as long as a condition
is met. It's usually used to repeat a block of code a certain number of times.
• Example:
<!DOCTYPE html>
<html>
<body>
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
</body>
</html>
for-each
• The foreach loop works only on array and is used to loop through each
key/value pair in array.
• Example:
Thank you

Introduction to php contains basic....pptx

  • 1.
  • 2.
    ■ Origins anduses of PHP ■ Overview of PHP ■ General Syntactic Characteristics ■ Primitives ■ Operations and Expressions ■ Output ■ Control Statements
  • 3.
    Origins and usesof PHP • PHP was developed by Rasmus Lerdorf a member of the Apache Group. • In 1995 Lerdorf originally named it Personal Home Page (PHP) but currently is known asHypertext Preprocessor. • PHP is a server-side, scripting language executed on the server. • PHP is a widely used, open-source scripting language .
  • 4.
    Overview of PHP PHPprocessor has two modes of operations: 1. Copy mode : On the server side when the PHP processor finds XHTML code in the input file, it simply copies it to the output file. 2. Interpret mode: On the server side when it encounters a PHP script in the input file, it interprets it and sendsany output of the script to the output file.
  • 5.
    General Syntactic Characteristics •PHP scripts can be embedded within the XHTML document by enclosing it between the <?php and ?> tags. • Example: <!DOCTYPE html> <html> <body> <? php // single-line comment # single-line comment /* multiple-lines comment block */ echo “Hello World”; ?> </body> </html>
  • 6.
    PHP allows commentsto be specified in three different ways: • Single-line comments can be specified either with # or with //. • Multiple-line comments are delimited with /* and */ , as in many other programming languages. • PHP statements are terminated with semicolons(;). • All variables in PHP begin with dollar sign($) followed by the name of the variable. The variable names are case sensitive, $age and $Age are not same. • Variables are not declared in PHP; they have a value and the type of the value.
  • 7.
    • A variablename cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ). • A variable name must start with a letter or the underscore character.
  • 8.
    • Reserved wordsof PHP • Although variable names in PHP are case sensitive, neither reserved words nor function names are • For example, there is no difference between while, WHILE, While, wHile.
  • 9.
    Primitives, operations andexpressions • PHP has four scalar data types: -Boolean - Integer -Double and -String • PHP has two compound data types: - Array - Object • PHP has two special types: -Resource -NULL
  • 10.
    • Integer type: -PHPinteger is corresponds to long in C, it is usually 32 bits. • Double type: -PHP's double type corresponds to C's double type. -It can have a decimal point, exponent or both. -There does not need to be any digits before or after the decimal point, both are legal. EX: .345 345. • String type: -String literals are defined with either single (’) or double quotes (") delimiters.
  • 11.
    • In singlequoted string literals, escape characters such as n, are not recognized as anythingspecial and the values of embedded variables are not substituted. • In double-quoted string literals, escape sequences are recognized and embedded variables arereplaced by their current values. • Ex: • Boolean type Boolean values are either TRUE or FALSE, both of which are case insensitive.
  • 12.
    Output • echo andprint are both used to output data to the screen. • echo has no return value and can take multiple parameters. print has return value 1 and cannot take multiple parmeters. Ex:
  • 13.
    Control Statements • Thecontrol structures in PHP are very similar to C/C++/Java. • The control statements include: - if and if-else -switch - while and do-while - for and foreach
  • 14.
    If-elseif-else • This statementexecutes different codes for more than two conditions. • EX: <?php if ($x > $y) { echo "x is bigger than y"; } elseif ($x == $y) { echo "x is equal to y"; } else { echo "x is smaller than y"; } ?>
  • 15.
    switch statement • Theswitch statement is used to perform different action based on different conditions. • Example: <?php $x = 2; switch ($x) { case 1:printf("Choice is 1"); break; case 2:printf("Choice is 2"); break; case 3:printf("Choice is 3"); break; default:printf("Choice other than 1, 2 and 3"); } ?>
  • 16.
    While loop • Thewhile loop excute the statement until the specified condition is true. • Ex: <html> <body> <?php $x = 1; while($x <= 5) { echo " $x "; $x++; } ?> </body> </html> Do while loop • It excutes the block of the code and then check for the condition. • Ex: <html> <body> <?php $x = 1; do { echo " $x "; $x++; } while ($x <= 5); ?> </body> </html>
  • 17.
    For loop • Forloop is a control structure that repeats a block of code as long as a condition is met. It's usually used to repeat a block of code a certain number of times. • Example: <!DOCTYPE html> <html> <body> <?php for ($x = 0; $x <= 10; $x++) { echo "The number is: $x <br>"; } ?> </body> </html>
  • 18.
    for-each • The foreachloop works only on array and is used to loop through each key/value pair in array. • Example:
  • 19.