SlideShare a Scribd company logo
1 of 28
Basics of PHP
SACHEEN DHANJIE
Sacheen Dhanjie
Senior Developer @ Afrihost.com
What is PHP?
• Hypertext PreProcessor
• Built on C
• Made for the web, but can run on the
command line
• We run all our processing scripts via the
CLI
Variables
• The $ is used to prefix a string, which denotes a variable
• $name = “BOB”; //this assigns the variable
• $sentence = “my name is $name”; // what will this out put?
• $concatenate = “my name is ”.$name; //what will this
output?
• Display information
• echo $sentence;
• print($sentence);
Naming Variables
• $name; // lower case for single words
• $lastName; //camel case for more than one word
• $animalsArray = (‘cow’,’dog’); // not good
• $animals = (‘cow’,’dog’);
• $userObj = new User() // not good
• $user = new User();
• http://www.php-fig.org/psr/
Numbers
• echo 2 + 2; ?
• echo '2' + 2; ?
• ;echo '2' + "2"; ?
• echo 2 + 'c’; ?
Numbers
• Everything in PHP is cast to an integer by default
• echo 2 + 2; //4
• echo '2' + 2; //4
• ;echo '2' + "2"; //4
• echo 2 + 'c’; //2
Casting
• What is casting?
• Casting means making a value a particular type
• $i = 2.332242;
• (int) $i; //2
• (float) $i; // 2.332242
• (string) $i; // 2.332242
Casting
• Why do we care about casting?
• When we interact with data, the language you work with may assume
things
• If you parse and XML message - https://en.wikipedia.org/wiki/XML
• <amount>10.32</amount>
• Using an XML parser, the 10.32 will be assumed as an integer, 10 and
not 10.32
• Casting the value will give you the 10.32
• These things are picked up as you learn to code
Operations
• < //less than
• > //greater than
• <= //less than or equal to
• >= //greater than or equal to
• = //assignment $i =1;
• == //equality (1 == true)
• === //type equlity (1 === true)
• ! //negation - you should be carful (!$bob instanceof
Class) rather (!($bob instanceof Class))
What if $bob?
Bob Ross or just Ross?
What if $bob
• (!$bob)
• If $bob was null; //true
• If $bob was 1; //false
• If $bob was false; //true
• If $bob was 0; //true
Conditions
Conditions
Arrays
Arrays
• Can be used to represent a data source [database, file,
etc.]
• There are many built in functions for arrays.
http://php.net/manual/en/function.array.php
• array_sum($amounts); //R24.23;
• Database queries can be returned as arrays
I hate else
Limit Nesting
Any suggestions to neaten this up?
Limit Nesting
Nesting
• Keep your nesting to a minimal
• The deeper you nest the more complex you code
becomes
• The more it is to debug
• Refactor if your code becomes a mess.
• Make smaller functions for each complex bit.
• Avoid else
• Return early is not a bad thing
Do one thing and do it well
Simple Objects – Object Oriented Programing
• Classes
• Can be instantiated
• Has instance variables
• getters
• Setters [mutators]
• Can be extended
• Accessibility
• - public / private / protected
User Class
Using the User Class
Extend the User Class
Using the Reseller Class
Cool Functions
Something Cool

More Related Content

What's hot

What's hot (20)

Introduction to php php++
Introduction to php php++Introduction to php php++
Introduction to php php++
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
Constructor and encapsulation in php
Constructor and encapsulation in phpConstructor and encapsulation in php
Constructor and encapsulation in php
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards
 
Basic of PHP
Basic of PHPBasic of PHP
Basic of PHP
 
PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop Notes
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
 
Php Lecture Notes
Php Lecture NotesPhp Lecture Notes
Php Lecture Notes
 
PHP Comprehensive Overview
PHP Comprehensive OverviewPHP Comprehensive Overview
PHP Comprehensive Overview
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
 
Basic PHP
Basic PHPBasic PHP
Basic PHP
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Php
PhpPhp
Php
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
Data Types In PHP
Data Types In PHPData Types In PHP
Data Types In PHP
 
PHP slides
PHP slidesPHP slides
PHP slides
 

Viewers also liked

Mackin lay
Mackin layMackin lay
Mackin lay
benjamac
 
Rosendale-Road-Brochure_Final-Web
Rosendale-Road-Brochure_Final-WebRosendale-Road-Brochure_Final-Web
Rosendale-Road-Brochure_Final-Web
Sabastian Dyer
 
Formula integral no. 6
Formula integral no. 6Formula integral no. 6
Formula integral no. 6
Ceci Guerrero
 

Viewers also liked (18)

Mackin lay
Mackin layMackin lay
Mackin lay
 
Excel 2010
Excel 2010Excel 2010
Excel 2010
 
Autoforma relj-fmmp (1)
Autoforma relj-fmmp (1)Autoforma relj-fmmp (1)
Autoforma relj-fmmp (1)
 
Luciano web
Luciano webLuciano web
Luciano web
 
Rosendale-Road-Brochure_Final-Web
Rosendale-Road-Brochure_Final-WebRosendale-Road-Brochure_Final-Web
Rosendale-Road-Brochure_Final-Web
 
El poder electoral II
El poder electoral IIEl poder electoral II
El poder electoral II
 
Gionelly_Fernández_Herramientas web 2.0_blog
Gionelly_Fernández_Herramientas web 2.0_blogGionelly_Fernández_Herramientas web 2.0_blog
Gionelly_Fernández_Herramientas web 2.0_blog
 
1001 libros que leer
1001 libros que leer1001 libros que leer
1001 libros que leer
 
541 an 15 setembro_2015.ok
541 an 15 setembro_2015.ok541 an 15 setembro_2015.ok
541 an 15 setembro_2015.ok
 
Dev traning 2016 databases
Dev traning 2016   databasesDev traning 2016   databases
Dev traning 2016 databases
 
Formula integral no. 6
Formula integral no. 6Formula integral no. 6
Formula integral no. 6
 
Presentación100
Presentación100Presentación100
Presentación100
 
Scaling
ScalingScaling
Scaling
 
Kaufman Research Interests
Kaufman Research InterestsKaufman Research Interests
Kaufman Research Interests
 
Cv16
Cv16Cv16
Cv16
 
Workshop #5: Phygital - The Future of Seating by L+W
Workshop #5: Phygital - The Future of Seating by L+WWorkshop #5: Phygital - The Future of Seating by L+W
Workshop #5: Phygital - The Future of Seating by L+W
 
Dt Wcdma Validação De Sites WCDMA - Parte 2
Dt Wcdma   Validação De Sites  WCDMA - Parte 2Dt Wcdma   Validação De Sites  WCDMA - Parte 2
Dt Wcdma Validação De Sites WCDMA - Parte 2
 
How to choose an idea for your startup Dalton Caldwell Y Combinator
How to choose an idea for your startup  Dalton Caldwell Y CombinatorHow to choose an idea for your startup  Dalton Caldwell Y Combinator
How to choose an idea for your startup Dalton Caldwell Y Combinator
 

Similar to Dev traning 2016 basics of PHP

MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
BUDNET
 
PHP Underground Session 1: The Basics
PHP Underground Session 1: The BasicsPHP Underground Session 1: The Basics
PHP Underground Session 1: The Basics
Robin Hawkes
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of php
pooja bhandari
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
Hung-yu Lin
 
Building an e:commerce site with PHP
Building an e:commerce site with PHPBuilding an e:commerce site with PHP
Building an e:commerce site with PHP
webhostingguy
 

Similar to Dev traning 2016 basics of PHP (20)

Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
 
Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010
 
Synapseindia reviews on array php
Synapseindia reviews on array phpSynapseindia reviews on array php
Synapseindia reviews on array php
 
PHP Underground Session 1: The Basics
PHP Underground Session 1: The BasicsPHP Underground Session 1: The Basics
PHP Underground Session 1: The Basics
 
Intro to Perl and Bioperl
Intro to Perl and BioperlIntro to Perl and Bioperl
Intro to Perl and Bioperl
 
Introduction in php
Introduction in phpIntroduction in php
Introduction in php
 
PHP Basics and Demo HackU
PHP Basics and Demo HackUPHP Basics and Demo HackU
PHP Basics and Demo HackU
 
05php
05php05php
05php
 
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 fundamental
php fundamentalphp fundamental
php fundamental
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of php
 
php
phpphp
php
 
05php
05php05php
05php
 
rtwerewr
rtwerewrrtwerewr
rtwerewr
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
 
Building an e:commerce site with PHP
Building an e:commerce site with PHPBuilding an e:commerce site with PHP
Building an e:commerce site with PHP
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptx
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
My cool new Slideshow!
My cool new Slideshow!My cool new Slideshow!
My cool new Slideshow!
 

Recently uploaded

Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 

Recently uploaded (20)

%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 

Dev traning 2016 basics of PHP

  • 3. What is PHP? • Hypertext PreProcessor • Built on C • Made for the web, but can run on the command line • We run all our processing scripts via the CLI
  • 4. Variables • The $ is used to prefix a string, which denotes a variable • $name = “BOB”; //this assigns the variable • $sentence = “my name is $name”; // what will this out put? • $concatenate = “my name is ”.$name; //what will this output? • Display information • echo $sentence; • print($sentence);
  • 5. Naming Variables • $name; // lower case for single words • $lastName; //camel case for more than one word • $animalsArray = (‘cow’,’dog’); // not good • $animals = (‘cow’,’dog’); • $userObj = new User() // not good • $user = new User(); • http://www.php-fig.org/psr/
  • 6. Numbers • echo 2 + 2; ? • echo '2' + 2; ? • ;echo '2' + "2"; ? • echo 2 + 'c’; ?
  • 7. Numbers • Everything in PHP is cast to an integer by default • echo 2 + 2; //4 • echo '2' + 2; //4 • ;echo '2' + "2"; //4 • echo 2 + 'c’; //2
  • 8. Casting • What is casting? • Casting means making a value a particular type • $i = 2.332242; • (int) $i; //2 • (float) $i; // 2.332242 • (string) $i; // 2.332242
  • 9. Casting • Why do we care about casting? • When we interact with data, the language you work with may assume things • If you parse and XML message - https://en.wikipedia.org/wiki/XML • <amount>10.32</amount> • Using an XML parser, the 10.32 will be assumed as an integer, 10 and not 10.32 • Casting the value will give you the 10.32 • These things are picked up as you learn to code
  • 10. Operations • < //less than • > //greater than • <= //less than or equal to • >= //greater than or equal to • = //assignment $i =1; • == //equality (1 == true) • === //type equlity (1 === true) • ! //negation - you should be carful (!$bob instanceof Class) rather (!($bob instanceof Class))
  • 11. What if $bob? Bob Ross or just Ross?
  • 12. What if $bob • (!$bob) • If $bob was null; //true • If $bob was 1; //false • If $bob was false; //true • If $bob was 0; //true
  • 16. Arrays • Can be used to represent a data source [database, file, etc.] • There are many built in functions for arrays. http://php.net/manual/en/function.array.php • array_sum($amounts); //R24.23; • Database queries can be returned as arrays
  • 18. Limit Nesting Any suggestions to neaten this up?
  • 20. Nesting • Keep your nesting to a minimal • The deeper you nest the more complex you code becomes • The more it is to debug • Refactor if your code becomes a mess. • Make smaller functions for each complex bit. • Avoid else • Return early is not a bad thing
  • 21. Do one thing and do it well
  • 22. Simple Objects – Object Oriented Programing • Classes • Can be instantiated • Has instance variables • getters • Setters [mutators] • Can be extended • Accessibility • - public / private / protected
  • 24. Using the User Class