35
PHP for HTML Gurus Andrea Tarr Tarr Consulting J and Beyond 2012

PHP for HTML Gurus - J and Beyond 2012

Embed Size (px)

DESCRIPTION

If you are tired of copy/pasting bits of PHP in your layouts by trial and error, here is your chance to learn what those bits of PHP mean. This talk explains the PHP code found in layouts and templates: what it does, how you can change it, and what to watch out for. Intended for those who know HTML but don’t know what all those dollar signs and arrows are doing. And if you are a code guru, it's your chance to learn about layouts. We’ll start by briefly going over the basics of PHP, then move on to looking at the actual code in various layouts to illustrate what is being done. We’ll end by making changes and additions to the coding and seeing how that changes what you see on the sceen. If you want to learn more about PHP, see the book "PHP and MySQL, 24-Hour Trainer".

Citation preview

Page 1: PHP for HTML Gurus - J and Beyond 2012

PHP for HTML GurusAndrea TarrTarr ConsultingJ and Beyond 2012

Page 2: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Outline• PHP Basics• Explanation of

actual code in templates & layouts• Changing code

and seeing it in action• Book give away

2

Page 3: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

PHP Basics• Get in and out of PHP with <?php ?>• End each statement with a semi-colon<?php

$first_name = 'Andrea';$last_name = 'Tarr';

?>

• Whitespace is irrelevant<?php $first_name='Andrea';$last_name='Tarr';?>

• Omit any final ?> at the end of a file3

Page 4: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Comments• Single line comments// This is a single line comment$language = 'PHP'; // End of line comment// Multiple lines of single comments// can be done by repeating the slashes

• Multiple line comments/* With this type of comment you canstart a comment on one line and endit on an other line */

4

Page 5: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Variables• Used to store information that can change• Start with a $• Assign a value with =• Use single ('') or double ("") quotes for text• You need to assign a value before you can use the

variable• Use echo to display a value• Use a dot (.) to join items together

$first_name = 'Andrea';$last_name = 'Tarr';echo $first_name . ' ' . $last_name;

5

Page 6: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

$Variable Types• String Text: Andrea• Numbers: 42• Logical: True/False, 1/0• Array: An array is a list, either indexed or named• [0]=>Sun, [1]=>Mon, [2]=>Tue, [3]=>Wed• Name=>Joomla, Version=>2.5, • You can nest arrays

• Object: We'll come back to Object shortly

6

Page 7: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Functions()• Functions perform an action• You recognize a function by the ()• You can pass information to the function in the ()$full_name = ' Andrea Tarr ';echo trim($full_name);

• This results in Andrea Tarr

• You can pass a function to a functionecho strtoupper(trim($full_name);

• This results in ANDREA TARR7

Page 8: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

CONSTANTS• Constants are assigned once and don't change• No $• Usually in ALL CAPSdefine('SITENAME', 'My Site');echo SITENAME;

8

Page 9: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Objects• Think of an object as a named array plus functions• Properties are variables• Functions are also called methods

Examples:• $item->title contains the title• $item->introtext contains the intro text• $params->get('show_modify_date')

9

Page 10: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Classes• Classes are the blueprints that are used to create

objects• You can use the classes directly without creating

an object

Examples:• JText::_('COM_CONTENT_READ_MORE');• JHtml::_('string.truncate', ($this->item->title),

$params->get('readmore_limit'))

10

Page 11: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Making Decisions: if/else• Example 1if ($sum > 10) :

// do something when greater than 10else :

// do something when 10 or lessendif;

• Example 2if ($errors) :

// do something when there are errorsendif;

• You can also nest if statements

11

Page 12: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Comparison Operators

12Don't use = to compare!

Page 13: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

One Line If Statement• Normal wayif ($gender == 'M') :

echo 'Man';else :

echo 'Woman';endif;

• One line versionecho ($gender == 'M') ? 'Man' : 'Woman';

13

Page 14: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Alternative Syntax• Alternative Syntax used when mixing with HTMLif ($sum > 10) :

// do something when greater than 10else :

// do something when 10 or lessendif;

• Normal syntax with curly bracesif ($sum > 10) {

// do something when greater than 10} else {

// do something when 10 or less}

14

Page 15: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Looping• While• Repeats while a condition is true

• Do/While• Does it at least once then repeats while true

• For• Loops a given number of times

• Foreach• Repeats the code for each element in an array or

object

• Jumping out early• Continue

• Jump to the next iteration

• Break• Jump out of the loop

15

Page 16: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Template index.php file• Template Parameters• Conditional Stylesheets• Conditional Positions• One Line If Statement

16

Page 17: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

$color = $this->params->get('templatecolor');

<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/<?php echo htmlspecialchars($color); ?>.css" type="text/css" />

<link rel="stylesheet" href="/localhost/jc/templates/beez_20/css/personal.css" type="text/css" />

Template Parameters

17

Page 18: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Conditional Stylesheets<?php if ($this->direction == 'rtl') : ?> <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/template_rtl.css" type="text/css" /><?php endif; ?>

18

Page 19: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Conditional Positions<?php if ($this->countModules('position-12')): ?><div id="top"> <jdoc:include type="modules" name="position-12" /></div><?php endif; ?>

19

Page 20: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

$showRightColumn = ($this->countModules('position-3') OR$this->countModules('position-6') OR$this->countModules('position-8'));

<div id="<?php echo $showRightColumn ? 'contentarea2' : 'contentarea';?>">

If there are right modules: <div id="contentarea2">If there aren't: <div id="contentarea">

One Line If Statement

20

Page 21: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Module Layout • Latest Articles• mod_articles_latest/tmpl/default.php

21

Page 22: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Latest Articles

22

Page 23: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Latest News default.php

23

Page 24: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

var_dump()

24

Page 25: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Object $item

25

Page 26: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Object $item

26

Page 27: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Object $item – Page 2

27

Page 28: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Object $item – Page 3

28

Page 29: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Object $item – Page 4

29

Page 30: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Object $item – Page 5

30

Page 31: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Object $item – Page 6

31

Page 32: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Add information

32

Page 33: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

List with intro text

33

Page 34: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Questions?

34

Page 35: PHP for HTML Gurus - J and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Book Give Away

35