SlideShare a Scribd company logo
1 of 26
What is PHP?
• PHP is an acronym for "PHP: Hypertext Preprocessor"
• PHP is a widely-used, open source scripting language
• PHP scripts are executed on the server, and the result is
returned to the browser as plain HTML
• PHP is free to download and use
• PHP is used in WordPress and Facebook
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
BASIC PHP SYNTAX
• PHP scripting block starts with <?php and end with ?>
• Each code line in PHP must end with a semicolon. The semicolon is a
separator and is used to distinguish one set of instructions from another
• There are two basic statements to output text with PHP: echo and
print.
• PHP files have a file extension of ".php", ".php3", or ".phtml" .The
default extension is “.php”
• In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and
user-defined functions are not case-sensitive.
• But all variables are case sensitive.
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)
• We use php echo statement to output data to screen
• Variable type is automatically assigned according to value of the variable.
• We use dot operator to concatenate two or variables together
PHP Variable scope
• Local
• Global
• Static
Global scope
• A variable declared outside a function has a GLOBAL
SCOPE and can only be accessed outside a function.
• We need to use “global” keyword to access global
variable within in a function.
• PHP also stores all global variables in an array called
$GLOBALS[index]. The index holds the name of the
variable. This array is also accessible from within
functions and can be used to update global variables
directly.
Local scope
• A variable declared within a function has a LOCAL
SCOPE and can only be accessed within that function
static scope
• Normally, when a function is completed/executed, all of
its variables are deleted.
• But we can use “static” keyword to store the value of a
variable even after executing.
• So each time we called the function it picked previous
value of the variable from memory and use it.
Comments
• “//” or “#” => for single line comment
<?php
// This is a single-line comment
# This is also a single-line comment
?>
• “/*” and “*/” => for multiline comments
<?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>
Operators
echo and print
echo
▪ output data to the screen.
▪ echo has no return value
▪ echo can take multiple
parameters.
▪ echo is marginally faster than
print
print
▪ output data to the screen
▪ print has a return value of 1
▪ print can take one argument
PHP DATA TYPES
▪ PHP supports the following data types:
• String
• Integer
• Float (floating point numbers - also called double)
• Boolean
• Array
• Object
• NULL
• Resource
PHP BOOLEAN
▪ A Boolean represents two possible states: TRUE or FALSE.
$x = true;
$y = false;
PHP NULL VALUE
▪ Null is a special data type which can have only one value: NULL.
▪ A variable of data type NULL is a variable that has no value assigned
to it.
▪ If a variable is created without a value, it is automatically assigned a
value of NULL.
▪ Variables can also be emptied by setting the value to NULL:
<?php
$x = "Hello world!";
$x = null;
?>
PHP settype() function
▪ The settype() function converts a variable to a specific type,
▪ Syntax
settype(variable, type);
variable Required. Specifies the variable
to convert
type Required. Specifies the type to
convert variable to. The
possible types are: boolean,
bool, integer, int, float, double,
string, array, object, null
TYPECASTING IN PHP
▪ typecasting is a way to utilize one data type variable into the different data type.
• (int), (integer) - cast to integer
• (bool), (boolean) - cast to boolean
• (float), (double), (real) - cast to float
• (string) - cast to string
• (array) - cast to array
• (object) - cast to object
Arrays
▪ An array stores multiple values in one single variable.
▪ In PHP, the array() function is used to create an array.
▪ In PHP, there are three types of arrays:
• Indexed arrays - Arrays with a numeric index
• Associative arrays - Arrays with named keys
• Multidimensional arrays - Arrays containing one or more arrays
Indexed array
▪ There are two ways to create indexed arrays:
▪ The index can be assigned automatically (index always
starts at 0), like this:
$cars = array("Volvo", "BMW", "Toyota");
▪ or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
Associative arrays
▪ Associative arrays are arrays that use named keys that you
assign to them.
▪ There are two ways to create an associative array:
$age = array("Peter"=>"35", "Ben"=>"37",
"Joe"=>"43");
▪ or:
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
PHP strcmp() function
▪ The strcmp() function compares two strings
▪ Syntax
Strcmp(string1,string2)
• This function returns:
• 0 - if the two strings are equal
• <0 - if string1 is less than string2
• >0 - if string1 is greater than string2
Parameter Description
string1 Required. Specifies the first string to compare
string2 Required. Specifies the second string to compare

More Related Content

What's hot

Javascript session 01 - Introduction to Javascript
Javascript session 01 - Introduction to JavascriptJavascript session 01 - Introduction to Javascript
Javascript session 01 - Introduction to Javascript
Livingston Samuel
 
introduction to javascript
introduction to javascriptintroduction to javascript
introduction to javascript
Kumar
 

What's hot (20)

JS Event Loop
JS Event LoopJS Event Loop
JS Event Loop
 
Introduction to JavaScript Programming
Introduction to JavaScript ProgrammingIntroduction to JavaScript Programming
Introduction to JavaScript Programming
 
Introduction To JavaScript
Introduction To JavaScriptIntroduction To JavaScript
Introduction To JavaScript
 
Introduction to JavaScript Programming
Introduction to JavaScript ProgrammingIntroduction to JavaScript Programming
Introduction to JavaScript Programming
 
Javascript session 01 - Introduction to Javascript
Javascript session 01 - Introduction to JavascriptJavascript session 01 - Introduction to Javascript
Javascript session 01 - Introduction to Javascript
 
introduction to javascript
introduction to javascriptintroduction to javascript
introduction to javascript
 
Java script
Java scriptJava script
Java script
 
Web development basics (Part-7)
Web development basics (Part-7)Web development basics (Part-7)
Web development basics (Part-7)
 
JavaScript operators
JavaScript operatorsJavaScript operators
JavaScript operators
 
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
 
Javascript 01 (js)
Javascript 01 (js)Javascript 01 (js)
Javascript 01 (js)
 
Javascript functions
Javascript functionsJavascript functions
Javascript functions
 
Introduction to JavaScript Basics.
Introduction to JavaScript Basics.Introduction to JavaScript Basics.
Introduction to JavaScript Basics.
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
3. Java Script
3. Java Script3. Java Script
3. Java Script
 
Typescript
TypescriptTypescript
Typescript
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
002. Introducere in type script
002. Introducere in type script002. Introducere in type script
002. Introducere in type script
 

Similar to PHP Basics

MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
BUDNET
 
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
 

Similar to PHP Basics (20)

Php Basics
Php BasicsPhp Basics
Php Basics
 
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
 
php basic part one
php basic part onephp basic part one
php basic part one
 
Materi Dasar PHP
Materi Dasar PHPMateri Dasar PHP
Materi Dasar PHP
 
Php1
Php1Php1
Php1
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
05php
05php05php
05php
 
php Chapter 1.pptx
php Chapter 1.pptxphp Chapter 1.pptx
php Chapter 1.pptx
 
rtwerewr
rtwerewrrtwerewr
rtwerewr
 
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...
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
 
PHP MySQL Workshop - facehook
PHP MySQL Workshop - facehookPHP MySQL Workshop - facehook
PHP MySQL Workshop - facehook
 
INTRODUCTION to php.pptx
INTRODUCTION to php.pptxINTRODUCTION to php.pptx
INTRODUCTION to php.pptx
 
Introduction to PHP.pptx
Introduction to PHP.pptxIntroduction to PHP.pptx
Introduction to PHP.pptx
 
Intro to php
Intro to phpIntro to php
Intro to php
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
 
Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1
 
Php tutorial from_beginner_to_master
Php tutorial from_beginner_to_masterPhp tutorial from_beginner_to_master
Php tutorial from_beginner_to_master
 
05php
05php05php
05php
 
php fundamental
php fundamentalphp fundamental
php fundamental
 

Recently uploaded

Recently uploaded (20)

Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 

PHP Basics

  • 1.
  • 2. What is PHP? • PHP is an acronym for "PHP: Hypertext Preprocessor" • PHP is a widely-used, open source scripting language • PHP scripts are executed on the server, and the result is returned to the browser as plain HTML • PHP is free to download and use • PHP is used in WordPress and Facebook
  • 3. 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
  • 4. BASIC PHP SYNTAX • PHP scripting block starts with <?php and end with ?> • Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another • There are two basic statements to output text with PHP: echo and print. • PHP files have a file extension of ".php", ".php3", or ".phtml" .The default extension is “.php” • In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive. • But all variables are case sensitive.
  • 5. 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) • We use php echo statement to output data to screen • Variable type is automatically assigned according to value of the variable. • We use dot operator to concatenate two or variables together
  • 6. PHP Variable scope • Local • Global • Static
  • 7. Global scope • A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function. • We need to use “global” keyword to access global variable within in a function. • PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.
  • 8. Local scope • A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function
  • 9. static scope • Normally, when a function is completed/executed, all of its variables are deleted. • But we can use “static” keyword to store the value of a variable even after executing. • So each time we called the function it picked previous value of the variable from memory and use it.
  • 10. Comments • “//” or “#” => for single line comment <?php // This is a single-line comment # This is also a single-line comment ?> • “/*” and “*/” => for multiline comments <?php /* This is a multiple-lines comment block that spans over multiple lines */ ?>
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17. echo and print echo ▪ output data to the screen. ▪ echo has no return value ▪ echo can take multiple parameters. ▪ echo is marginally faster than print print ▪ output data to the screen ▪ print has a return value of 1 ▪ print can take one argument
  • 18. PHP DATA TYPES ▪ PHP supports the following data types: • String • Integer • Float (floating point numbers - also called double) • Boolean • Array • Object • NULL • Resource
  • 19. PHP BOOLEAN ▪ A Boolean represents two possible states: TRUE or FALSE. $x = true; $y = false;
  • 20. PHP NULL VALUE ▪ Null is a special data type which can have only one value: NULL. ▪ A variable of data type NULL is a variable that has no value assigned to it. ▪ If a variable is created without a value, it is automatically assigned a value of NULL. ▪ Variables can also be emptied by setting the value to NULL: <?php $x = "Hello world!"; $x = null; ?>
  • 21. PHP settype() function ▪ The settype() function converts a variable to a specific type, ▪ Syntax settype(variable, type); variable Required. Specifies the variable to convert type Required. Specifies the type to convert variable to. The possible types are: boolean, bool, integer, int, float, double, string, array, object, null
  • 22. TYPECASTING IN PHP ▪ typecasting is a way to utilize one data type variable into the different data type. • (int), (integer) - cast to integer • (bool), (boolean) - cast to boolean • (float), (double), (real) - cast to float • (string) - cast to string • (array) - cast to array • (object) - cast to object
  • 23. Arrays ▪ An array stores multiple values in one single variable. ▪ In PHP, the array() function is used to create an array. ▪ In PHP, there are three types of arrays: • Indexed arrays - Arrays with a numeric index • Associative arrays - Arrays with named keys • Multidimensional arrays - Arrays containing one or more arrays
  • 24. Indexed array ▪ There are two ways to create indexed arrays: ▪ The index can be assigned automatically (index always starts at 0), like this: $cars = array("Volvo", "BMW", "Toyota"); ▪ or the index can be assigned manually: $cars[0] = "Volvo"; $cars[1] = "BMW"; $cars[2] = "Toyota";
  • 25. Associative arrays ▪ Associative arrays are arrays that use named keys that you assign to them. ▪ There are two ways to create an associative array: $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); ▪ or: $age['Peter'] = "35"; $age['Ben'] = "37"; $age['Joe'] = "43";
  • 26. PHP strcmp() function ▪ The strcmp() function compares two strings ▪ Syntax Strcmp(string1,string2) • This function returns: • 0 - if the two strings are equal • <0 - if string1 is less than string2 • >0 - if string1 is greater than string2 Parameter Description string1 Required. Specifies the first string to compare string2 Required. Specifies the second string to compare