12.module4 PHP-INTRO 10 PDF
12.module4 PHP-INTRO 10 PDF
12.module4 PHP-INTRO 10 PDF
Prof.N.Nalini
AP(Sr)
SCOPE
VIT
PHP INTRODUCTION
4
PHP with HTML
5
PHP Syntax
Closing Tag
6
PHP Programs
Code
Result
<HTML>
<HEAD>
<TITLE>
My First PHP Program
</TITLE> <h1>
</HEAD>
<BODY>
<?php
echo “I’m a PHP Program.”;
?>
</BODY>
</HTML>
7
PHP Programs
Code Result
<?php
$name=“VIT UNIVERSITY”;
echo ‘My name is ‘.$name;
?>
8
Comment Line in PHP
2.# -comment
3.Multi Lines Comment - Multi lines
comment in PHP is identified by /* … */
<?Php
/* line1
line2
line 3 */
?>
9
10
Output in PHP
<?php
echo “Hello World”;
print “Hello World”; // prints out Hello World
?>
11
• Echo display the outputs one or more strings
separated by commas.
• It accepts an list of argument (multiple arguments
can be passed) and returns no value or returns void.
12
Output in PHP
<?php
echo $hits; // prints out the number of hits
print $hits; // returns a value if print process success.
?>
<?php
echo $a, “<br>”,$b ; //prints a & b in separate
lines
?>
13
Data Type
14
<?php
PHP STRING $x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
• A string is a sequence echo $y;
of characters, like ?>
"Hello world!".
• A string can be any
text inside quotes. You
can use single or
double quotes:
Quotes (“ Vs. ‘)
In a double-quoted string any variable names are expanded
to their values.
In a single-quoted string, no variable expansion takes place.
Output is Output is
$name is $age abc is 23
16
<?php
$x = 5985;
PHP INTEGER var_dump($x);
?>
• An integer is a whole number (without
decimals). It is a number between -
2,147,483,648 and +2,147,483,647.
• Rules for integers:
• An integer must have at least one digit (0-9)
• An integer cannot contain comma or blanks
• An integer must not have a decimal point
• An integer can be either positive or
negative
• Integers can be specified in three formats:
decimal (10-based), hexadecimal (16-
based - prefixed with 0x) or octal (8-based
- prefixed with 0)
PHP FLOAT <?php
$x = 10.365;
var_dump($x);
?>
20
21
22
Variables
23
Variables
Rules for Naming User Defined Variables:
In PHP, unlike some other programming languages,
there is no restriction on the size of a variable name.
24
Variables
$prod_desc $9OctSales
$Intvar Sales123
$_Salesamt $*asgs
25
Variables (cont)
26
VARIABLES (CONT)
$Ball = "a";
$Foo = "Ball";
$World = "Foo";
$Hello = "World";
$a = "Hello";
$a;
$$a;
$$$a;
$$$$a;
$$$$$a;
1. $$$$$$a; --------------
2. $$$$$$$a; -----------------
27
$a; //Returns Hello
$$a; //Returns World
$$$a; //Returns Foo
$$$$a; //Returns Ball
$$$$$a; //Returns a
1. $$$$$$a; --------------
2. $$$$$$$a; -----------------
28
Assign by Reference
29
30
31
PHP VARIABLES SCOPE
32
33
PHP SUPER GLOBALS
Super Globals are variables that are automatically
available throughout all program code in all scopes.
These variables do not require declaration and then too
they can be accessed.
Super global variables provide :-
Useful information about the environment.
Allow access to HTML form variables or parameters.
Access to cookies stored on a client.
Keeping track of sessions and file uploads.
Name Functionality
Contains all variables sent via a HTTP GET request. That is, sent
$_GET
by way of the URL.
$_FILES Contains all variables sent via a HTTP POST file upload.
Contains all variables set by the web server you are using, or
$_SERVER
other sources that directly relate to the execution of your script.
Name Functionality
Contains all variables sent via HTTP GET, HTTP POST, and HTTP
cookies. This is basically the equivalent of combining $_GET,
$_POST, and $_COOKIE, and is less dangerous than using
$_REQUEST
$GLOBALS. However, as it does contain all variables from
untrusted sources (that is, your visitors), you should still try to
steer clear unless you have very good reason to use it.
Settype
Syntax:
Settype(Variablename, “newDataType”);
E.g.
$pi = 3.14 //float
Settype($pi,”string”); //now string – “3.14”
Settype($pi,”integer”);// now integer - 3
37
Settype & Gettype
In PHP, Variable type and can know by Gettype.
Syntax:
Gettype(Variablename);
E.g.
$pi = 3.14;
print gettype($pi);
Print “---$pi <br>”;
Settype($pi,”string”);
print gettype($pi);
Print “---$pi <br>”;
Settype($pi,”integer”);
print gettype($pi);
Print “---$pi <br>”;
38
HERE DOCUMENTS
The heredoc string structure is a method of including larger strings inside the
php code. To create a heredoc, you use a special operator that is made
up of three left brackets ( <<< ). The syntax is as follows:
Syntax:
39
Operators
40
PHP ARITHMETIC
OPERATORS
PHP ASSIGNMENT
OPERATORS
Assignment Same as... Description
$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
PHP COMPARISON
OPERATORS
Operator Name Example Result
== Equal $x == $y Returns true if $x is equal to $y
=== Identical $x === $y Returns true if $x is equal to $y, and they are of the
same type
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of
the same type
>= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y
<= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y
PHP INCREMENT /
DECREMENT
OPERATORS
Operator Name Description
or Or $x or $y True if either $x or $y is
true
|| Or $x || $y True if either $x or $y is
true
=== Identity $x === $y Returns true if $x and $y have the same key/value
pairs in the same order and of the same types
Syntax E.g.
if (<condition>) if ($num1 > $num2)
{ {
//True part $max = $num1;
Php code }
} else
else {
{ $max = $num2;
//False part }
php code
}
48
Conditional Statements
(Branching)
2. If…else if Statement – multiple decision stmt
Syntax
if (<condition1>) E.g.
{
//True part Php code
} if ($num1 > $num2)
else if (<condition2>) {
{ $max = $num1;
//1st False 2nd true part }
php code else if ($num2 > $num3)
} {
else $max = $num3;
{ }
// false part PhP code else
} $max = $num2;
49
Conditional Statements
(Branching)
3. Switch Statement – replacement of “if ……
else if stmt”.
E.g.
Syntax
switch ($day)
switch (expression) {
{ case 1: {echo “its Sunday”;
case value1: {stmts1; break;}
break;} case 2: {echo “its Monday”;
case value2: {stmts2; break;}
break;} ………
………… ………
………… case 7: {echo “its Saturday”;
[default: stmts;] break;}
} default: {echo “not specified
value”;}
}
50
Conditional Statements
(Looping)
4. For Statement – replacement of “if …… else
if stmt”.
Syntax E.g.
51
FOREACH LOOP
The foreach construct provides an easy way to iterate over arrays.
foreach works only on arrays and objects.
foreach will issue an error when you try to use it on a variable with a different data type or
an uninitialized variable.
Syntax : 1 foreach (array_expression as $value)
statement // indexed array
Syntax : 2 foreach (array_expression as $key => $value)
statement // associative array
When foreach first starts executing, the internal array pointer is automatically reset to the
first element of the array. Associative Array :
Indexed Array : <?php
<?php $arr = array(1=>’ABC’,2=>’PQR’);
$array = array(1,2,3,4); foreach ($arr as $key => $val) {
foreach($array as $val) { print "$key = $val\n";
print $val; } }
?> ?>
Conditional Statements
(Looping)
5. Do … While Statement
Syntax E.g.
do do
{ {
Statement (S); $i=$i+1;
} echo $i;
while (<condition>); } while ($i < 10);
53
Conditional Statements
(Looping)
5. While Statement
Syntax E.g.
54
<html>
<body>
<form method=POST action="<?php $_SERVER[“PHP_SELF”]; ?>">
[or]
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Enter your name <input type=text name="tname" value=" ">
<br>
<input type=submit name="submit" value="Click">
</form>
<?php
//if ($_SERVER["REQUEST_METHOD"] == "POST")
if($_POST['submit']=="Click")
{
$var=$_POST['tname'];
echo "Your name is ".$var;
}
?>
</body>
</html>
55