PHP Basics for Designers

4,039 views

Published on

Published in: Technology
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
4,039
On SlideShare
0
From Embeds
0
Number of Embeds
495
Actions
Shares
0
Downloads
99
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide
  • 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?

    ×