SlideShare ist ein Scribd-Unternehmen logo
1 von 29
Downloaden Sie, um offline zu lesen
basic concept of php(Gunikhan sonowal)
Introduction
PHP: Hypertext Preprocessor
Originally called “Personal Home Page Tools”
Popular server-side scripting technology
Open-source
Anyone may view, modify and redistribute source code
Supported freely by community
Platform independent
History
Started as a Perl hack in 1994 by Rasmus Lerdorf
developed to PHP/FI 2.0
By 1997 up to PHP 3.0 with a new parser
engine by Zeev Suraski and Andi Gutmans
Version 5.2.4 is current version, rewritten by
Zend to include a number of features, such as
an object model
Current is version 5
What is a PHP File?

PHP files can contain text, HTML, CSS, JavaScript, and
PHP code
PHP code are executed on the server, and the result is
returned to the browser as plain HTML
PHP files have extension ".php"
What Can PHP Do?
PHP can generate dynamic page content
PHP can create, open, read, write, and close
files on the server
PHP can collect form data
PHP can add, delete, modify data in your
database
PHP can restrict users to access some pages
on your website
PHP can encrypt data
Why PHP?
PHP runs on various platforms (Windows,
Linux, Unix, Mac OS X, etc.)
PHP is compatible with almost all servers
used today (Apache, IIS, etc.)
PHP supports a wide range of databases
PHP is free. Download it from the official PHP
resource: www.php.net
PHP is easy to learn and runs efficiently on
the server side
Basic PHP Syntax
A PHP script can be placed anywhere in the
document.
A PHP script starts with <?php and ends with ?>:
<?php
// PHP code goes here
?>
A PHP file normally contains HTML tags, and
some PHP scripting code.
Below, we have an example of a simple PHP file,
with a PHP script that uses a built-in PHP
function "echo" to output the text "Hello World!"
on a web page:
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
Comments in PHP

<html>
<body>
<?php
// This is a single line comment
# This is also a single line comment
/*This is a multiple lines comment block
that spans over more than
one line
*/
?>
</body>
</html>
PHP Case Sensitivity
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
PHP Variables

Rules for PHP variables:
A variable starts with the $ sign, followed by
the name of the variable
A variable name must start with a letter or the
underscore character
A variable name cannot start with a number
A variable name can only contain alphanumeric characters and underscores (A-z, 0-9,
and _ )
Variable names are case sensitive ($y and $Y
are two different variables)
Php variables examples
<?php
$x=5; // global scope
function myTest()
{
$y=10; // local scope
echo "<p>Test variables inside the function:<p>";
echo "Variable x is: $x";
echo "<br>";
echo "Variable y is: $y";
}
myTest();
echo "<p>Test variables outside the function:<p>";
echo "Variable x is: $x";
echo "<br>";
echo "Variable y is: $y";
?>
PHP Operators
<?php
$x=10;
$y=6;
echo ($x + $y); // outputs 16
echo ($x - $y); // outputs 4
echo ($x * $y); // outputs 60
echo ($x / $y); // outputs 1.6666666666667
echo ($x % $y); // outputs 4
?>
PHP String Operators
<?php
$a = "Hello";
$b = $a . " world!";
echo $b; // outputs Hello world!
$x="Hello";
$x .= " world!";
echo $x; // outputs Hello world!
?>
PHP Increment / Decrement
Operators

<?php
$x=10;
echo ++$x; // outputs 11
$y=10;
echo $y++; // outputs 10
$z=5;
echo --$z; // outputs 4
$i=5;
echo $i--; // outputs 5
?>
PHP if...else...elseif Statements
<?php
$t=21;
if ($t<"20")
{
echo "Have a good day!";
}
else
{
echo "Have a good night!";
}
?>
PHP switch Statement

<?php
$favcolor="red";
switch ($favcolor)
{
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, or green!";
}
?>
PHP while Loops
In PHP, we have the following looping
statements:
while - loops through a block of code as long
as the specified condition is true
do...while - loops through a block of code
once, and then repeats the loop as long as the
specified condition is true
The PHP while Loop
<?php
$x=1;
while($x<=5)
{
echo "The number is: $x <br>";
$x++;
}
?>
The PHP do...while Loop
<?php
$x=1;
do
{
echo "The number is: $x <br>";
$x++;
}
while ($x<=5)
?>
PHP for Loops
for (init counter; test counter; increment counter)
{
code to be executed;
}
Parameters:
init counter: Initialize the loop counter value
test counter: Evaluated for each loop iteration. If it
evaluates to TRUE, the loop continues. If it evaluates to
FALSE, the loop ends.
increment counter: Increases the loop counter value
Php for loops(cont'd)
The PHP foreach Loop
The foreach loop works only on arrays, and is used to loop
through each key/value pair in an array.
<?php
$colors = array("red","green","blue","yellow");
foreach ($colors as $value)
{
echo "$value <br>";
}
?>
<?php

PHP Functions

function writeMsg()
{
echo "Hello world!";
}
writeMsg(); // call the function
?>
PHP Function Arguments
<?php
function familyName($fname)
{
echo "$fname Refsnes.<br>";
}
familyName("Jani");
?>
PHP Arrays
<?php
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " .
$cars[2] . ".";
?>
In PHP, there are three types of arrays:
Indexed arrays - Arrays with numeric index
Associative arrays - Arrays with named keys
Multidimensional arrays - Arrays containing one or
more arrays
Array(cont'd)
PHP Indexed Arrays
There are two ways to create indexed arrays:
1.The index can be assigned automatically
(index always starts at 0):
$cars=array("Volvo","BMW","Toyota");
2.The count() function is used to return the
length (the number of elements) of an array:
Array(cont'd)

PHP Associative Arrays
Associative arrays are arrays that use named
keys that you assign to them.
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"
43");
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"
43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
PHP - Sort Functions For Arrays

The following PHP array sort functions:
sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending
order, according to the value
ksort() - sort associative arrays in ascending
order, according to the key
arsort() - sort associative arrays in descending
order, according to the value
krsort() - sort associative arrays in descending
order, according to the key
PHP Form Handling
PHP - A Simple HTML Form
<html>
<body>
<form action="welcome.php"
method="post">
Name: <input type="text"
name="name"><br>
E-mail: <input type="text"
name="email"><br>
<input type="submit">
</form>
</body>
</html>

Php program
<html>
<body>
Welcome <?php echo
$_POST["name"]; ?><br>
Your email address is: <?php
echo $_POST["email"]; ?>
</body>
</html>
Thanks you

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (19)

Php by shivitomer
Php by shivitomerPhp by shivitomer
Php by shivitomer
 
Php Tutorial | Introduction Demo | Basics
 Php Tutorial | Introduction Demo | Basics Php Tutorial | Introduction Demo | Basics
Php Tutorial | Introduction Demo | Basics
 
Php
PhpPhp
Php
 
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDESPHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Course
 
PHP Basics and Demo HackU
PHP Basics and Demo HackUPHP Basics and Demo HackU
PHP Basics and Demo HackU
 
Php Unit 1
Php Unit 1Php Unit 1
Php Unit 1
 
Php with my sql
Php with my sqlPhp with my sql
Php with my sql
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
 
Intro to php
Intro to phpIntro to php
Intro to php
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
PHP: The easiest language to learn.
PHP: The easiest language to learn.PHP: The easiest language to learn.
PHP: The easiest language to learn.
 
HackU PHP and Node.js
HackU PHP and Node.jsHackU PHP and Node.js
HackU PHP and Node.js
 
Introducation to php for beginners
Introducation to php for beginners Introducation to php for beginners
Introducation to php for beginners
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Php a dynamic web scripting language
Php   a dynamic web scripting languagePhp   a dynamic web scripting language
Php a dynamic web scripting language
 
07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards
 

Ähnlich wie basic concept of php(Gunikhan sonowal) (20)

WT_PHP_PART1.pdf
WT_PHP_PART1.pdfWT_PHP_PART1.pdf
WT_PHP_PART1.pdf
 
PHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERSPHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERS
 
Prersentation
PrersentationPrersentation
Prersentation
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHP
 
PHP
PHPPHP
PHP
 
Basic php 5
Basic php 5Basic php 5
Basic php 5
 
PHP Hypertext Preprocessor
PHP Hypertext PreprocessorPHP Hypertext Preprocessor
PHP Hypertext Preprocessor
 
Day1
Day1Day1
Day1
 
PHP TUTORIAL
PHP TUTORIALPHP TUTORIAL
PHP TUTORIAL
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Wt unit 4 server side technology-2
Wt unit 4 server side technology-2Wt unit 4 server side technology-2
Wt unit 4 server side technology-2
 
FYBSC IT Web Programming Unit IV PHP and MySQL
FYBSC IT Web Programming Unit IV  PHP and MySQLFYBSC IT Web Programming Unit IV  PHP and MySQL
FYBSC IT Web Programming Unit IV PHP and MySQL
 
Lecture8
Lecture8Lecture8
Lecture8
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
Php
PhpPhp
Php
 
Php unit i
Php unit iPhp unit i
Php unit i
 
Php My SQL Tutorial | beginning
Php My SQL Tutorial | beginningPhp My SQL Tutorial | beginning
Php My SQL Tutorial | beginning
 
Learning of Php and My SQL Tutorial | For Beginners
Learning of Php and My SQL Tutorial | For BeginnersLearning of Php and My SQL Tutorial | For Beginners
Learning of Php and My SQL Tutorial | For Beginners
 

Kürzlich hochgeladen

Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxraviapr7
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?TechSoup
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxiammrhaywood
 
KARNAADA.pptx made by - saransh dwivedi ( SD ) - SHALAKYA TANTRA - ENT - 4...
KARNAADA.pptx  made by -  saransh dwivedi ( SD ) -  SHALAKYA TANTRA - ENT - 4...KARNAADA.pptx  made by -  saransh dwivedi ( SD ) -  SHALAKYA TANTRA - ENT - 4...
KARNAADA.pptx made by - saransh dwivedi ( SD ) - SHALAKYA TANTRA - ENT - 4...M56BOOKSTORE PRODUCT/SERVICE
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...Nguyen Thanh Tu Collection
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17Celine George
 
Protein Structure - threading Protein modelling pptx
Protein Structure - threading Protein modelling pptxProtein Structure - threading Protein modelling pptx
Protein Structure - threading Protein modelling pptxvidhisharma994099
 
3.26.24 Race, the Draft, and the Vietnam War.pptx
3.26.24 Race, the Draft, and the Vietnam War.pptx3.26.24 Race, the Draft, and the Vietnam War.pptx
3.26.24 Race, the Draft, and the Vietnam War.pptxmary850239
 
Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.raviapr7
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptxmary850239
 
Work Experience for psp3 portfolio sasha
Work Experience for psp3 portfolio sashaWork Experience for psp3 portfolio sasha
Work Experience for psp3 portfolio sashasashalaycock03
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...CaraSkikne1
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptxSandy Millin
 
Over the counter (OTC)- Sale, rational use.pptx
Over the counter (OTC)- Sale, rational use.pptxOver the counter (OTC)- Sale, rational use.pptx
Over the counter (OTC)- Sale, rational use.pptxraviapr7
 
In - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxIn - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxAditiChauhan701637
 
How to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesHow to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesCeline George
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfYu Kanazawa / Osaka University
 

Kürzlich hochgeladen (20)

March 2024 Directors Meeting, Division of Student Affairs and Academic Support
March 2024 Directors Meeting, Division of Student Affairs and Academic SupportMarch 2024 Directors Meeting, Division of Student Affairs and Academic Support
March 2024 Directors Meeting, Division of Student Affairs and Academic Support
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptx
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
 
KARNAADA.pptx made by - saransh dwivedi ( SD ) - SHALAKYA TANTRA - ENT - 4...
KARNAADA.pptx  made by -  saransh dwivedi ( SD ) -  SHALAKYA TANTRA - ENT - 4...KARNAADA.pptx  made by -  saransh dwivedi ( SD ) -  SHALAKYA TANTRA - ENT - 4...
KARNAADA.pptx made by - saransh dwivedi ( SD ) - SHALAKYA TANTRA - ENT - 4...
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17
 
Protein Structure - threading Protein modelling pptx
Protein Structure - threading Protein modelling pptxProtein Structure - threading Protein modelling pptx
Protein Structure - threading Protein modelling pptx
 
3.26.24 Race, the Draft, and the Vietnam War.pptx
3.26.24 Race, the Draft, and the Vietnam War.pptx3.26.24 Race, the Draft, and the Vietnam War.pptx
3.26.24 Race, the Draft, and the Vietnam War.pptx
 
Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptx
 
Work Experience for psp3 portfolio sasha
Work Experience for psp3 portfolio sashaWork Experience for psp3 portfolio sasha
Work Experience for psp3 portfolio sasha
 
Prelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quizPrelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quiz
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
 
Over the counter (OTC)- Sale, rational use.pptx
Over the counter (OTC)- Sale, rational use.pptxOver the counter (OTC)- Sale, rational use.pptx
Over the counter (OTC)- Sale, rational use.pptx
 
In - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxIn - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptx
 
How to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesHow to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 Sales
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
 
Finals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quizFinals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quiz
 

basic concept of php(Gunikhan sonowal)

  • 2. Introduction PHP: Hypertext Preprocessor Originally called “Personal Home Page Tools” Popular server-side scripting technology Open-source Anyone may view, modify and redistribute source code Supported freely by community Platform independent
  • 3. History Started as a Perl hack in 1994 by Rasmus Lerdorf developed to PHP/FI 2.0 By 1997 up to PHP 3.0 with a new parser engine by Zeev Suraski and Andi Gutmans Version 5.2.4 is current version, rewritten by Zend to include a number of features, such as an object model Current is version 5
  • 4. What is a PHP File? PHP files can contain text, HTML, CSS, JavaScript, and PHP code PHP code are executed on the server, and the result is returned to the browser as plain HTML PHP files have extension ".php"
  • 5. What Can PHP Do? PHP can generate dynamic page content PHP can create, open, read, write, and close files on the server PHP can collect form data PHP can add, delete, modify data in your database PHP can restrict users to access some pages on your website PHP can encrypt data
  • 6. Why PHP? PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP supports a wide range of databases PHP is free. Download it from the official PHP resource: www.php.net PHP is easy to learn and runs efficiently on the server side
  • 7. Basic PHP Syntax A PHP script can be placed anywhere in the document. A PHP script starts with <?php and ends with ?>: <?php // PHP code goes here ?>
  • 8. A PHP file normally contains HTML tags, and some PHP scripting code. Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function "echo" to output the text "Hello World!" on a web page: <html> <body> <h1>My first PHP page</h1> <?php echo "Hello World!"; ?> </body> </html>
  • 9. Comments in PHP <html> <body> <?php // This is a single line comment # This is also a single line comment /*This is a multiple lines comment block that spans over more than one line */ ?> </body> </html>
  • 10. PHP Case Sensitivity <html> <body> <?php ECHO "Hello World!<br>"; echo "Hello World!<br>"; EcHo "Hello World!<br>"; ?> </body> </html>
  • 11. PHP Variables Rules for PHP variables: A variable starts with the $ sign, followed by the name of the variable A variable name must start with a letter or the underscore character A variable name cannot start with a number A variable name can only contain alphanumeric characters and underscores (A-z, 0-9, and _ ) Variable names are case sensitive ($y and $Y are two different variables)
  • 12. Php variables examples <?php $x=5; // global scope function myTest() { $y=10; // local scope echo "<p>Test variables inside the function:<p>"; echo "Variable x is: $x"; echo "<br>"; echo "Variable y is: $y"; } myTest(); echo "<p>Test variables outside the function:<p>"; echo "Variable x is: $x"; echo "<br>"; echo "Variable y is: $y"; ?>
  • 13. PHP Operators <?php $x=10; $y=6; echo ($x + $y); // outputs 16 echo ($x - $y); // outputs 4 echo ($x * $y); // outputs 60 echo ($x / $y); // outputs 1.6666666666667 echo ($x % $y); // outputs 4 ?>
  • 14. PHP String Operators <?php $a = "Hello"; $b = $a . " world!"; echo $b; // outputs Hello world! $x="Hello"; $x .= " world!"; echo $x; // outputs Hello world! ?>
  • 15. PHP Increment / Decrement Operators <?php $x=10; echo ++$x; // outputs 11 $y=10; echo $y++; // outputs 10 $z=5; echo --$z; // outputs 4 $i=5; echo $i--; // outputs 5 ?>
  • 16. PHP if...else...elseif Statements <?php $t=21; if ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
  • 17. PHP switch Statement <?php $favcolor="red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, or green!"; } ?>
  • 18. PHP while Loops In PHP, we have the following looping statements: while - loops through a block of code as long as the specified condition is true do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true
  • 19. The PHP while Loop <?php $x=1; while($x<=5) { echo "The number is: $x <br>"; $x++; } ?>
  • 20. The PHP do...while Loop <?php $x=1; do { echo "The number is: $x <br>"; $x++; } while ($x<=5) ?>
  • 21. PHP for Loops for (init counter; test counter; increment counter) { code to be executed; } Parameters: init counter: Initialize the loop counter value test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. increment counter: Increases the loop counter value
  • 22. Php for loops(cont'd) The PHP foreach Loop The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. <?php $colors = array("red","green","blue","yellow"); foreach ($colors as $value) { echo "$value <br>"; } ?>
  • 23. <?php PHP Functions function writeMsg() { echo "Hello world!"; } writeMsg(); // call the function ?> PHP Function Arguments <?php function familyName($fname) { echo "$fname Refsnes.<br>"; } familyName("Jani"); ?>
  • 24. PHP Arrays <?php $cars=array("Volvo","BMW","Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?> In PHP, there are three types of arrays: Indexed arrays - Arrays with numeric index Associative arrays - Arrays with named keys Multidimensional arrays - Arrays containing one or more arrays
  • 25. Array(cont'd) PHP Indexed Arrays There are two ways to create indexed arrays: 1.The index can be assigned automatically (index always starts at 0): $cars=array("Volvo","BMW","Toyota"); 2.The count() function is used to return the length (the number of elements) of an array:
  • 26. Array(cont'd) PHP Associative Arrays Associative arrays are arrays that use named keys that you assign to them. $age=array("Peter"=>"35","Ben"=>"37","Joe"=>" 43"); <?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>" 43"); echo "Peter is " . $age['Peter'] . " years old."; ?>
  • 27. PHP - Sort Functions For Arrays The following PHP array sort functions: sort() - sort arrays in ascending order rsort() - sort arrays in descending order asort() - sort associative arrays in ascending order, according to the value ksort() - sort associative arrays in ascending order, according to the key arsort() - sort associative arrays in descending order, according to the value krsort() - sort associative arrays in descending order, according to the key
  • 28. PHP Form Handling PHP - A Simple HTML Form <html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html> Php program <html> <body> Welcome <?php echo $_POST["name"]; ?><br> Your email address is: <?php echo $_POST["email"]; ?> </body> </html>