PDA

View Full Version : Uses of PHP?



Anne Arbor
06-12-2011, 08:08 PM
I've been struggling to master PHP on and off for quite a few months now, with relatively little success. :(

It occurs to me that it might help if I had a better idea of PHP's uses. My impression is that PHP is mainly used with respect to databases. That is, one uses it to enter data and then to retrieve the data in various useful combinations. It's also used to edit and delete data, but those are secondary uses, so to speak.

Since the Web is made up of databases, to such a very large extent, that does cover quite a lot of territory.

I'm wondering, though, are there other 'big' uses of PHP that I'm overlooking?


- - -

On edit: I found this page (http://www.daaq.net/old/php/index.php?page=uses+of+php&parent=what+is+php) which lists some other uses of PHP. I don't really understand them, perhaps just because I haven't seen them in action.

Would the 'other' uses described be somewhat rarer? or would they be considered more 'advanced'?

I guess my question is: when I'm trying to learn PHP, what am I learning? Mostly, I guess, how to set up and manage a database-driven Web page? That was my original wish, and is still my primary need.

.

james438
06-12-2011, 08:49 PM
I think what would help you here is if you had a few examples of what php can do.


It can be used to create a forum for users to visit.
It can be used to display a quote on your website that changes to the next quote in a list every day at midnight.
You can create an upload script that can upload an image to your site and automatically crop it, create a thumbnail image, resize it and make it semi-transparent.
You can create a content management system with easy to use controls to determine aspects of your site like how many of your most recently updated articles to display on your mainpage at one time.
You can create a script to search your site.
You can record who visits your site and where they are coming from or who has links to your site.
you can send and receive email.
create private files only viewable by those with the correct password.


You can get much more advanced with all of the above. These are just some of the ideas that come to mind.

traq
06-12-2011, 10:08 PM
specifically, PHP's job is to write HTML. Yeah, there's lots of cool stuff it can do on the way there, and the possibilities go far beyond includes, mysql, and mail. but the end goal is to write a webpage. keeping this in mind will be very, very, very helpful as you learn.

you do not have a webpage that you insert php code into: you have php code that gives you a webpage when it's done running. As soon as you use php, the entire page is a php script - the plain html parts are just stuff you don't do anything to before your script outputs it.

If there was one thing I would change about how I learned php, that would be it. my advice is to separate your php from your html as much as possible. Do all your database calls and processing first. use $variables to save your html markup instead of echoing everything as you build it. Your aim should be to output no html - nothing at all - until the very end of your php script. doing so will also allow you to get into higher level functions later on without having to completely re-learn how to code.

an example:

instead of doing this -

// [...]
$result = mysql_query("SELECT * FROM `table`");
while($row = mysql_fetch_array($result)){
echo 'Row 1 value: ';
echo $row[0];
echo '<br>';
// etc., etc.
}do this -
// [...]
$result = mysql_query("SELECT * FROM `table`");
$info = ''; // you're going to put all your markup here
while($row = mysql_fetch_array($result)){
$info .= 'Row 1 value: ';
$info .= $row[0];
$info .= '<br>';
}
echo $info;

have fun!

djr33
06-13-2011, 01:23 AM
As a brief addition, what I like about PHP is that it makes my job writing HTML easier. And it adds some extra functions. Basically it makes it faster and more powerful.

If you want to have a list of similar things, you can generate it using a loop in PHP rather than hand coding each. Or if you want to use a database, send email, and do other interaction type things, you can also do it in PHP.


On the other hand, there is one major thing PHP can't do: it can't interact in real time with the user. That must be done using Javascript. (Or flash.)
If you really need this and Javascript alone won't be enough (such as for a chat room) you can look into Ajax.


I find PHP to be a relatively easy language and with practice you'll get there. That doesn't mean it will be tomorrow. For me, after about 8 years of just casually using it (sometimes more seriously than others) now I'm good at PHP. And if you spend more time than I did using it, you'll get there faster.

Anne Arbor
06-13-2011, 09:50 PM
I think what would help you here is if you had a few examples of what php can do.

* * *


James, thank you for your list. Since I'm trying to understand the bigger categories of what PHP can do, I guess I'd add that one can add good stuff to one's site and get various kinds of information about the site (visitors, search = users, content). Those are definitely different than what I had been contemplating.

Also, I enjoyed looking at your own website and reading some of your progress notes over the years. It's inspiring to see how much you have learned and all the different things you have implemented!

Thank you again. :)

Anne Arbor
06-13-2011, 09:57 PM
specifically, PHP's job is to write HTML. Yeah, there's lots of cool stuff it can do on the way there, and the possibilities go far beyond includes, mysql, and mail. but the end goal is to write a webpage. keeping this in mind will be very, very, very helpful as you learn.

* * *

Adrian, thank you very much for your reply. I really appreciate it that you took the time and trouble to spell out your thoughts and, expecially, give examples.

I'm not sure I'll be able to take full advantage of your advice . . . I'm a little worried that it might be one of those things that you can't understand when it would really do the most good? sort of the way "youth is wasted on the young"? (that the inexperienced might be too inexperienced to take full advantage of your experience?)

What you've said is intriguing, though, and I'll keep mulling it over. Maybe something will sink in!

Thank you again for your help. :)

.

Anne Arbor
06-13-2011, 10:06 PM
* * *
I find PHP to be a relatively easy language and with practice you'll get there. That doesn't mean it will be tomorrow. For me, after about 8 years of just casually using it (sometimes more seriously than others) now I'm good at PHP. And if you spend more time than I did using it, you'll get there faster.

Daniel, I'm encouraged to learn that it took you some time to become proficient in PHP. I've had this idea that somehow I should be able to pick it up fast and painlessly -- and I think that idea has caused me a lot of pain.

In my current educational iteration, I'm taking it slower and am trying to be more forgiving of my errors and stumbling.

My biggest error in the past has been, it seems, that I had the notion that one could learn PHP (or whatever) by reading about it. My lesson of the past couple of weeks seems to be that only using it really has an impact. One hour of use is probably worth about five hours of reading. (more?)

Thank you very much for your reply. :)

.

djr33
06-13-2011, 10:17 PM
If you don't know where to start, reading about it is fine. But personally I prefer to learn by doing. The only tutorial about PHP that I ever took seriously was a MySQL/PHP tutorial (one that I recommend -- php-mysql-tutorial.com), and everything else was either just trial and error or reading comments here or other places but not in any organize tutorial.

Of course that requires time for trial and error. For me, I was originally happy just making very basic scripts that didn't really do much looking back at them now. Starting with too big a project can be a good way to get in too far before you're ready. That's also another way to approach it, but don't be overwhelmed by it. Start with smaller things (or smaller pieces of larger projects) and just keep testing out code. Sooner or later it will start to make sense.


As for tutorials, if you aren't familiar with the fundamentals of programming (ifs, loops, and that sort of thing) then reading about them is a good place to start, but just at the very beginning. Hands on work will soon be more useful.

traq
06-13-2011, 11:55 PM
I'm not sure I'll be able to take full advantage of your advice . . . I'm a little worried that it might be one of those things that you can't understand when it would really do the most good?

well, you're right; when you're still learning, it can be hard to see why it would make much difference one way or the other.

The first situation most people encounter is when they start using sessions: You can't start a session if there has already been output to the browser (that's why everyone tells you to put session_start() at the very top of your script). waiting till your script is done to echo the result solves this problem, along with any other header() functions you may start using someday.

another example would be in a Content Management System - you can start building the page markup right away, in whatever order is convenient for the script, and you don't have to worry about whether or not some following piece of content will necessitate a change to that code, require a special header, or even completely change what content you need to return.

you might compare it to writing an essay in school - would you rather turn in your work word-by-word, or wait until you've finished the whole thing?

in larger scripts, it also contributes to maintainability and troubleshooting - all of your html is in one place, all of this function in another, all of that function over here, etc..

.......................

If all you want to do is spice up your html, using the more basic php functions, then you don't need to worry too much. but if you really want to see what php has to offer and see what you can end up building, then it's worth it to try to build those clean coding habits early on.

james438
06-14-2011, 01:10 AM
Also, I enjoyed looking at your own website and reading some of your progress notes over the years. It's inspiring to see how much you have learned and all the different things you have implemented!

Thank you again. :)
That was a very nice compliment, thank you :)

Anne Arbor
06-15-2011, 05:27 PM
Returning to my original question, there is quite a good article on 15 representative uses of PHP here (http://net.tutsplus.com/tutorials/php/15-wonderfully-creative-uses-for-php/). It begins with the database applications that I began with, but then goes on to list some of the uses that James mentioned and that Daniel alluded to.

Adrian has addressed more the question of good practices in learning, and Daniel has touched on that also. I wish that there were more comments along those lines, here and in other forums. It's so helpful to have the encouragement of other's people's example and the benefits of their experience.

Gentlemen, thank you again.