dev traning 2016 basics of php

28
Basics of PHP SACHEEN DHANJIE

Upload: sacheen-dhanjie

Post on 22-Feb-2017

170 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Dev traning 2016   basics of PHP

Basics of PHPSACHEEN DHANJIE

Page 2: Dev traning 2016   basics of PHP

Sacheen DhanjieSenior Developer @ Afrihost.com

Page 3: Dev traning 2016   basics of PHP

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

Page 4: Dev traning 2016   basics of PHP

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);

Page 5: Dev traning 2016   basics of PHP

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/

Page 6: Dev traning 2016   basics of PHP

Numbers• echo 2 + 2; ?• echo '2' + 2; ?• ;echo '2' + "2"; ?• echo 2 + 'c’; ?

Page 7: Dev traning 2016   basics of PHP

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

Page 8: Dev traning 2016   basics of PHP

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

Page 9: Dev traning 2016   basics of PHP

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

Page 10: Dev traning 2016   basics of PHP

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))

Page 11: Dev traning 2016   basics of PHP

What if $bob?

Bob Ross or just Ross?

Page 12: Dev traning 2016   basics of PHP

What if $bob• (!$bob)

• If $bob was null; //true• If $bob was 1; //false• If $bob was false; //true• If $bob was 0; //true

Page 13: Dev traning 2016   basics of PHP

Conditions

Page 14: Dev traning 2016   basics of PHP

Conditions

Page 15: Dev traning 2016   basics of PHP

Arrays

Page 16: Dev traning 2016   basics of PHP

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

Page 17: Dev traning 2016   basics of PHP

I hate else

Page 18: Dev traning 2016   basics of PHP

Limit Nesting

Any suggestions to neaten this up?

Page 19: Dev traning 2016   basics of PHP

Limit Nesting

Page 20: Dev traning 2016   basics of PHP

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

Page 21: Dev traning 2016   basics of PHP

Do one thing and do it well

Page 22: Dev traning 2016   basics of PHP

Simple Objects – Object Oriented Programing

• Classes• Can be instantiated• Has instance variables• getters• Setters [mutators]• Can be extended• Accessibility

• - public / private / protected

Page 23: Dev traning 2016   basics of PHP

User Class

Page 24: Dev traning 2016   basics of PHP

Using the User Class

Page 25: Dev traning 2016   basics of PHP

Extend the User Class

Page 26: Dev traning 2016   basics of PHP

Using the Reseller Class

Page 27: Dev traning 2016   basics of PHP

Cool Functions

Page 28: Dev traning 2016   basics of PHP

Something Cool