SlideShare a Scribd company logo
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

PPTX
php basics
PPTX
Php basics
PPT
PHP - Introduction to PHP
PDF
Introduction to php
PPT
PDF
Php a dynamic web scripting language
php basics
Php basics
PHP - Introduction to PHP
Introduction to php
Php a dynamic web scripting language

What's hot (20)

PPT
Introduction to php php++
PPTX
PHP Basics
PPTX
Constructor and encapsulation in php
PPT
Class 3 - PHP Functions
PPT
Introduction to PHP
PDF
07 Introduction to PHP #burningkeyboards
PPTX
Basic of PHP
PPT
PHP Workshop Notes
PPT
Control Structures In Php 2
PPT
Php Lecture Notes
PPSX
PHP Comprehensive Overview
PPTX
Php Tutorial
PPT
Php i basic chapter 3
ODP
PHP Basic
PPT
Basic PHP
PPT
PPTX
PPTX
Php.ppt
PDF
Data Types In PHP
PPTX
PHP slides
Introduction to php php++
PHP Basics
Constructor and encapsulation in php
Class 3 - PHP Functions
Introduction to PHP
07 Introduction to PHP #burningkeyboards
Basic of PHP
PHP Workshop Notes
Control Structures In Php 2
Php Lecture Notes
PHP Comprehensive Overview
Php Tutorial
Php i basic chapter 3
PHP Basic
Basic PHP
Php.ppt
Data Types In PHP
PHP slides
Ad

Viewers also liked (18)

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

Similar to Dev traning 2016 basics of PHP (20)

PDF
Zend Certification Preparation Tutorial
PPT
MIND sweeping introduction to PHP
PDF
Php Crash Course - Macq Electronique 2010
PPT
Synapseindia reviews on array php
KEY
PHP Underground Session 1: The Basics
PPTX
Introduction in php
PPTX
PHP Basics and Demo HackU
PPTX
Introduction To PHP000000000000000000000000000000.pptx
PPT
PDF
PHP Programming and its Applications workshop
PDF
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
PPT
PPT
Php introduction with history of php
PPT
php fundamental
PPT
PPT
rtwerewr
PDF
2014 database - course 2 - php
PPT
Building an e:commerce site with PHP
PPTX
php programming.pptx
Zend Certification Preparation Tutorial
MIND sweeping introduction to PHP
Php Crash Course - Macq Electronique 2010
Synapseindia reviews on array php
PHP Underground Session 1: The Basics
Introduction in php
PHP Basics and Demo HackU
Introduction To PHP000000000000000000000000000000.pptx
PHP Programming and its Applications workshop
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
Php introduction with history of php
php fundamental
rtwerewr
2014 database - course 2 - php
Building an e:commerce site with PHP
php programming.pptx

Recently uploaded (20)

PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Introduction to Artificial Intelligence
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Digital Strategies for Manufacturing Companies
PPTX
Transform Your Business with a Software ERP System
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
System and Network Administraation Chapter 3
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
2025 Textile ERP Trends: SAP, Odoo & Oracle
Operating system designcfffgfgggggggvggggggggg
Design an Analysis of Algorithms I-SECS-1021-03
How to Choose the Right IT Partner for Your Business in Malaysia
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PTS Company Brochure 2025 (1).pdf.......
Internet Downloader Manager (IDM) Crack 6.42 Build 41
ISO 45001 Occupational Health and Safety Management System
Odoo Companies in India – Driving Business Transformation.pdf
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Introduction to Artificial Intelligence
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Digital Strategies for Manufacturing Companies
Transform Your Business with a Software ERP System
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
System and Network Administraation Chapter 3
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...

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