SlideShare une entreprise Scribd logo
PHP
PHP Introduction
• PHP code is executed on the server.
• PHP is an acronym for "PHP: Hypertext
Preprocessor"
• PHP is a widely-used, open source
scripting language
• PHP scripts are executed on the server
• PHP is free to download and use
What is a PHP File?
• PHP files can contain text, HTML, CSS,
JavaScript, and PHP code
• PHP code is 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, delete,
and close files on the server
• PHP can collect form data
• PHP can send and receive cookies
• PHP can add, delete, modify data in your
database
• PHP can be used to control user-access
• PHP can encrypt data
PHP
• Create a file named hello.php and save it in the root directory of your web
server with the following content.
• Now, enter your web server's URL in your browser, ending with /hello.php. As
you are using a web server installed on PC, the full URL might be
http://localhost/hello.php or http://127.0.0.1/hello.php.
PHP Tags
• Opening Tag
• Closing Tag
• When you request a PHP file, the server process the file using the PHP parser.
The parsed output will be sent as the response.
• When PHP parses a file, it searches for opening and closing tags.
• All the code inside these tags is interpreted. Everything outside these tags is
ignored by the PHP parser.
PHP Comments
• Single line comment
• Multiline comments
PHP Variables
• Variables are used to store information and retrieve them when needed.
• They can be labeled with a meaningful name to be understood easily by the
readers and ourselves.
❑ The $ identifier should be added in front of the name of the variable.
❑ A variable name must start with a letter or underscore.
❑ A variable name cannot start with a number.
❑ A variable name cannot be $this. ($this is a special variable which cannot be assigned)
❑ A variable name can contain letters, numbers, and characters between 127-255 ASCII
after the first character.
❑ Variable names are case sensitive.
PHP Variables
PHP Variables
• In programming, creating a variable is called declaring.
• But, in PHP, unlike other programming languages, there is no keyword to
declare a variable.
PHP Variables
❑ Strings should be wrapped with " (double-quotes) or '
(single-quotes)..
❑ The variable name must be on the left.
❑ The = sign as the assignment operator.
❑ Next, the value to be assigned.
❑ Finally, ; sign to say the statement is finished.
PHP Variables
• Strings should be wrapped with " (double-quotes) or '
(single-quotes)..
❑ The variable name must be on the left.
❑ The = sign as the assignment operator.
❑ Next, the value to be assigned.
❑ Finally, ; sign to say the statement is finished.
PHP Variables
• Example: Output is “World”
PHP Variable Scope
Global Scope
• A variable declared in the main flow of the code (not inside a function) has
a global scope. These variables can't be used inside functions.
PHP Variable Scope
Local Scope
• A variable declared inside a function has a local scope.
• This variable is unique to this function and can only be accessed within the
function.
• You can define different variables with the same name in different
functions.
PHP Variable Scope
Global Variables in Local Scope
• You can use global keyword inside a function to access global
variables.
• Before using variables, add global keyword followed by comma-
separated variable names you need to use inside the function.
PHP Variable Scope
Global Variables in Local Scope
• Local scope variables are deleted after the end of the execution
of the function. But, sometimes we need to keep the variable alive.
• To use this feature, add static keyword before the variable when
declaring it.
PHP Constants
• PHP does not have a keyword to declare a constant. We have to use
define() function for the purpose
❑ Once a constant is defined, it cannot be changed.
❑ All the constants have global scopes, even it is defined inside a function.
❑ Unlike variables, constants do not have $ before the name.
❑ A constant name should start with a letter or underscore.
❑ A constant name can have letters, numbers, ASCII characters between 127-255 after
the first letter..
PHP Constants
Constants
PHP Strings
• The easiest way to declare a string is by enclosing it by single quotes.
Character: ‘
• What if you needed to add a single quote inside a single quoted string?
Just escape that character with a back slash.
PHP Strings
Strings
• What if you needed to have  in a single quoted string?
• Use  to escape the  character.
PHP Strings
Double Quoted Strings
• Strings can be declared enclosed by double quotes. Character: “”
• In single quotes, only  and  ' had a special meaning. But, in double
quotes, there are more escape sequences.
PHP Output
PHP Output
• In PHP, there are two output statements. echo, print
PHP Data types
PHP Data types
There are several data types in PHP.
• Boolean
• Integer
• Float or Double
• String
• Array
• Object
• Null
PHP Data types
var_dump
❑ var_dump function dumps information about a variable.
❑ The importance of this function is, it outputs the data type of the variable
with its value. It is also a output function like echo()
PHP Data types
Php array
There are two ways to declare an array.
• An array can be declared using the array() function.
• An array can be declared wrapping with [ and ].
• Elements of the arrays should be separated with a comma. And, elements
can be any type
PHP Data types
Php null
• Null means no value.
• Null is not 0, false or any other value, it is null.
PHP Operators
Arithmetic Operators
PHP Operators
Assignment Operators
• The basic assignment operator in PHP is =. It is actually not the "equal" mark in PHP.
• It works as assignment operator, which assigns a value to a variable.
In this example,
• 5 is assigned to $a.
• 7 is assigned to $b.
• Then, the value of $b (7) is assigned to $a.
PHP Operators
Assignment Operators
PHP Operators
Comparison Operators
PHP Operators
Logical Operators
PHP Operators
String Operators
PHP Conditionals
Conditionals
In PHP, we have the following conditional statements:
• If Statement
• If-Else Statement
• If-Elseif-Else Statement
• Switch Statement
PHP Conditionals
If Statement
PHP Conditionals
If else
PHP Conditionals
If-Elseif-Else Statement
PHP Conditionals
Switch
PHP Loops
• Often when you write code, you want the same
block of code to run over and over again in a row.
• Instead of adding several almost equal lines in a
script, we can use loops to perform a task like this.
• In PHP, we have the following looping statements:
– while - loops through a block of code while a specified
condition is true
– do...while - loops through a block of code once, and
then repeats the loop as long as a specified condition
is true
– for - loops through a block of code a specified number
of times
– foreach - loops through a block of code for each
element in an array
PHP Functions
a. User defined Functions
PHP Functions
User defined Functions
Function Argument
The PHP Date()
• The PHP date() function is used to format a date
and/or a time.
• Here are some characters that are commonly used
for dates:
– d - Represents the day of the month (01 to 31)
– m - Represents a month (01 to 12)
– Y - Represents a year (in four digits)
– l (lowercase 'L') - Represents the day of the week
PHP Forms - $_GET
Function
• The PHP superglobals $_GET and $_POST
are used to collect form-data.
• The built-in $_GET function is used to
collect values from a form sent 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) and
has limits on the amount of information to
send (max. 100 characters).
PHP Forms - $_GET
Function
• When using method="get" in HTML forms, all
variable names and values are displayed in
the URL.
• This method should not be used when
sending passwords or other sensitive
information!
• However, because the variables are displayed
in the URL, it is possible to bookmark the
page. This can be useful in some cases.
• The get method is not suitable for large
variable values;
PHP Forms - $_POST
Function
• The built-in $_POST function is used to
collect values from a form sent 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.
PHP Forms - $_POST
Function
When to use 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.
• However, because the variables are not
displayed in the URL, it is not possible to
bookmark the page.
PHP Forms - $_POST
Function
Welcome.php
Thank you!
Any Questions?

Contenu connexe

Similaire à Introduction to PHP_Slides by Lesley_Bonyo.pdf

Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Muhamad Al Imran
 
PHPVariables_075026.ppt
PHPVariables_075026.pptPHPVariables_075026.ppt
PHPVariables_075026.ppt
06Vinit
 
PHP Variables and scopes
PHP Variables and scopesPHP Variables and scopes
PHP Variables and scopes
sana mateen
 
Php basics
Php basicsPhp basics
Php basics
Jamshid Hashimi
 
Php tutorial from_beginner_to_master
Php tutorial from_beginner_to_masterPhp tutorial from_beginner_to_master
Php tutorial from_beginner_to_master
PrinceGuru MS
 
Introduction to php contains basic....pptx
Introduction to php contains basic....pptxIntroduction to php contains basic....pptx
Introduction to php contains basic....pptx
RanjithaGowda63
 
php Chapter 1.pptx
php Chapter 1.pptxphp Chapter 1.pptx
php Chapter 1.pptx
HambaAbebe2
 
1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master
jeeva indra
 
php app development 1
php app development 1php app development 1
php app development 1
barryavery
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
Muthuganesh S
 
Chap 4 PHP.pdf
Chap 4 PHP.pdfChap 4 PHP.pdf
Chap 4 PHP.pdf
HASENSEID
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
pratik tambekar
 
LAB PHP Consolidated.ppt
LAB PHP Consolidated.pptLAB PHP Consolidated.ppt
LAB PHP Consolidated.ppt
YasirAhmad80
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
McSoftsis
 
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdfIT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
pkaviya
 
Php essentials
Php essentialsPhp essentials
Php essentials
sagaroceanic11
 
php basics
php basicsphp basics
php basics
Anmol Paul
 
Programming in PHP Course Material BCA 6th Semester
Programming in PHP Course Material BCA 6th SemesterProgramming in PHP Course Material BCA 6th Semester
Programming in PHP Course Material BCA 6th Semester
SanthiNivas
 
05php
05php05php
Basics PHP
Basics PHPBasics PHP

Similaire à Introduction to PHP_Slides by Lesley_Bonyo.pdf (20)

Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
 
PHPVariables_075026.ppt
PHPVariables_075026.pptPHPVariables_075026.ppt
PHPVariables_075026.ppt
 
PHP Variables and scopes
PHP Variables and scopesPHP Variables and scopes
PHP Variables and scopes
 
Php basics
Php basicsPhp basics
Php basics
 
Php tutorial from_beginner_to_master
Php tutorial from_beginner_to_masterPhp tutorial from_beginner_to_master
Php tutorial from_beginner_to_master
 
Introduction to php contains basic....pptx
Introduction to php contains basic....pptxIntroduction to php contains basic....pptx
Introduction to php contains basic....pptx
 
php Chapter 1.pptx
php Chapter 1.pptxphp Chapter 1.pptx
php Chapter 1.pptx
 
1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master
 
php app development 1
php app development 1php app development 1
php app development 1
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
Chap 4 PHP.pdf
Chap 4 PHP.pdfChap 4 PHP.pdf
Chap 4 PHP.pdf
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
LAB PHP Consolidated.ppt
LAB PHP Consolidated.pptLAB PHP Consolidated.ppt
LAB PHP Consolidated.ppt
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
 
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdfIT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
 
Php essentials
Php essentialsPhp essentials
Php essentials
 
php basics
php basicsphp basics
php basics
 
Programming in PHP Course Material BCA 6th Semester
Programming in PHP Course Material BCA 6th SemesterProgramming in PHP Course Material BCA 6th Semester
Programming in PHP Course Material BCA 6th Semester
 
05php
05php05php
05php
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 

Dernier

Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
David Brossard
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
Federico Razzoli
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 

Dernier (20)

Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 

Introduction to PHP_Slides by Lesley_Bonyo.pdf

  • 1. PHP
  • 2. PHP Introduction • PHP code is executed on the server. • PHP is an acronym for "PHP: Hypertext Preprocessor" • PHP is a widely-used, open source scripting language • PHP scripts are executed on the server • PHP is free to download and use
  • 3. What is a PHP File? • PHP files can contain text, HTML, CSS, JavaScript, and PHP code • PHP code is executed on the server, and the result is returned to the browser as plain HTML • PHP files have extension ".php"
  • 4. What Can PHP Do? • PHP can generate dynamic page content • PHP can create, open, read, write, delete, and close files on the server • PHP can collect form data • PHP can send and receive cookies • PHP can add, delete, modify data in your database • PHP can be used to control user-access • PHP can encrypt data
  • 5. PHP • Create a file named hello.php and save it in the root directory of your web server with the following content. • Now, enter your web server's URL in your browser, ending with /hello.php. As you are using a web server installed on PC, the full URL might be http://localhost/hello.php or http://127.0.0.1/hello.php.
  • 6. PHP Tags • Opening Tag • Closing Tag • When you request a PHP file, the server process the file using the PHP parser. The parsed output will be sent as the response. • When PHP parses a file, it searches for opening and closing tags. • All the code inside these tags is interpreted. Everything outside these tags is ignored by the PHP parser.
  • 7. PHP Comments • Single line comment • Multiline comments
  • 8. PHP Variables • Variables are used to store information and retrieve them when needed. • They can be labeled with a meaningful name to be understood easily by the readers and ourselves. ❑ The $ identifier should be added in front of the name of the variable. ❑ A variable name must start with a letter or underscore. ❑ A variable name cannot start with a number. ❑ A variable name cannot be $this. ($this is a special variable which cannot be assigned) ❑ A variable name can contain letters, numbers, and characters between 127-255 ASCII after the first character. ❑ Variable names are case sensitive.
  • 10. PHP Variables • In programming, creating a variable is called declaring. • But, in PHP, unlike other programming languages, there is no keyword to declare a variable.
  • 11. PHP Variables ❑ Strings should be wrapped with " (double-quotes) or ' (single-quotes).. ❑ The variable name must be on the left. ❑ The = sign as the assignment operator. ❑ Next, the value to be assigned. ❑ Finally, ; sign to say the statement is finished.
  • 12. PHP Variables • Strings should be wrapped with " (double-quotes) or ' (single-quotes).. ❑ The variable name must be on the left. ❑ The = sign as the assignment operator. ❑ Next, the value to be assigned. ❑ Finally, ; sign to say the statement is finished.
  • 13. PHP Variables • Example: Output is “World”
  • 14. PHP Variable Scope Global Scope • A variable declared in the main flow of the code (not inside a function) has a global scope. These variables can't be used inside functions.
  • 15. PHP Variable Scope Local Scope • A variable declared inside a function has a local scope. • This variable is unique to this function and can only be accessed within the function. • You can define different variables with the same name in different functions.
  • 16. PHP Variable Scope Global Variables in Local Scope • You can use global keyword inside a function to access global variables. • Before using variables, add global keyword followed by comma- separated variable names you need to use inside the function.
  • 17. PHP Variable Scope Global Variables in Local Scope • Local scope variables are deleted after the end of the execution of the function. But, sometimes we need to keep the variable alive. • To use this feature, add static keyword before the variable when declaring it.
  • 18. PHP Constants • PHP does not have a keyword to declare a constant. We have to use define() function for the purpose ❑ Once a constant is defined, it cannot be changed. ❑ All the constants have global scopes, even it is defined inside a function. ❑ Unlike variables, constants do not have $ before the name. ❑ A constant name should start with a letter or underscore. ❑ A constant name can have letters, numbers, ASCII characters between 127-255 after the first letter..
  • 20. PHP Strings • The easiest way to declare a string is by enclosing it by single quotes. Character: ‘ • What if you needed to add a single quote inside a single quoted string? Just escape that character with a back slash.
  • 21. PHP Strings Strings • What if you needed to have in a single quoted string? • Use to escape the character.
  • 22. PHP Strings Double Quoted Strings • Strings can be declared enclosed by double quotes. Character: “” • In single quotes, only and ' had a special meaning. But, in double quotes, there are more escape sequences.
  • 23. PHP Output PHP Output • In PHP, there are two output statements. echo, print
  • 24. PHP Data types PHP Data types There are several data types in PHP. • Boolean • Integer • Float or Double • String • Array • Object • Null
  • 25. PHP Data types var_dump ❑ var_dump function dumps information about a variable. ❑ The importance of this function is, it outputs the data type of the variable with its value. It is also a output function like echo()
  • 26. PHP Data types Php array There are two ways to declare an array. • An array can be declared using the array() function. • An array can be declared wrapping with [ and ]. • Elements of the arrays should be separated with a comma. And, elements can be any type
  • 27. PHP Data types Php null • Null means no value. • Null is not 0, false or any other value, it is null.
  • 29. PHP Operators Assignment Operators • The basic assignment operator in PHP is =. It is actually not the "equal" mark in PHP. • It works as assignment operator, which assigns a value to a variable. In this example, • 5 is assigned to $a. • 7 is assigned to $b. • Then, the value of $b (7) is assigned to $a.
  • 34. PHP Conditionals Conditionals In PHP, we have the following conditional statements: • If Statement • If-Else Statement • If-Elseif-Else Statement • Switch Statement
  • 39. PHP Loops • Often when you write code, you want the same block of code to run over and over again in a row. • Instead of adding several almost equal lines in a script, we can use loops to perform a task like this. • In PHP, we have the following looping statements: – while - loops through a block of code while a specified condition is true – do...while - loops through a block of code once, and then repeats the loop as long as a specified condition is true – for - loops through a block of code a specified number of times – foreach - loops through a block of code for each element in an array
  • 40. PHP Functions a. User defined Functions
  • 41. PHP Functions User defined Functions Function Argument
  • 42. The PHP Date() • The PHP date() function is used to format a date and/or a time. • Here are some characters that are commonly used for dates: – d - Represents the day of the month (01 to 31) – m - Represents a month (01 to 12) – Y - Represents a year (in four digits) – l (lowercase 'L') - Represents the day of the week
  • 43. PHP Forms - $_GET Function • The PHP superglobals $_GET and $_POST are used to collect form-data. • The built-in $_GET function is used to collect values from a form sent 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) and has limits on the amount of information to send (max. 100 characters).
  • 44. PHP Forms - $_GET Function • When using method="get" in HTML forms, all variable names and values are displayed in the URL. • This method should not be used when sending passwords or other sensitive information! • However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases. • The get method is not suitable for large variable values;
  • 45. PHP Forms - $_POST Function • The built-in $_POST function is used to collect values from a form sent 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.
  • 46. PHP Forms - $_POST Function When to use 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. • However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
  • 47. PHP Forms - $_POST Function Welcome.php