by pavs · 13y ago · 0 comments
Can anyone give me any tips?
(please don't suggest other programming language, I have wasted countless hours trying to decide. I will eventually try to learn other languages but decided to start with php for now. My main interest is webapps, so it would seem that php is the most popular - and easy - choice.)
I personally try to follow a couple basic rules. .php files should be a single <?php ?> block with nothing outside it, and .phtml files shouldn't use anything but if, foreach, and already defined variables. You'll end up with code that's both cleaner and better structured.
Second, if at all possible avoid Wordpress until you've already got a firm grasp of the language. Wordpress itself has a few decisions that made sense in PHP 4 but are terrible practice now (like using global functions instead of classes) and a few others that, while not technically wrong, were questionable even then (the loop). On top of that, the quality of code in third party themes and plugins varies wildly. If you copy the techniques you see there, you'll learn some awesome tricks, but you'll also learn every bad practice in the book. Until you've got enough experience to recognize the difference, you'll do yourself more harm than good.
Unless things have changed since I last looked, PHP files are all templates. Last time I used PHP, with symphony, I actually thought PHP was a pretty reasonable templating system.
A lot of code written by beginners (myself included, once) will put business logic as close to where it will eventually be rendered as possible, so you end up with code that looks like this
<ul>
<?php
// Thirty lines of database calls and string manipulation
foreach ($results as $result): ?>
<li><?= $result ?></li>
<?php endforeach ?>
</ul>
which makes intuitive sense but quickly leads to an unreadable template. It turns out that, even though you're writing these together, you'll rarely actually want to change them together. So you eventually learn to have template files and nontemplate files.The .php vs .phtml distinction has no relevance to the compiler. They're only really useful to a human in making that template/code distinction explicit.
https://github.com/symfony/Process/blob/master/PhpProcess.ph...
Or in other words, php files are only templates if you use them that way.
Having said which, I'm confused by the lack of a closing ?>. Maybe PHP's parser lets you get away with that though.
- Get into the habit of using PHPUnit early. If you haven't come across it yet there are plenty of tutorials on http://net.tutsplus.com/ that cover it's basic use. It may seem pointless to begin with but you'll be thankful later on.
- I know you said you didn't want suggestions for another language and I won't give you any. However alongside PHP I think it would be really beneficial to do a primer on basic computer science. An understanding of concepts such as language paradigms, design patterns and simple algorithms would inform your learning and explain why some of the advanced resources you come across later do things the way that they do.
Without you being more specific, I wouldn't know what to say other than build simple things with it, then more complex things, and keep looking at source of mature projects for inspiration and kicks. Also, modify existing things until they break, then do it again. As long as you're not dealing with sensitive data of yourself or others, don't be afraid of to just poke and pull at things, or to try an idea just to see if it works. Maybe some would advise against that, and maybe they're right, I am not a good programmer by any means. But I have fun doing it, and that's how I learnt.
Learning grammar and vocabulary is obviously the first step, but I think the real fun begins when you learn to express yourself freely, when the path between idea and execution gets shorter, and is less often interrupted by hunting down unexpected bugs. Even though that is possible without knowing all that much of the language, just by getting good at using the bits you do know, it still takes time in any case, and you will have to overcome frustrations. But no matter what language, it's worth it :)
Comment religiously. You'll thank yourself in six months.
You'll save yourself a lot of time if you learn to use Composer (http://getcomposer.org) to manage dependencies, and a templating system (twig, mustache, smarty, what have you) and class autoloading. PSR-0 (https://github.com/php-fig/fig-standards/blob/master/accepte...) is annoying, but it is really convenient once you get past the overhead. If you're on Windows, it really helps to get PHP running from the command line.
The bulk of my php coding really sits, in foreach loops, if statements (and comparison operators), and the use of arrays. Get to know arrays, build them, loop through them, echo out values, mutate them, they're your friend.
Learn about namespacing, functions and classes. Avoid globals but use the superglobals!
And as with all web programming, understand the browser/server lifecycle of a request. Start with the basics of HTTP.
google for REPL, than get one for php, so you can see results instantly. https://github.com/d11wtq/boris