SlideShare a Scribd company logo
1 of 48
1
Basic PHP
2
Group Members:
• Dianna Marie Manalo
• Anjannette De Villa
• Apol Magbuhos
• Marco Paolo Aclan
• Lenie Aniel
• Kim Ralph Perez
• Glenn Perez
• Neil Ian Bagsic
• Christian Recto
• Deborah Ana Perez
3
Basic PHP Syntax
• A PHP scripting block always
– starts with <?php and
– ends with ?>
• Example
<?php
…….….
………..
?>
4
The PHP echo Statement
• echo is a language construct, and can be used
with or without parentheses: echo or echo().
Display Strings
• The following example shows how to display
different strings with the echo command (also
notice that the strings can contain HTML
markup):
<?php
echo "<h2>PHP is fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This", " string", " was", " made", " with
multiple parameters.";
?>
5
The PHP print Statements
• print is also a language construct, and can be
used with or without parentheses: print or print().
Display Strings
• The following example shows how to display
different strings with the print command (also
notice that the strings can contain HTML
markup):
<?php
print "<h2>PHP is fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
6
Combining HTML and PHP
<html>
<head>
<title>A PHP script including HTML</title>
</head>
<body>
<b>
"hello world- HTML";
<?php
echo "hello world-PHP";
?>
</b>
</body>
</html>
7
Comments in PHP
PHP supports two types of comments
• Single line comment
// This is a single line comment
# This is also a single line comment
Multiline comment
/* this is multiline comment
this is multiline comment
*/
8
Example
<html>
<body>
<?php
//This is a single-line comment
/*
This is
a comment
block
*/
?>
</body>
</html>
9
Variables in PHP
• Variables are used for storing a values,
like text strings, numbers or arrays.
• When a variable is set it can be used over
and over again in your script
• All variables in PHP start with a $ sign
symbol.
• The correct way of setting a variable in
PHP:
$var_name = value;
Eg: $name=“Sunil”;
10
Example
<?php
$txt = "Hello World!";
$number = 16;
echo $txt;
echo $number;
?>
11
Variable Naming Rules
• A variable name must start with a letter or an
underscore "_"
• A variable name can only contain alpha-numeric
characters and underscores (a-Z, 0-9, and _ )
• A variable name should not contain spaces. If a
variable name is more than one word, it should
be separated with underscore ($my_string), or
with capitalization ($myString)
12
PHP String
• String variables are used for values that
contains character strings.
• Example 1:
<?php
$txt="Hello World";
echo $txt;
?>
The output of the code above will be:
Hello World
13
PHP String
• Example 2:
<?php
$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2;
?>
The output of the code above will be:
Hello World 1234
14
The Strlen() Function
• The strlen() function is used to return the
length of a string.
Example
<?php
echo strlen("Hello world!");
?>
The output of the code above will be:
12
15
The Strpos() Function
• The strpos() function is used to search for
characters within the string.
Example
<?php
echo strpos("Hello world!","world");
?>
The output of the code above will be:
6
16
PHP Operators
• Arithmetic Operators
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (division remainder)
++Increment
--Decrement
17
PHP Operators
• Comparison Operators
== is equal to 5==8 returns false
!= is not equal5!=8 returns true
> is greater than 5>8 returns false
< is less than5<8 returns true
>= is greater than or equal to 5>=8 returns false
<= is less than or equal to 5<=8 returns true
18
Conditional Statements
If...Else Statement
• If you want to execute some code if a
condition is true and another code if a
condition is false, use the if....else
statement.
• Syntax
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
19
Conditional Statements
Example 1:
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
</body>
</html>
20
Conditional Statements
• If you want to execute some code if one of
several conditions are true use the elseif
statement
• Syntax
if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
21
Conditional Statements
Example :
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
</body>
</html>
22
PHP Switch Statement
• If you want to select one of many blocks of
code to be executed, use the Switch
statement.
• The switch statement is used to avoid long
blocks of if..elseif..else code.
23
PHP Switch Statement
• Syntax
switch (expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different from both label1 and label2;
}
24
PHP Switch Statement
Example:
<html>
<body>
<?php
$x=2;
switch ($x)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
</body>
</html>
25
PHP Arrays
• What is an array?
– When working with PHP, sooner or later, you
might want to create many similar variables.
– Instead of having many similar variables, you
can store the data as elements in an array.
– Each element in the array has its own ID so
that it can be easily accessed.
26
PHP Arrays : Example 1
<?php
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
echo $names[1] . " and " . $names[2] .
" are ". $names[0] . "'s neighbors";
?>
The code above will output:
Quagmire and Joe are Peter's neighbors
27
PHP Arrays : Example 2
<?php
$names = array("Peter","Quagmire","Joe");
echo $names[1] . " and " . $names[2] .
" are ". $names[0] . "'s neighbors";
?>
The code above will output:
Quagmire and Joe are Peter's neighbors
28
Multidimensional Arrays
• In a multidimensional array,
– each element in the main array can also be
an array.
– each element in the sub-array can be an
array, and so on
29
Multidimensional Arrays
• In a multidimensional array,
– each element in the main array can also be
an array.
– each element in the sub-array can be an
array, and so on
30
PHP Looping
• Looping statements in PHP are
used to execute the same block of
code a specified number of times.
31
PHP Looping
• Looping statements in PHP are used to
execute the same block of code a
specified number of times.
• In PHP we have the following looping
statements:
– while - loops
– do...while - loops
– for - loops
– foreach - loops
32
The while Statement
• The while statement will execute a block
of code if and as long as a condition is
true.
• Syntax
while (condition)
{
statement 1;
statement 2;
}
33
The while Statement
Example
.
.
.
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
.
.
.
34
do...while Statement
• The do...while statement will execute a block of
code at least once - it then will repeat the loop
as long as a condition is true.
• Syntax
do
{
code to be executed;
}
while (condition);
35
do...while Statement
• Example
.
.
$i=0;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<5);
.
.
36
The for Statement
• The for statement is used when you know
how many times you want to execute a
statement or a list of statements.
• Syntax
for (initialization; condition; increment)
{
code to be executed;
}
37
The for Statement
• Example
.
.
for ($i=1; $i<=5; $i++)
{
echo "Hello World!<br />";
}
.
.
38
PHP Forms
• The PHP $_GET and $_POST variables
are used to retrieve information from
forms, like user input.
39
Example
• Welcome.htm
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
40
Example
• welcome.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
41
PHP $_GET
• The $_GET variable is used to collect
values from a form with method="get".
• Information sent from a form with the GET
method is visible to everyone (it will be
displayed in the browser's address bar)
• It has limits on the amount of information
to send (max. 100 characters).
42
Example
<form action="welcome.php" method="get">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
• Welcome.php
Welcome <?php echo $_GET["name"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!
43
Note:
• When using the $_GET variable all
variable names and values are displayed
in the URL.
• So this method should not be used when
sending passwords or other sensitive
information!
• The HTTP GET method is not suitable on
large variable values; the value cannot
exceed 100 characters.
44
PHP $_POST
• The $_POST variable is used to collect
values from a form with method="post".
• Information sent from a form with the
POST method is invisible to others and
has no limits on the amount of information
to send.
45
Example
<form action="welcome.php" method="post">
Enter your name: <input type="text" name="name" />
Enter your age: <input type="text" name="age" />
<input type="submit" />
</form>
• Welcome.php
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old!
46
Create Database snd Tables phpMyAdmin
• Start by logging onto phpMyAdmin
• When you are logged on, simply type a name for
the database and press the button "Create".
• To create the table write this code in SQL.
Create table info (
id integer not null primary key
auto_increment,
Firstname varchar (25) not null,
Lastname varchar (25) not null,
Phonenumber int (25) not null
);
47
Connecting php to Database
• Config.php
<?php
$hostname='localhost';
$username='root';
$password='';
$database='example';
$conn=mysql_connect($hostname,$username,
$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
?>
48
Connecting php to Database(cont.)
• Register.php
<?php
include 'config.php';
if (isset($_POST['submit'])){
$fname = $_POST['Firstname'];
$lname = $_POST['Lastname'];
$mobile = $_POST['Phonenumber'];
$sql="Insert into
info(Firstname,Lastname,Phonenumber)values('$fname','$lname','$mobile')";
$result=mysql_query($sql);
echo 'Succesfully Added' ;
}
?>
<form name="register" method="post">
<label> First Name </label>
<input type="text" name="Firstname" required>
<br>
<label>Lastname</label>
<input type="text" name="Lastname" required>
<br>
<label> Phone Number </label>
<input type="text" name="Phonenumber" required>
<br>
<input type="submit" name="submit">

More Related Content

What's hot

What's hot (20)

PHP Tutorials
PHP TutorialsPHP Tutorials
PHP Tutorials
 
PHP
PHPPHP
PHP
 
php
phpphp
php
 
Introduction To Web Technology
Introduction To Web TechnologyIntroduction To Web Technology
Introduction To Web Technology
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
Php
PhpPhp
Php
 
Php Lecture Notes
Php Lecture NotesPhp Lecture Notes
Php Lecture Notes
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
PHP-MySQL Database Connectivity Using XAMPP Server
PHP-MySQL Database Connectivity Using XAMPP ServerPHP-MySQL Database Connectivity Using XAMPP Server
PHP-MySQL Database Connectivity Using XAMPP Server
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
PHP
PHPPHP
PHP
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Apache HBase™
Apache HBase™Apache HBase™
Apache HBase™
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 
Http
HttpHttp
Http
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
Flexbox
FlexboxFlexbox
Flexbox
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commands
 

Similar to Php(report)

Lecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdfLecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdf
ShaimaaMohamedGalal
 

Similar to Php(report) (20)

Intro to php
Intro to phpIntro to php
Intro to php
 
PHP - Web Development
PHP - Web DevelopmentPHP - Web Development
PHP - Web Development
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginnersPHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
 
PHP
PHPPHP
PHP
 
Php Basics
Php BasicsPhp Basics
Php Basics
 
php Chapter 1.pptx
php Chapter 1.pptxphp Chapter 1.pptx
php Chapter 1.pptx
 
php basics
php basicsphp basics
php basics
 
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
 
Hsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdfHsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdf
 
Lecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdfLecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdf
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
 
Materi Dasar PHP
Materi Dasar PHPMateri Dasar PHP
Materi Dasar PHP
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Php
PhpPhp
Php
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
 
PHP Basics and Demo HackU
PHP Basics and Demo HackUPHP Basics and Demo HackU
PHP Basics and Demo HackU
 
Free PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in IndiaFree PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in India
 
PHP MATERIAL
PHP MATERIALPHP MATERIAL
PHP MATERIAL
 
What Is Php
What Is PhpWhat Is Php
What Is Php
 
Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Php(report)

  • 2. 2 Group Members: • Dianna Marie Manalo • Anjannette De Villa • Apol Magbuhos • Marco Paolo Aclan • Lenie Aniel • Kim Ralph Perez • Glenn Perez • Neil Ian Bagsic • Christian Recto • Deborah Ana Perez
  • 3. 3 Basic PHP Syntax • A PHP scripting block always – starts with <?php and – ends with ?> • Example <?php …….…. ……….. ?>
  • 4. 4 The PHP echo Statement • echo is a language construct, and can be used with or without parentheses: echo or echo(). Display Strings • The following example shows how to display different strings with the echo command (also notice that the strings can contain HTML markup): <?php echo "<h2>PHP is fun!</h2>"; echo "Hello world!<br>"; echo "I'm about to learn PHP!<br>"; echo "This", " string", " was", " made", " with multiple parameters."; ?>
  • 5. 5 The PHP print Statements • print is also a language construct, and can be used with or without parentheses: print or print(). Display Strings • The following example shows how to display different strings with the print command (also notice that the strings can contain HTML markup): <?php print "<h2>PHP is fun!</h2>"; print "Hello world!<br>"; print "I'm about to learn PHP!"; ?>
  • 6. 6 Combining HTML and PHP <html> <head> <title>A PHP script including HTML</title> </head> <body> <b> "hello world- HTML"; <?php echo "hello world-PHP"; ?> </b> </body> </html>
  • 7. 7 Comments in PHP PHP supports two types of comments • Single line comment // This is a single line comment # This is also a single line comment Multiline comment /* this is multiline comment this is multiline comment */
  • 8. 8 Example <html> <body> <?php //This is a single-line comment /* This is a comment block */ ?> </body> </html>
  • 9. 9 Variables in PHP • Variables are used for storing a values, like text strings, numbers or arrays. • When a variable is set it can be used over and over again in your script • All variables in PHP start with a $ sign symbol. • The correct way of setting a variable in PHP: $var_name = value; Eg: $name=“Sunil”;
  • 10. 10 Example <?php $txt = "Hello World!"; $number = 16; echo $txt; echo $number; ?>
  • 11. 11 Variable Naming Rules • A variable name must start with a letter or an underscore "_" • A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ ) • A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)
  • 12. 12 PHP String • String variables are used for values that contains character strings. • Example 1: <?php $txt="Hello World"; echo $txt; ?> The output of the code above will be: Hello World
  • 13. 13 PHP String • Example 2: <?php $txt1="Hello World"; $txt2="1234"; echo $txt1 . " " . $txt2; ?> The output of the code above will be: Hello World 1234
  • 14. 14 The Strlen() Function • The strlen() function is used to return the length of a string. Example <?php echo strlen("Hello world!"); ?> The output of the code above will be: 12
  • 15. 15 The Strpos() Function • The strpos() function is used to search for characters within the string. Example <?php echo strpos("Hello world!","world"); ?> The output of the code above will be: 6
  • 16. 16 PHP Operators • Arithmetic Operators + Addition - Subtraction * Multiplication / Division % Modulus (division remainder) ++Increment --Decrement
  • 17. 17 PHP Operators • Comparison Operators == is equal to 5==8 returns false != is not equal5!=8 returns true > is greater than 5>8 returns false < is less than5<8 returns true >= is greater than or equal to 5>=8 returns false <= is less than or equal to 5<=8 returns true
  • 18. 18 Conditional Statements If...Else Statement • If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement. • Syntax if (condition) code to be executed if condition is true; else code to be executed if condition is false;
  • 19. 19 Conditional Statements Example 1: <html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!"; ?> </body> </html>
  • 20. 20 Conditional Statements • If you want to execute some code if one of several conditions are true use the elseif statement • Syntax if (condition) code to be executed if condition is true; elseif (condition) code to be executed if condition is true; else code to be executed if condition is false;
  • 21. 21 Conditional Statements Example : <html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; elseif ($d=="Sun") echo "Have a nice Sunday!"; else echo "Have a nice day!"; ?> </body> </html>
  • 22. 22 PHP Switch Statement • If you want to select one of many blocks of code to be executed, use the Switch statement. • The switch statement is used to avoid long blocks of if..elseif..else code.
  • 23. 23 PHP Switch Statement • Syntax switch (expression) { case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }
  • 24. 24 PHP Switch Statement Example: <html> <body> <?php $x=2; switch ($x) { case 1: echo "Number 1"; break; case 2: echo "Number 2"; break; case 3: echo "Number 3"; break; default: echo "No number between 1 and 3"; } ?> </body> </html>
  • 25. 25 PHP Arrays • What is an array? – When working with PHP, sooner or later, you might want to create many similar variables. – Instead of having many similar variables, you can store the data as elements in an array. – Each element in the array has its own ID so that it can be easily accessed.
  • 26. 26 PHP Arrays : Example 1 <?php $names[0] = "Peter"; $names[1] = "Quagmire"; $names[2] = "Joe"; echo $names[1] . " and " . $names[2] . " are ". $names[0] . "'s neighbors"; ?> The code above will output: Quagmire and Joe are Peter's neighbors
  • 27. 27 PHP Arrays : Example 2 <?php $names = array("Peter","Quagmire","Joe"); echo $names[1] . " and " . $names[2] . " are ". $names[0] . "'s neighbors"; ?> The code above will output: Quagmire and Joe are Peter's neighbors
  • 28. 28 Multidimensional Arrays • In a multidimensional array, – each element in the main array can also be an array. – each element in the sub-array can be an array, and so on
  • 29. 29 Multidimensional Arrays • In a multidimensional array, – each element in the main array can also be an array. – each element in the sub-array can be an array, and so on
  • 30. 30 PHP Looping • Looping statements in PHP are used to execute the same block of code a specified number of times.
  • 31. 31 PHP Looping • Looping statements in PHP are used to execute the same block of code a specified number of times. • In PHP we have the following looping statements: – while - loops – do...while - loops – for - loops – foreach - loops
  • 32. 32 The while Statement • The while statement will execute a block of code if and as long as a condition is true. • Syntax while (condition) { statement 1; statement 2; }
  • 33. 33 The while Statement Example . . . $i=1; while($i<=5) { echo "The number is " . $i . "<br />"; $i++; } . . .
  • 34. 34 do...while Statement • The do...while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true. • Syntax do { code to be executed; } while (condition);
  • 35. 35 do...while Statement • Example . . $i=0; do { $i++; echo "The number is " . $i . "<br />"; } while ($i<5); . .
  • 36. 36 The for Statement • The for statement is used when you know how many times you want to execute a statement or a list of statements. • Syntax for (initialization; condition; increment) { code to be executed; }
  • 37. 37 The for Statement • Example . . for ($i=1; $i<=5; $i++) { echo "Hello World!<br />"; } . .
  • 38. 38 PHP Forms • The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.
  • 39. 39 Example • Welcome.htm <html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html>
  • 40. 40 Example • welcome.php <html> <body> Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?> years old. </body> </html>
  • 41. 41 PHP $_GET • The $_GET variable is used to collect values from a form with method="get". • Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) • It has limits on the amount of information to send (max. 100 characters).
  • 42. 42 Example <form action="welcome.php" method="get"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> • Welcome.php Welcome <?php echo $_GET["name"]; ?>.<br /> You are <?php echo $_GET["age"]; ?> years old!
  • 43. 43 Note: • When using the $_GET variable all variable names and values are displayed in the URL. • So this method should not be used when sending passwords or other sensitive information! • The HTTP GET method is not suitable on large variable values; the value cannot exceed 100 characters.
  • 44. 44 PHP $_POST • The $_POST variable is used to collect values from a form with method="post". • Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
  • 45. 45 Example <form action="welcome.php" method="post"> Enter your name: <input type="text" name="name" /> Enter your age: <input type="text" name="age" /> <input type="submit" /> </form> • Welcome.php Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?> years old!
  • 46. 46 Create Database snd Tables phpMyAdmin • Start by logging onto phpMyAdmin • When you are logged on, simply type a name for the database and press the button "Create". • To create the table write this code in SQL. Create table info ( id integer not null primary key auto_increment, Firstname varchar (25) not null, Lastname varchar (25) not null, Phonenumber int (25) not null );
  • 47. 47 Connecting php to Database • Config.php <?php $hostname='localhost'; $username='root'; $password=''; $database='example'; $conn=mysql_connect($hostname,$username, $password) or die(mysql_error()); mysql_select_db($database) or die(mysql_error()); ?>
  • 48. 48 Connecting php to Database(cont.) • Register.php <?php include 'config.php'; if (isset($_POST['submit'])){ $fname = $_POST['Firstname']; $lname = $_POST['Lastname']; $mobile = $_POST['Phonenumber']; $sql="Insert into info(Firstname,Lastname,Phonenumber)values('$fname','$lname','$mobile')"; $result=mysql_query($sql); echo 'Succesfully Added' ; } ?> <form name="register" method="post"> <label> First Name </label> <input type="text" name="Firstname" required> <br> <label>Lastname</label> <input type="text" name="Lastname" required> <br> <label> Phone Number </label> <input type="text" name="Phonenumber" required> <br> <input type="submit" name="submit">