Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

What exactly is php?
Welcome, Guest. Please login or register.
December 01, 2008, 09:10:58 PM
11304 Posts in 1248 Topics by 496 Members
Latest Member: teentiodo
Experts Round Table Network  |  Serverside Technology  |  PHP  |  What exactly is php? « previous next »
Pages: [1]
Author Topic: What exactly is php?  (Read 811 times)
rpgfreak

Offline Offline

Posts: 3


« on: January 18, 2006, 10:42:31 PM »

Hello, I am semi-new to html and css. I was wondering what php was, what it is used for, and what advantages/disadvantages it has compared to html.
Logged
Zyloch
Site Builder

Offline Offline

Posts: 8


« Reply #1 on: January 18, 2006, 11:46:52 PM »

Hello, and welcome to ERT :-)

PHP is an acronym for PHP: Hypertext Preprocessor. The PHP website gives this short blurb about PHP:

Quote from: "www.php.net"
PHP  is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.


What does this mean? Well, PHP is a scripting language. If you are familiar with client-side scripting for your webpage, such as Javascript or VBScript, you will know what a scripting language is. You are able to make choices, with if clauses and switch statements and use loops (three common types: while, for, do-while).

HTML, on the other hand, is a markup language. This means that it is used to describe information. If you recall, HTML tags are static and unchanging. Once you have a form on the page, it will stay on the page regardless (notice that all tags in HTML describe the presentation of the page, such as <b></b> or <br /> or even <form></form>, as is characteristic of a markup language). You cannot make decisions about whether to include the form or not (for instance, check whether the user is logged-in or not).

The situation can be partially remedied with Javascript. Yes, using the DOM, you can hide a form with CSS. But Javascript support can be turned off, and a very few people may still be using legacy browsers. Furthermore, every page load will require the form in the code viewed by the user, unless you redirect to another page. In the case of logins, this makes it extremely difficult to secure the page--what if someone turns off Javascript or does View Source? Can we do better?

Of course! We can use a server-side language, such as PHP. If you remember nothing else from this post, remember that PHP is server-side. This means that it is processed by the server, before it is shipped out to the client. Normally, the client requests a page from the server, and receives it. Javascript is an example of a client-side language, and is shipped out by the server to the client. It is the responsibility of the client/user's browser to display the Javascript correctly (and why it can be turned off as well).

In the case of a login scenario, you will need to ship to the client the detection code to see if someone is logged in. It is then the client/user's responsibility to execute that code. You need to hand the user the information for them to work with. This makes for a very insecure system.

If we use PHP, we can have the server process the code beforehand. This even is represented in the name: Hypertext Preprocessor. If you have any previous experience with a language such as C/C++, you will know that the preprocessor runs before the code itself is compiled. The same with PHP. The client requests a PHP page from the server. The server then runs that PHP page, running all the if/else clauses, loops, etc. before returning to the client ONLY the information that it needs.

The best way of seeing this is with an example:

Bob is a malicious user who wants to hack our site. He knows the admin panel is at www.oursite.org/admin.php. Being a rather smart fellow, Bob decides to skip the login page and go directly to admin.php. Should work? Of course not :)

If we program our admin.php page correctly, Bob should not have access. We can do something like this:

Code:
<?php
session_start();

if (true === $_SESSION['b_logged_in'])
{
    //Welcome back!
    echo('<b>You are logged in!</b>');
}
else
{
    //Not logged in? Redirect to the login page
    header('Location: login.php');
}

?>


What does this snippet do? It basically checks to see if Bob is logged in or not. If he is not, in the else clause, we see a header() statement. This redirects the page to the login page. The code checks for login with a session cookie. This is a particularly useful aspect of server-side languages and PHP has a particularly large support base for it. A quick Google or a more specific question here will give you a better response (I don't want to get too off-topic!).

What happens is Bob goes to admin.php, the server will load the page, and run the code. It checks if $_SESSION['b_logged_in'] is true. If it is, we're logged in, and we display the data. Notice that what is in the echo() statement is just HTML! PHP can give you choices on when to output what HTML, simple as that.

The description PHP.net gives mentions embedding inside HTML. That can be done too (as long as you have a server with PHP installed, and give you files the right extension, most of the time '.php'), for example:

Code:
<html>
<head>
<title>
<?php

echo('You came from ' . $_SERVER['HTTP_REFERER']);

?>
</title>
</head>
<body>
Look at the title!
</body>
</html>


What this example page does is print out the page you came from in the title of the website! As you see, PHP can also be embedded inside the HTML. And why not? It outputs HTML anyways :-)

If you look at the code ERT is coded in, it will be PHP. In fact, as of now, ERT is using phpBB forums, which is made entirely in PHP, and is amazingly powerful. Forums would be impossible without server-side languages.

However, keep in mind that PHP is not the only server-side language. There is ASP and JSP as well just to name two. However, PHP does have a large support base, and built to be easy to program in, and that makes it attractive to many programmers alike.

Enjoy your trip into this world, and you will discover so much you can do with PHP! Ultimately, when you come out of it, you will be a much more knowledgeable website programmer, and will understand how to connect to databases, create secure login sites, work with session cookies, and even modify images on the fly! The opportunities are endless.

If you want, we can help you with that journey ;-)
Logged

Ted
nicholassolutions
Administrator
*
Offline Offline

Posts: 133



WWW
« Reply #2 on: January 19, 2006, 01:09:42 AM »

Great answer Zyloch  =D>

Just wanted to add a few things about what else PHP can do that you cannot do with client-side scripting or plain HTML, and that are often more difficult in other languages such as perl (not that I'd ever knock perl!):
[list=1]
  • Send and recieve/process email
  • store and retrieve data from databases (PHP's database support is especially good).
  • read and create files on the server on the fly, including files such as PDFs, images, zip files, and MS Word documents.
  • process user input from website forms in virtually any way you can think of
  • gather data from other sites. A nice example of this is the news feeds on our homepage. COBOLdinosaur wrote a PHP script that the server runs every hour. The script gathers all the latest news headlines from various sites, and stores them in files so that we can display the news on ERT.
  • send data to other sites
  • output serverside languages like javascript and markup languages like HTML and XML (Zyloch described this above).
  • Track user activity on your site and restrict access to certain areas (Zyloch covered this too)
  • In general, create dynamic content of all types and make webpages interactive.
  • Although not originally designed for this, PHP may also be used purely as a command line tool for performing tasks on your server like backup operations, mail sorting, spam filtering, etc.
  • [/list:o]
    Roughly speaking, HTML is used to control how a webpage
looks, and scripting is used to determine what a webpage does. Many of the things on the list above just can't be done with client-side languages alone, and that is where serverside languages come in. If you're looking for a serverside language, PHP offers a great deal of power without too steep a learning curve, and I'd highly recommend learning about it.

Here's a link to a book I always recommend to beginning PHP programmers (it's a free online book, but it has recently been published in hard copy as well):
http://www.hudzilla.org/phpbook/read.php/2_0_0
That page has some additional discussion of what exactly PHP is, and it will also teach you how to program in PHP if you are interested. www.php.net is another excellent resource - one of the best things about PHP is its excellent documentation.

And of course, ERT is always a great place to come when you have questions :wink:
Logged
Anonymous
Guest
« Reply #3 on: January 19, 2006, 01:50:05 AM »

As mentioned above, PHP can be used to used to create command line programs. Even GUI applications!

Modern versions of PHP come in many flavours. Which flavour depends upon usage. ISAPI for loading as a server based module for web servers. CLI for command line variants. CGI for web server Common Gateway Interface. Or even as source code for compiling directly into the web server (a popular method for Unix/Liunux servers using Apache Web Server).

For developing GUI applications with PHP, PHP-GTK can be used. GTK is a toolkit to allow the creation of GUI applications. PHP-GTK is some clever software that connects PHP scripts to the GTK to allow you to write once, run anywhere your PHP code. GTK is supported on many platforms and therefore your PHP code developed on a Windows machine can be ran (normally without ANY change) on Unix, Max, BEOS, etc (whatever supports PHP and GTK!). The primary downside to the GTK is that the application doesn't look native. For some people this is significant.

Alternatives for creating Windows-only GUI's also exist (WinBinder being one that comes to mind).

As a Windows developer, one of the most useful things I have found with PHP is the ability to create Windows services.

These are mini applications which run all the time without the need to have a user manually start them or even require the computer to be logged on (just turned on).

Now I hope you are asking why on earth would you go to all the hassle of writing code for services in PHP or GUI applications in PHP?

The main answer is code reusability. If I create a class which can take multipage TIFF file and convert this to multiple single page TIFF files, then I can use the same code in any environment:
  • Web based - A user uploads a file via a web form for processing
  • CLI based - A sysop wants to convert a directory full of multi-page TIFFs
  • Service - The service watches a directory and converts the TIFFs when they become available
  • GUI based - A intranet user wants to drag/drop the TIFFs into the application


The physical code written to do the conversion is written once. The code to support the input mechanism is unique to each platform, but even that can be managed inside a single script. Theoretically, I could actually have a single source code file running in all of these ways!

If I upgrade that single source-code file, then ALL the instances gain the benefit. I don't need to recompile 3 different versions of the code (or more if I want to support multiple platforms using different processors). Write Once, Use Anywhere.
Logged
COBOLdinosaur
ERT.com Admin

Offline Offline

Posts: 481



WWW
« Reply #4 on: January 19, 2006, 05:44:57 AM »

I'm going to add one little piece in here.

Just in case you are thinking that it sounds difficult.  Four months ago when we started this project, I did not knwo any PHP at all.  Now I am able to do the coding to generate the pages being used on the content side, modify the forum code and use php scripting on the backend of the site for administrative and utility tasks.

The people I had available to help and teach me are right here at ERT, so I would say anyone doing Web development probably can get most of the support they need right here from our team of PHP Mentors.
Logged
nicholassolutions
Administrator
*
Offline Offline

Posts: 133



WWW
« Reply #5 on: January 19, 2006, 08:53:12 AM »

I forgot about TK! It's actually a language in itself, so if you learn to do it in PHP, you will be able to write graphical interfaces in other languages (such as Perl) with little extra effort. For instance, I have a GUI program I wrote in Perl/Tk that I use to generate the HTML for all my ebay auctions  :wink:

Speaking of other languages, PHP is also great as a 'glue' to stick other programs together. For instance, say you have a program you've been working on in C, and you have a shell script that completes some other task. You can use PHP to run each of these (maybe with some data processing inbetween) and pass data back and forth between them, instead of having to rewrite the whole thing in one language (which may be very hard). This can save you quite a bit of time and effort and help you work more efficiently by using each language where it is strongest (and where you are most comfortable using it).
Logged
rpgfreak

Offline Offline

Posts: 3


« Reply #6 on: January 19, 2006, 12:39:19 PM »

Thank you everyone for all the information you gave me.
Logged
Anonymous
Guest
« Reply #7 on: January 20, 2006, 01:20:34 AM »

And to add some more info ...

PHP is Open Source. Effectively this means that there are a LOT of people contributing to the language. Not just developers USING PHP, but developers BUILDING PHP.

PHP is also extendable. Third party add-ons are available. Sometimes with source, sometimes just as binaries.

The standard distribution of PHP comes with 46 extensions on top of a LOT of functionality built into PHP.

There is a recognised library of PHP extensions (called PECL - PHP Extension Community Library). The standard distribution of PECL contains around 60 extensions.

And these are just the ones whose maintenance is coordinated within the PHP development structures.

Then there is PHP code libraries (PEAR - PHP Extension and Application Repository). This is a library of code developed along a specific coding standard providing additional functionality in a consistent and controlled way. This code is written in PHP and is a very good start point to see how you can write PHP code. Well structured. Well documented. Not to everyone's taste though.

The great thing about all of this is that YOU can contribute, either in keeping documentation uptodate, bug fixing the PHP source code or creating your own PECL/PEAR extensions! No one will mind. Sure you will be peer-reviwed and this may seem quite daunting, but it is a community project. Just like Experts Round Table. And it is "A Good Thing" (!tm).
Logged
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #8 on: January 29, 2006, 10:08:23 AM »

Yo ;-)

I would also add thatdespite what is said now at www.php.net to get a respectability, PHP stand once upon a time for Pretty Home Page

in short, it's a server-side language that will generate the client-side HTML you already know.

It makes the step between static HTML - the one you know - and dynamic HTML.

As a very short start-of-tutorial, changing a static HTML page into a dynamic HTML page is just a matter of adding :
<?php
echo <<<EOF

at the start and
EOF;
?>

at the end ;-))

then you will begin introducing dynamic elements...
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
Pages: [1]
« previous next »
    Jump to: