Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
PHP Basics for Designers <ul><ul><li>Matthew Turland </li></ul></ul><ul><ul><li>Atlanta Web Designers Group </li></ul></ul...
So what is PHP? <ul><ul><li>A programming language. </li></ul></ul><ul><ul><li>Mainly intended for web applications. </li>...
Let's jump right in... <ul><ul><li>These do the same thing. </li></ul></ul><ul><ul><li><p>Hello world!</p> </li></ul></ul>...
How is that useful? <ul><ul><li>When 'Hello world!' could be anything. </li></ul></ul><ul><ul><li><?php $title = 'Goodbye ...
Can you break that down? <ul><ul><li><?php $title = 'Goodbye cruel world!'; ?> </li></ul></ul>opening PHP tag variable ass...
A little shorter, maybe? <ul><ul><li>These do the same thing. </li></ul></ul><ul><ul><li><p> <?php echo $title; ?> </p> </...
Now you see me... <ul><ul><li>The HTML here is displayed. </li></ul></ul><ul><ul><li><?php $sidebar = true; ?> </li></ul><...
... and now you don't. <ul><ul><li>The HTML here is  not  displayed. </li></ul></ul><ul><ul><li><?php $sidebar =   false ;...
On one condition... <ul><ul><li><?php if ($page   ==   'home'): ?> </li></ul></ul><ul><ul><li><ul> </li></ul></ul><ul><ul>...
Playing with strings <ul><ul><li>These all result in the same output. </li></ul></ul><ul><ul><li>echo 'Hello'  .  ' '  .  ...
Building strings <ul><ul><li>These do the same thing. </li></ul></ul><ul><ul><li>$greeting = 'Hello'; </li></ul></ul><ul><...
All for one... <ul><ul><li>Your standard HTML list. </li></ul></ul><ul><ul><li><ul> </li></ul></ul><ul><ul><li><li>Milk</l...
... one for all. <ul><ul><li>The same list of items in PHP. </li></ul></ul><ul><ul><li><?php $list = array( 'Milk', 'Eggs'...
Adding a dimension... <ul><ul><li><?php </li></ul></ul><ul><ul><li>$links = array( </li></ul></ul><ul><ul><li>array( </li>...
... and displaying it. <ul><ul><li><ul> </li></ul></ul><ul><ul><li><?php foreach ($links as $link): ?> </li></ul></ul><ul>...
The results are in! <ul><ul><li><ul> </li></ul></ul><ul><ul><li><li><a href=” /exciting-new-headline ”> Exciting New Headl...
The not-so-great wall <ul><ul><li>http://example.com/foo.php?title=<script>... </li></ul></ul><ul><ul><li><p> <?= $_GET['t...
The great escape <ul><ul><li>http://example.com/foo.php?title=<script>... </li></ul></ul><ul><ul><li><p> <?= htmlspecialch...
Stylish yet functional <ul><ul><li>Functions are reusable bits of code. </li></ul></ul><ul><ul><li>function ul($items) { <...
Call me <ul><ul><li>This... </li></ul></ul><ul><ul><li><?= ul(array('Milk', 'Eggs', 'Bread')) ?> </li></ul></ul><ul><ul><l...
Plug and play <?php include 'header.html'; ?> <?php include 'navigation.php'; ?> Content <?php include 'footer.html'; ?> <...
Want to learn more? <ul><ul><li>http://php.net/manual/en </li></ul></ul><ul><ul><li>http://phpcommunity.org </li></ul></ul...
To sum it up... <ul><ul><li>Avoid copy and paste. Includes and functions! </li></ul></ul><ul><ul><li>Before outputting var...
Questions?
Upcoming SlideShare
Loading in …5
×

PHP Basics for Designers

4,194 views

Published on

Published in: Technology
  • Be the first to like this

PHP Basics for Designers

  1. 1. PHP Basics for Designers <ul><ul><li>Matthew Turland </li></ul></ul><ul><ul><li>Atlanta Web Designers Group </li></ul></ul><ul><ul><li>November 11, 2008 </li></ul></ul>
  2. 2. So what is PHP? <ul><ul><li>A programming language. </li></ul></ul><ul><ul><li>Mainly intended for web applications. </li></ul></ul><ul><ul><li>Also useful for other things. </li></ul></ul><ul><ul><li>That last part is another story for another time... </li></ul></ul>
  3. 3. Let's jump right in... <ul><ul><li>These do the same thing. </li></ul></ul><ul><ul><li><p>Hello world!</p> </li></ul></ul><ul><ul><li><p> <?php echo 'Hello world!'; ?> </p> </li></ul></ul>
  4. 4. How is that useful? <ul><ul><li>When 'Hello world!' could be anything. </li></ul></ul><ul><ul><li><?php $title = 'Goodbye cruel world!'; ?> </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li><p> <?php echo $title; ?> </p> </li></ul></ul><ul><ul><li>Result : <p>Goodbye cruel world!</p> </li></ul></ul>
  5. 5. Can you break that down? <ul><ul><li><?php $title = 'Goodbye cruel world!'; ?> </li></ul></ul>opening PHP tag variable assignment operator string (quote to quote) closing PHP tag end of statement
  6. 6. A little shorter, maybe? <ul><ul><li>These do the same thing. </li></ul></ul><ul><ul><li><p> <?php echo $title; ?> </p> </li></ul></ul><ul><ul><li><p> <?= $title ?> </p> </li></ul></ul><ul><ul><li>For #2, short_open_tag must be on. </li></ul></ul>
  7. 7. Now you see me... <ul><ul><li>The HTML here is displayed. </li></ul></ul><ul><ul><li><?php $sidebar = true; ?> </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li><?php if ($sidebar): ?> </li></ul></ul><ul><ul><li><ul> </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li></ul> </li></ul></ul><ul><ul><li><?php endif; ?> </li></ul></ul>
  8. 8. ... and now you don't. <ul><ul><li>The HTML here is not displayed. </li></ul></ul><ul><ul><li><?php $sidebar = false ; ?> </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li><?php if ($sidebar): ?> </li></ul></ul><ul><ul><li><ul> </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li></ul> </li></ul></ul><ul><ul><li><?php endif; ?> </li></ul></ul>Here's why
  9. 9. On one condition... <ul><ul><li><?php if ($page == 'home'): ?> </li></ul></ul><ul><ul><li><ul> </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li></ul> </li></ul></ul><ul><ul><li><?php endif; ?> </li></ul></ul>comparison operator == for “is” != for “is not”
  10. 10. Playing with strings <ul><ul><li>These all result in the same output. </li></ul></ul><ul><ul><li>echo 'Hello' . ' ' . 'world!'; </li></ul></ul><ul><ul><li>$greeting = 'Hello'; </li></ul></ul><ul><ul><li>echo $greeting . ' ' . 'world!'; </li></ul></ul><ul><ul><li>$object = 'world'; </li></ul></ul><ul><ul><li>echo $greeting . ' ' . $object . '!'; </li></ul></ul>concatenation operator (combines strings)
  11. 11. Building strings <ul><ul><li>These do the same thing. </li></ul></ul><ul><ul><li>$greeting = 'Hello'; </li></ul></ul><ul><ul><li>$greeting = $greeting . ' world!'; </li></ul></ul><ul><ul><li>$greeting = 'Hello'; </li></ul></ul><ul><ul><li>$greeting .= ' world!'; </li></ul></ul>
  12. 12. All for one... <ul><ul><li>Your standard HTML list. </li></ul></ul><ul><ul><li><ul> </li></ul></ul><ul><ul><li><li>Milk</li> </li></ul></ul><ul><ul><li><li>Eggs</li> </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li><li>Bread</li> </li></ul></ul><ul><ul><li></ul> </li></ul></ul>
  13. 13. ... one for all. <ul><ul><li>The same list of items in PHP. </li></ul></ul><ul><ul><li><?php $list = array( 'Milk', 'Eggs', ... , 'Bread'); ?> </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li><ul> </li></ul></ul><ul><ul><li><?php foreach ($list as $item): ?> </li></ul></ul><ul><ul><li><li> <?php echo $item; ?> </li> </li></ul></ul><ul><ul><li><?php endforeach; ?> </li></ul></ul><ul><ul><li></ul> </li></ul></ul>
  14. 14. Adding a dimension... <ul><ul><li><?php </li></ul></ul><ul><ul><li>$links = array( </li></ul></ul><ul><ul><li>array( </li></ul></ul><ul><ul><li>'text' => 'Exciting new headline', </li></ul></ul><ul><ul><li>'href' => '/exciting-new-headline' </li></ul></ul><ul><ul><li>), </li></ul></ul><ul><ul><li>array( </li></ul></ul><ul><ul><li>'text' => 'News from yesterday', </li></ul></ul><ul><ul><li>'href' => '/news-from-yesterday' </li></ul></ul><ul><ul><li>) </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>?> </li></ul></ul>“ row” “ column” key value
  15. 15. ... and displaying it. <ul><ul><li><ul> </li></ul></ul><ul><ul><li><?php foreach ($links as $link): ?> </li></ul></ul><ul><ul><li><li><a href=” <?php echo $link['href']; ?> ”> </li></ul></ul><ul><ul><li><?php echo $link['text']; ?> </a></li> </li></ul></ul><ul><ul><li><?php endforeach; ?> </li></ul></ul><ul><ul><li></ul> </li></ul></ul>
  16. 16. The results are in! <ul><ul><li><ul> </li></ul></ul><ul><ul><li><li><a href=” /exciting-new-headline ”> Exciting New Headline </a></li> </li></ul></ul><ul><ul><li><li><a href=” /news-from-yesterday ”> News from yesterday </a></li> </li></ul></ul><ul><ul><li></ul> </li></ul></ul>
  17. 17. The not-so-great wall <ul><ul><li>http://example.com/foo.php?title=<script>... </li></ul></ul><ul><ul><li><p> <?= $_GET['title'] ?> </p> </li></ul></ul><ul><ul><li>Result : <p><script>...</p> </li></ul></ul><ul><ul><li>Most likely, this is not good for security. </li></ul></ul>
  18. 18. The great escape <ul><ul><li>http://example.com/foo.php?title=<script>... </li></ul></ul><ul><ul><li><p> <?= htmlspecialchars($_GET['title']) ?> </p> </li></ul></ul><ul><ul><li>Result : <p>&lt;script&gt;...</p> </li></ul></ul><ul><ul><li>Rendered result: <script> </li></ul></ul>
  19. 19. Stylish yet functional <ul><ul><li>Functions are reusable bits of code. </li></ul></ul><ul><ul><li>function ul($items) { </li></ul></ul><ul><ul><li>$result = '<ul>'; </li></ul></ul><ul><ul><li>foreach ($items as $item) { </li></ul></ul><ul><ul><li>$result .= '<li>' . htmlspecialchars($item) . '</li>'; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>return $result . '</ul>'; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  20. 20. Call me <ul><ul><li>This... </li></ul></ul><ul><ul><li><?= ul(array('Milk', 'Eggs', 'Bread')) ?> </li></ul></ul><ul><ul><li>... becomes this... </li></ul></ul><ul><ul><li><ul><li>Milk</li><li>Eggs</li><li>Bread</li></ul> </li></ul></ul>
  21. 21. Plug and play <?php include 'header.html'; ?> <?php include 'navigation.php'; ?> Content <?php include 'footer.html'; ?> <?php include 'sidebar.html'; ?>
  22. 22. Want to learn more? <ul><ul><li>http://php.net/manual/en </li></ul></ul><ul><ul><li>http://phpcommunity.org </li></ul></ul><ul><ul><li>irc://irc.freenode.net/phpc </li></ul></ul><ul><ul><li>http://phpwomen.org </li></ul></ul><ul><ul><li>irc://irc.freenode.net/phpwomen </li></ul></ul><ul><ul><li>http://phpbuilder.com/board </li></ul></ul><ul><ul><li>http://www.amazon.com/dp/0596005601 </li></ul></ul>
  23. 23. To sum it up... <ul><ul><li>Avoid copy and paste. Includes and functions! </li></ul></ul><ul><ul><li>Before outputting variables, escape them! </li></ul></ul><ul><ul><li>PHP has lots of built-in functions. Use them! </li></ul></ul><ul><ul><li>Don't let your learning about PHP stop here! </li></ul></ul>
  24. 24. Questions?

×