SlideShare a Scribd company logo
PHP - INTRODUCTION
Introduction
• 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
• 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"
Introduction(Contd.,)
• 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
• With PHP you are not limited to output HTML.
• You can output images or PDF files. You can also output
any text, such as XHTML and XML.
Why?
• 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
What’s New in PHP?
• PHP 7 is much faster than the previous
popular stable release (PHP 5.6)
• PHP 7 has improved Error Handling
• PHP 7 supports stricter Type Declarations for
function arguments
• PHP 7 supports new operators (like the
spaceship operator: <=>)
Set Up PHP on Your Own PC
• However, if your server does not support PHP,
you must:
• install a web server
• install PHP
• install a database, such as MySQL
• The official PHP website (PHP.net) has installation
instructions for
PHP: http://php.net/manual/en/install.php
•
Basic PHP Syntax
• A PHP script is executed on the server, and the
plain HTML result is sent back to the browser.
• <?php
// PHP code goes here
?>
• The default file extension for PHP files is
".php".
• A PHP file normally contains HTML tags, and
some PHP scripting code.
Sample PHP file
• <!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
PHP Case Sensitivity
• In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions,
and user-defined functions are not case-sensitive.
• In the example below, all three echo statements below are equal
and legal:
• <!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
• Look at the example below; only the first statement
will display the value of the $color variable! This is
because $color, $COLOR, and $coLOR are treated as
three different variables:
• <!DOCTYPE html>
<html>
<body>
<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html>
Comments in PHP
• <!DOCTYPE html>
<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 multiple
lines
*/
?>
</body>
</html>
Creating (Declaring) PHP Variables
• In PHP, a variable starts with the $ sign,
followed by the name of the variable:
• <?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
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 alpha-numeric
characters and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive
($age and $AGE are two different variables)
Example
• <?php
$txt = “Universe";
echo "I love $txt!";
?>
• <?php
$txt = “Universe";
echo "I love " . $txt . "!";
?>
PHP is a Loosely Typed Language
• In the example above, notice that we did not
have to tell PHP which data type the variable is.
• PHP automatically associates a data type to the
variable, depending on its value. Since the data
types are not set in a strict sense, you can do
things like adding a string to an integer without
causing an error.
• In PHP 7, type declarations were added. This
gives an option to specify the data type expected
when declaring a function, and by enabling the
strict requirement, it will throw a "Fatal Error" on
a type mismatch.
PHP Variables Scope
• The scope of a variable is the part of the script
where the variable can be referenced/used.
• PHP has three different variable scopes:
• local
• global
• static
Global and Local Scope
• A variable declared outside a function has a
GLOBAL SCOPE and can only be accessed outside
a function:
• <?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an
error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
Output:
Variable x inside function is:
Variable x outside function is: 5
Variable with local scope:
• <?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an
error
echo "<p>Variable x outside function is: $x</p>";
?>
Output:
Variable x inside function is: 5
Variable x outside function is:
PHP echo and print Statements
• echo and print are more or less the same. They
are both used to output data to the screen.
• The differences are small: echo has no return
value while print has a return value of 1 so it can
be used in expressions.
• echo can take multiple parameters (although
such usage is rare) while print can take one
argument.
• echo is marginally faster than print.
Examples
<!DOCTYPE html>
<html>
<body>
<?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.";
?>
</body>
</html>
Output:
PHP is Fun!
Hello world!
I'm about to learn PHP!
This string was made with multiple
parameters.
Examples
<?php
$txt1 = "Learn PHP";
$txt2 = “Its easy to learn";
$x = 5;
$y = 4;
echo "<h2>" . $txt1 . "</h2>";
echo "Study PHP at " . $txt2 . "<br>";
echo $x + $y;
?>
Output:
Study PHP at Its easy to learn
9
Display Variables
<?php
$txt1 = "Learn PHP";
$txt2 = “All";
$x = 5;
$y = 4;
print "<h2>" . $txt1 . "</h2>";
print "Study PHP at " . $txt2 . "<br>";
print $x + $y;
?>
Output:
Study PHP at All
9
PHP Data Types
PHP supports the following data types:
• String
• Integer
• Float (floating point numbers - also called double)
• Boolean
• Array
• Object
• NULL
• Resource
PHP String
• A string is a sequence of characters, like "Hello
world!".
• A string can be any text inside quotes. You can
use single or double quotes:
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>
PHP Integer
• 2, 256, -256, 10358, -179567 are all integers.
• An integer is a number without any decimal part.
• An integer data type is a non-decimal number
between -2147483648 and 2147483647 in 32 bit
systems, and between -9223372036854775808
and 9223372036854775807 in 64 bit systems.
• A value greater (or lower) than this, will be stored
as float, because it exceeds the limit of an integer.
• Note: Another important thing to know is that
even if 4 * 2.5 is 10, the result is stored as float,
because one of the operands is a float (2.5).
• Here are some rules for integers:
• An integer must have at least one digit
• 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 has the following predefined constants
for integers:
• PHP_INT_MAX - The largest integer supported
• PHP_INT_MIN - The smallest integer
supported
• PHP_INT_SIZE - The size of an integer in bytes
• PHP has the following functions to check if the
type of a variable is integer:
• is_int()
• is_integer() - alias of is_int()
• is_long() - alias of is_int()
Example
<!DOCTYPE html>
<html>
<body>
<?php
// Check if the type of a variable is integer
$x = 5985;
var_dump(is_int($x));
echo "<br>";
// Check again...
$x = 59.85;
var_dump(is_int($x));
?>
</body>
</html>
Output:
bool(true)
bool(false)

More Related Content

Similar to INTRODUCTION ON PHP - SERVER SIDE SCRIPTING LANGUAGE (20)

PHP
PHPPHP
PHP
sometech
 
introduction to php and its uses in daily
introduction to php and its uses in dailyintroduction to php and its uses in daily
introduction to php and its uses in daily
vishal choudhary
 
PHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERSPHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERS
Aminiel Michael
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
Gheyath M. Othman
 
PHP Lecture 01 .pptx PHP Lecture 01 pptx
PHP Lecture 01 .pptx PHP Lecture 01 pptxPHP Lecture 01 .pptx PHP Lecture 01 pptx
PHP Lecture 01 .pptx PHP Lecture 01 pptx
shahgohar1
 
WT_PHP_PART1.pdf
WT_PHP_PART1.pdfWT_PHP_PART1.pdf
WT_PHP_PART1.pdf
HambardeAtharva
 
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
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
php basic part one
php basic part onephp basic part one
php basic part one
jeweltutin
 
Php
PhpPhp
Php
Ajaigururaj R
 
The basics of php for engeneering students
The basics of php for engeneering studentsThe basics of php for engeneering students
The basics of php for engeneering students
rahuljustin77
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHP
wahidullah mudaser
 
Introduction of PHP.pdf
Introduction of PHP.pdfIntroduction of PHP.pdf
Introduction of PHP.pdf
karinaabyys
 
Php mysql
Php mysqlPhp mysql
Php mysql
Shehrevar Davierwala
 
basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)
Guni Sonow
 
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...
anshkhurana01
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
Nidhi mishra
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
Bhanuka Uyanage
 
PHP - Introduction to PHP Fundamentals
PHP -  Introduction to PHP FundamentalsPHP -  Introduction to PHP Fundamentals
PHP - Introduction to PHP Fundamentals
Vibrant Technologies & Computers
 
Prersentation
PrersentationPrersentation
Prersentation
Ashwin Deora
 
Ch1(introduction to php)
Ch1(introduction to php)Ch1(introduction to php)
Ch1(introduction to php)
Chhom Karath
 
introduction to php and its uses in daily
introduction to php and its uses in dailyintroduction to php and its uses in daily
introduction to php and its uses in daily
vishal choudhary
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
Gheyath M. Othman
 
PHP Lecture 01 .pptx PHP Lecture 01 pptx
PHP Lecture 01 .pptx PHP Lecture 01 pptxPHP Lecture 01 .pptx PHP Lecture 01 pptx
PHP Lecture 01 .pptx PHP Lecture 01 pptx
shahgohar1
 
php basic part one
php basic part onephp basic part one
php basic part one
jeweltutin
 
The basics of php for engeneering students
The basics of php for engeneering studentsThe basics of php for engeneering students
The basics of php for engeneering students
rahuljustin77
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHP
wahidullah mudaser
 
Introduction of PHP.pdf
Introduction of PHP.pdfIntroduction of PHP.pdf
Introduction of PHP.pdf
karinaabyys
 
basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)
Guni Sonow
 
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...
anshkhurana01
 
Ch1(introduction to php)
Ch1(introduction to php)Ch1(introduction to php)
Ch1(introduction to php)
Chhom Karath
 

Recently uploaded (20)

Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)
pelumiadigun2006
 
The first edition of the AIAG-VDA FMEA.pptx
The first edition of the AIAG-VDA FMEA.pptxThe first edition of the AIAG-VDA FMEA.pptx
The first edition of the AIAG-VDA FMEA.pptx
Mayank Mathur
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
Software Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance OptimizationSoftware Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance Optimization
kiwoong (daniel) kim
 
First Review PPT gfinal gyft ftu liu yrfut go
First Review PPT gfinal gyft  ftu liu yrfut goFirst Review PPT gfinal gyft  ftu liu yrfut go
First Review PPT gfinal gyft ftu liu yrfut go
Sowndarya6
 
22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank
Guru Nanak Technical Institutions
 
MODULE 6 - 1 VAE - Variational Autoencoder
MODULE 6 - 1 VAE - Variational AutoencoderMODULE 6 - 1 VAE - Variational Autoencoder
MODULE 6 - 1 VAE - Variational Autoencoder
DivyaMeenaS
 
FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.
maldonadocesarmanuel
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought                           .社内勉強会資料_Chain of Thought                           .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
Transformimet e sinjaleve numerike duke perdorur transformimet
Transformimet  e sinjaleve numerike duke perdorur transformimetTransformimet  e sinjaleve numerike duke perdorur transformimet
Transformimet e sinjaleve numerike duke perdorur transformimet
IndritEnesi1
 
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
ijscai
 
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
BeHappy728244
 
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.pptfy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
sukarnoamin
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
New Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docxNew Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docx
misheetasah
 
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
ijccmsjournal
 
Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)
pelumiadigun2006
 
The first edition of the AIAG-VDA FMEA.pptx
The first edition of the AIAG-VDA FMEA.pptxThe first edition of the AIAG-VDA FMEA.pptx
The first edition of the AIAG-VDA FMEA.pptx
Mayank Mathur
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
Software Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance OptimizationSoftware Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance Optimization
kiwoong (daniel) kim
 
First Review PPT gfinal gyft ftu liu yrfut go
First Review PPT gfinal gyft  ftu liu yrfut goFirst Review PPT gfinal gyft  ftu liu yrfut go
First Review PPT gfinal gyft ftu liu yrfut go
Sowndarya6
 
MODULE 6 - 1 VAE - Variational Autoencoder
MODULE 6 - 1 VAE - Variational AutoencoderMODULE 6 - 1 VAE - Variational Autoencoder
MODULE 6 - 1 VAE - Variational Autoencoder
DivyaMeenaS
 
FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.
maldonadocesarmanuel
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought                           .社内勉強会資料_Chain of Thought                           .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
Transformimet e sinjaleve numerike duke perdorur transformimet
Transformimet  e sinjaleve numerike duke perdorur transformimetTransformimet  e sinjaleve numerike duke perdorur transformimet
Transformimet e sinjaleve numerike duke perdorur transformimet
IndritEnesi1
 
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
ijscai
 
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
BeHappy728244
 
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.pptfy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
sukarnoamin
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
New Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docxNew Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docx
misheetasah
 
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
ijccmsjournal
 
Ad

INTRODUCTION ON PHP - SERVER SIDE SCRIPTING LANGUAGE

  • 2. Introduction • 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 • 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"
  • 3. Introduction(Contd.,) • 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 • With PHP you are not limited to output HTML. • You can output images or PDF files. You can also output any text, such as XHTML and XML.
  • 4. Why? • 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
  • 5. What’s New in PHP? • PHP 7 is much faster than the previous popular stable release (PHP 5.6) • PHP 7 has improved Error Handling • PHP 7 supports stricter Type Declarations for function arguments • PHP 7 supports new operators (like the spaceship operator: <=>)
  • 6. Set Up PHP on Your Own PC • However, if your server does not support PHP, you must: • install a web server • install PHP • install a database, such as MySQL • The official PHP website (PHP.net) has installation instructions for PHP: http://php.net/manual/en/install.php •
  • 7. Basic PHP Syntax • A PHP script is executed on the server, and the plain HTML result is sent back to the browser. • <?php // PHP code goes here ?> • The default file extension for PHP files is ".php". • A PHP file normally contains HTML tags, and some PHP scripting code.
  • 8. Sample PHP file • <!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php echo "Hello World!"; ?> </body> </html>
  • 9. PHP Case Sensitivity • In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive. • In the example below, all three echo statements below are equal and legal: • <!DOCTYPE html> <html> <body> <?php ECHO "Hello World!<br>"; echo "Hello World!<br>"; EcHo "Hello World!<br>"; ?> </body> </html>
  • 10. • Look at the example below; only the first statement will display the value of the $color variable! This is because $color, $COLOR, and $coLOR are treated as three different variables: • <!DOCTYPE html> <html> <body> <?php $color = "red"; echo "My car is " . $color . "<br>"; echo "My house is " . $COLOR . "<br>"; echo "My boat is " . $coLOR . "<br>"; ?> </body> </html>
  • 11. Comments in PHP • <!DOCTYPE html> <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 multiple lines */ ?> </body> </html>
  • 12. Creating (Declaring) PHP Variables • In PHP, a variable starts with the $ sign, followed by the name of the variable: • <?php $txt = "Hello world!"; $x = 5; $y = 10.5; ?>
  • 13. 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 alpha-numeric characters and underscores (A-z, 0-9, and _ ) • Variable names are case-sensitive ($age and $AGE are two different variables)
  • 14. Example • <?php $txt = “Universe"; echo "I love $txt!"; ?> • <?php $txt = “Universe"; echo "I love " . $txt . "!"; ?>
  • 15. PHP is a Loosely Typed Language • In the example above, notice that we did not have to tell PHP which data type the variable is. • PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error. • In PHP 7, type declarations were added. This gives an option to specify the data type expected when declaring a function, and by enabling the strict requirement, it will throw a "Fatal Error" on a type mismatch.
  • 16. PHP Variables Scope • The scope of a variable is the part of the script where the variable can be referenced/used. • PHP has three different variable scopes: • local • global • static
  • 17. Global and Local Scope • A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function: • <?php $x = 5; // global scope function myTest() { // using x inside this function will generate an error echo "<p>Variable x inside function is: $x</p>"; } myTest(); echo "<p>Variable x outside function is: $x</p>"; ?> Output: Variable x inside function is: Variable x outside function is: 5
  • 18. Variable with local scope: • <?php function myTest() { $x = 5; // local scope echo "<p>Variable x inside function is: $x</p>"; } myTest(); // using x outside the function will generate an error echo "<p>Variable x outside function is: $x</p>"; ?> Output: Variable x inside function is: 5 Variable x outside function is:
  • 19. PHP echo and print Statements • echo and print are more or less the same. They are both used to output data to the screen. • The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. • echo can take multiple parameters (although such usage is rare) while print can take one argument. • echo is marginally faster than print.
  • 20. Examples <!DOCTYPE html> <html> <body> <?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."; ?> </body> </html> Output: PHP is Fun! Hello world! I'm about to learn PHP! This string was made with multiple parameters.
  • 21. Examples <?php $txt1 = "Learn PHP"; $txt2 = “Its easy to learn"; $x = 5; $y = 4; echo "<h2>" . $txt1 . "</h2>"; echo "Study PHP at " . $txt2 . "<br>"; echo $x + $y; ?> Output: Study PHP at Its easy to learn 9
  • 22. Display Variables <?php $txt1 = "Learn PHP"; $txt2 = “All"; $x = 5; $y = 4; print "<h2>" . $txt1 . "</h2>"; print "Study PHP at " . $txt2 . "<br>"; print $x + $y; ?> Output: Study PHP at All 9
  • 23. PHP Data Types PHP supports the following data types: • String • Integer • Float (floating point numbers - also called double) • Boolean • Array • Object • NULL • Resource
  • 24. PHP String • A string is a sequence of characters, like "Hello world!". • A string can be any text inside quotes. You can use single or double quotes: <?php $x = "Hello world!"; $y = 'Hello world!'; echo $x; echo "<br>"; echo $y; ?>
  • 25. PHP Integer • 2, 256, -256, 10358, -179567 are all integers. • An integer is a number without any decimal part. • An integer data type is a non-decimal number between -2147483648 and 2147483647 in 32 bit systems, and between -9223372036854775808 and 9223372036854775807 in 64 bit systems. • A value greater (or lower) than this, will be stored as float, because it exceeds the limit of an integer. • Note: Another important thing to know is that even if 4 * 2.5 is 10, the result is stored as float, because one of the operands is a float (2.5).
  • 26. • Here are some rules for integers: • An integer must have at least one digit • 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)
  • 27. • PHP has the following predefined constants for integers: • PHP_INT_MAX - The largest integer supported • PHP_INT_MIN - The smallest integer supported • PHP_INT_SIZE - The size of an integer in bytes
  • 28. • PHP has the following functions to check if the type of a variable is integer: • is_int() • is_integer() - alias of is_int() • is_long() - alias of is_int()
  • 29. Example <!DOCTYPE html> <html> <body> <?php // Check if the type of a variable is integer $x = 5985; var_dump(is_int($x)); echo "<br>"; // Check again... $x = 59.85; var_dump(is_int($x)); ?> </body> </html> Output: bool(true) bool(false)