PHP Basics for Designers

0 %
100 %
Information about PHP Basics for Designers
Technology

Published on November 12, 2008

Author: tobias382

Source: slideshare.net

PHP Basics for Designers Matthew Turland Atlanta Web Designers Group November 11, 2008

Matthew Turland

Atlanta Web Designers Group

November 11, 2008

So what is PHP? A programming language. Mainly intended for web applications. Also useful for other things. That last part is another story for another time...

A programming language.

Mainly intended for web applications.

Also useful for other things.

That last part is another story for another time...

Let's jump right in... These do the same thing. <p>Hello world!</p> <p> <?php echo 'Hello world!'; ?> </p>

These do the same thing.

<p>Hello world!</p>

<p> <?php echo 'Hello world!'; ?> </p>

How is that useful? When 'Hello world!' could be anything. <?php $title = 'Goodbye cruel world!'; ?> ... <p> <?php echo $title; ?> </p> Result : <p>Goodbye cruel world!</p>

When 'Hello world!' could be anything.

<?php $title = 'Goodbye cruel world!'; ?>

...

<p> <?php echo $title; ?> </p>

Result : <p>Goodbye cruel world!</p>

Can you break that down? <?php $title = 'Goodbye cruel world!'; ?> opening PHP tag variable assignment operator string (quote to quote) closing PHP tag end of statement

<?php $title = 'Goodbye cruel world!'; ?>

A little shorter, maybe? These do the same thing. <p> <?php echo $title; ?> </p> <p> <?= $title ?> </p> For #2, short_open_tag must be on.

These do the same thing.

<p> <?php echo $title; ?> </p>

<p> <?= $title ?> </p>

For #2, short_open_tag must be on.

Now you see me... The HTML here is displayed. <?php $sidebar = true; ?> ... <?php if ($sidebar): ?> <ul> ... </ul> <?php endif; ?>

The HTML here is displayed.

<?php $sidebar = true; ?>

...

<?php if ($sidebar): ?>

<ul>

...

</ul>

<?php endif; ?>

... and now you don't. The HTML here is not displayed. <?php $sidebar = false ; ?> ... <?php if ($sidebar): ?> <ul> ... </ul> <?php endif; ?> Here's why

The HTML here is not displayed.

<?php $sidebar = false ; ?>

...

<?php if ($sidebar): ?>

<ul>

...

</ul>

<?php endif; ?>

On one condition... <?php if ($page == 'home'): ?> <ul> ... </ul> <?php endif; ?> comparison operator == for “is” != for “is not”

<?php if ($page == 'home'): ?>

<ul>

...

</ul>

<?php endif; ?>

Playing with strings These all result in the same output. echo 'Hello' . ' ' . 'world!'; $greeting = 'Hello'; echo $greeting . ' ' . 'world!'; $object = 'world'; echo $greeting . ' ' . $object . '!'; concatenation operator (combines strings)

These all result in the same output.

echo 'Hello' . ' ' . 'world!';

$greeting = 'Hello';

echo $greeting . ' ' . 'world!';

$object = 'world';

echo $greeting . ' ' . $object . '!';

Building strings These do the same thing. $greeting = 'Hello'; $greeting = $greeting . ' world!'; $greeting = 'Hello'; $greeting .= ' world!';

These do the same thing.

$greeting = 'Hello';

$greeting = $greeting . ' world!';

$greeting = 'Hello';

$greeting .= ' world!';

All for one... Your standard HTML list. <ul> <li>Milk</li> <li>Eggs</li> ... <li>Bread</li> </ul>

Your standard HTML list.

<ul>

<li>Milk</li>

<li>Eggs</li>

...

<li>Bread</li>

</ul>

... one for all. The same list of items in PHP. <?php $list = array( 'Milk', 'Eggs', ... , 'Bread'); ?> ... <ul> <?php foreach ($list as $item): ?> <li> <?php echo $item; ?> </li> <?php endforeach; ?> </ul>

The same list of items in PHP.

<?php $list = array( 'Milk', 'Eggs', ... , 'Bread'); ?>

...

<ul>

<?php foreach ($list as $item): ?>

<li> <?php echo $item; ?> </li>

<?php endforeach; ?>

</ul>

Adding a dimension... <?php $links = array( array( 'text' => 'Exciting new headline', 'href' => '/exciting-new-headline' ), array( 'text' => 'News from yesterday', 'href' => '/news-from-yesterday' ) ); ?> “ row” “ column” key value

<?php

$links = array(

array(

'text' => 'Exciting new headline',

'href' => '/exciting-new-headline'

),

array(

'text' => 'News from yesterday',

'href' => '/news-from-yesterday'

)

);

?>

... and displaying it. <ul> <?php foreach ($links as $link): ?> <li><a href=” <?php echo $link['href']; ?> ”> <?php echo $link['text']; ?> </a></li> <?php endforeach; ?> </ul>

<ul>

<?php foreach ($links as $link): ?>

<li><a href=” <?php echo $link['href']; ?> ”>

<?php echo $link['text']; ?> </a></li>

<?php endforeach; ?>

</ul>

The results are in! <ul> <li><a href=” /exciting-new-headline ”> Exciting New Headline </a></li> <li><a href=” /news-from-yesterday ”> News from yesterday </a></li> </ul>

<ul>

<li><a href=” /exciting-new-headline ”> Exciting New Headline </a></li>

<li><a href=” /news-from-yesterday ”> News from yesterday </a></li>

</ul>

The not-so-great wall http://example.com/foo.php?title=<script>... <p> <?= $_GET['title'] ?> </p> Result : <p><script>...</p> Most likely, this is not good for security.

http://example.com/foo.php?title=<script>...

<p> <?= $_GET['title'] ?> </p>

Result : <p><script>...</p>

Most likely, this is not good for security.

The great escape http://example.com/foo.php?title=<script>... <p> <?= htmlspecialchars($_GET['title']) ?> </p> Result : <p>&lt;script&gt;...</p> Rendered result: <script>

http://example.com/foo.php?title=<script>...

<p> <?= htmlspecialchars($_GET['title']) ?> </p>

Result : <p>&lt;script&gt;...</p>

Rendered result: <script>

Stylish yet functional Functions are reusable bits of code. function ul($items) { $result = '<ul>'; foreach ($items as $item) { $result .= '<li>' . htmlspecialchars($item) . '</li>'; } return $result . '</ul>'; }

Functions are reusable bits of code.

function ul($items) {

$result = '<ul>';

foreach ($items as $item) {

$result .= '<li>' . htmlspecialchars($item) . '</li>';

}

return $result . '</ul>';

}

Call me This... <?= ul(array('Milk', 'Eggs', 'Bread')) ?> ... becomes this... <ul><li>Milk</li><li>Eggs</li><li>Bread</li></ul>

This...

<?= ul(array('Milk', 'Eggs', 'Bread')) ?>

... becomes this...

<ul><li>Milk</li><li>Eggs</li><li>Bread</li></ul>

Plug and play <?php include 'header.html'; ?> <?php include 'navigation.php'; ?> Content <?php include 'footer.html'; ?> <?php include 'sidebar.html'; ?>

Want to learn more? http://php.net/manual/en http://phpcommunity.org irc://irc.freenode.net/phpc http://phpwomen.org irc://irc.freenode.net/phpwomen http://phpbuilder.com/board http://www.amazon.com/dp/0596005601

http://php.net/manual/en

http://phpcommunity.org

irc://irc.freenode.net/phpc

http://phpwomen.org

irc://irc.freenode.net/phpwomen

http://phpbuilder.com/board

http://www.amazon.com/dp/0596005601

To sum it up... Avoid copy and paste. Includes and functions! Before outputting variables, escape them! PHP has lots of built-in functions. Use them! Don't let your learning about PHP stop here!

Avoid copy and paste. Includes and functions!

Before outputting variables, escape them!

PHP has lots of built-in functions. Use them!

Don't let your learning about PHP stop here!

Questions?

Add a comment

Related presentations

Related pages

PHP Basics for Designers - HtmlCenter Blog

November 16, 2008 · PHP Tutorials · by HTMLCenter Team. The Atlanta Web Design Group ...
Read more

PHP Tutorials | Lynda.com

Library; PHP; PHP Training and Tutorials. Designed for intermediate users, our PHP tutorials can help you learn PHP inside and out. Find out how to use PHP ...
Read more

PHP for Web Designers | Lynda.com

PHP with MySQL Beyond the Basics Kevin Skoglund. 2,199,079 Views. Creating Secure PHP Websites ... You have completed PHP for Web Designers.
Read more

PHP Video Tutorials For Web Designers - KillerPHP.com

Beginners PHP tutorial for web designers. Menu . Home; PHP Video Tutorials; ... KillerPHP Newsletter. My PHP newsletter will let you know when any new ...
Read more

PHP Basics pt. 1 - WordPress 3.1 for Designers [Video]

PHP Basics pt. 1 by George Plumley - Selection from WordPress 3.1 for Designers [Video]
Read more

Beginners PHP - Killervideostore.com

Beginners PHP. Skill ... $39.99. Buy DVD! $39.99. Lifetime Access to Videos Learn the basics of the PHP programming language by ... Complete Web Designer;
Read more

phpDesigner 8 - PHP Editor & PHP IDE with built-in HTML5 ...

phpDesigner 8 is a fast PHP IDE and PHP editor with built-in HTML5 -, CSS3 - and JavaScript editors boosted with features to help you create amazing ...
Read more

Free Web Design Course For Beginners in 2016

In this massive free web design course you'll learn all the basics about graphic, web design theory and freelancing. Best place to start in 2016!
Read more

Web Design Basics

Tutorials on the basic skills of web site designers and designing effective web pages that are both simple and easy-to-use.
Read more