how to start with PHP

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
jonemuse
DCEmu Newbie
DCEmu Newbie
Posts: 3
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Mon Jan 18, 2010 9:58 am
Has thanked: 0
Been thanked: 0

how to start with PHP

Post by jonemuse »

Hi,
I know now a days PHP is coming in more use by the people.But My problem is I don't know PHP at all.So anybody there who can suggest me regarding to PHP tutorials or any link from which I will get everything and will be easy to learn.Any help is greatly appreciated.
Thanks in advance.
User avatar
not just souLLy now
DCEmu Respected
DCEmu Respected
Posts: 4070
Joined: Sun Jun 13, 2004 5:53 pm
Location: UK
Has thanked: 2 times
Been thanked: 3 times

Re: how to start with PHP

Post by not just souLLy now »

Whenever I've had a crack at php I've always found tutorials that assume you have at least some experience with using a similar language, so they're mostly a glossary of functions. W3C I'm looking at you.
With all of the security problems you can open yourself up to when using php/mysql, the fact that some functions are now depreciated or at least not considered best practice and without any idea of how to properly structure code, I've never managed to get past basic tutorials.
If there are better tutorials put there I'd be interested too.
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5666
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: how to start with PHP

Post by BlueCrab »

One thing you could try, if you want to learn PHP is to take a look around the internet for class notes for college courses where they taught it. For instance, I took a course last year at the university I go to that taught PHP (among other languages), and its notes are all online. Here's a link to them, in hopes that they might help someone:

http://userpages.umbc.edu/~dhood2/cours ... &topic=PHP

Unfortunately, we spent very little time on PHP, so there's not terribly much there, but it should be enough to get you started (and maybe make some of those indexes of functions make a bit more sense).
BlackAura
DC Developer
DC Developer
Posts: 9951
Joined: Sun Dec 30, 2001 9:02 am
Has thanked: 0
Been thanked: 1 time

Re: how to start with PHP

Post by BlackAura »

Any tutorials I've seen suck.

Basically, they tend to teach you how not to write decent PHP code. A good example would be teaching you to do database access like this:

Code: Select all

mysql_connect('localhost', 'username', 'password');
mysql_select_db('database');
$name = addslashes($_GET['name']);
$sql = "SELECT * FROM table WHERE name='$name';";
$results = mysql_query($sql);
if(!$results)
    die('Query failed - ' . mysql_error());

// Somewhere down the bottom of the page
echo "<table>";
while($row = mysql_fetch_row($results)) {
    echo "<tr>";
    for($i = 0; $i < count($row); $i++) {
        echo "<td>" . $row[$i] . "</td>";
    }
    echo "</tr>";
}
echo "</table>";
That's close to the kind of code you typically see in PHP tutorials, but that code sucks. It's using the (very) old way to access MySQL databases, it's incorrectly escaping URL parameters, it's building SQL statements inline (very bad idea), it's not separating the logic from the displayed page, and code written this way quickly becomes an unmaintainable mess.

What you should be doing is using a database abstraction of some sort. That abstraction should be using PDO (the newer way to connect to databases with PHP), and it should be using parameterized queries to prevent SQL injection attacks. The abstraction should be building the SQL for you in most of the cases. This makes the code easier to write, easier to read, less prone to mistakes, and much simpler to maintain.

Using while and for loops to iterate over the results is also a bad idea. It's much better to use the built-in iterators that were added in PHP 5, using the foreach keyword. That doesn't work with the old MySQL library either, so yet another reason not to use it.

You should also never be spitting out HTML manually like that. You should be using separate template files, or possibly even a whole different templating language like Smarty (which isn't necessary unless you want someone else to maintain or extend the templates, and you don't trust them with the PHP code). An example using iterators might look like:

Code: Select all

<table>
<? foreach($rows as $row) : ?>
    <tr>
    <? foreach($row as $column) : ?>
        <td><?=$column?></td>
    <? endforeach ?>
    </tr>
<? endforeach ?>
</table>
OK, it's just as long, but at least the PHP is much simpler, and doesn't need to call any functions.

Unfortunately, almost every tutorial I've seen doesn't teach this kind of stuff.

The best way to learn PHP is to take something that's already written, and go with that. Could be a framework, like Zend, CakePHP, Symphony or whatever - they're usually better documented than PHP itself, and push you towards doing things the more modern way. Since they usually contain database abstraction libraries, force code to fit inside the framework, and allow you to re-use components written by others, they can also shield you from a lot of the security issues, like SQL injection, or even some kinds of PHP code injection.

To learn the actual language itself, the PHP manual's actually pretty decent. It's only once it starts getting into all the extensions available that it becomes more of a function reference. It tells you what something does, but not why you'd use it.

I've actually been writing PHP web apps as (part of) my day job for several years. The code in those tutorials looks like old-style procedural Perl code, while most of the code I tend to write looks more like Java. Which someone has dropped a bucket of dollar symbols all over.
Post Reply