PHP Basics
Greetings, all.
I was directed here by a wise gentleman (gentlewoman? who knows) at the webmasterworld forum, who said this forum was a great place to learn the basics of PHP. I was told (so don't blame me) that newbie questions were the best ones, in which case you will find that I am the best poster you've ever had here...I literally know nothing about php, other than that it is a way to seperate content and structure (then layout and appearance from that with CSS). I seriously need this sort of control in my life. I currently code to validate in html401 strict and use css for all my layouts, but at the moment I have to hand code (or cut and paste) every page, with content. It's laborious, and getting old fast. So, newbie question number one: what are the minimum requirements for php? what's the basic theory behind it? what do I absolutely NEEd to know before I start diving into learning code? Thanks in advance for any responses. Back to top |
|||||
First steps to learning PHP
Hi createErrorMessage, those are good questions, we'll see if we can get you started, you have the right idea on how to approach a new subject.
First steps The very first thing you need is a server that supports php, basically all linux/unix web servers have php/apache/mysql installed. PHP is first and foremost a web scripting programming language - scripted languages means that the programming is interpreted by some other system at run time, javascript for example is interpreted by your browser when the page loads, php is interpreted by the server when the page is requested, but before the page is sent to the browser from the server. To see if you have php installed on your webserver, let's start with the most basic programming thing , where all stuff starts: In your text editor (having one that does syntax highlighting is really worthwhile, it makes code a lot easier to read, I use editplus, that costs $30, you can get HTMLKit for free, it's not my favorite, it's too cluttered, but it is free.) You can use MS notepad, but it's a pain saving non .txt extensions in that, you can do it, all ascii text is the same, doesn't matter what editor you use to make it, programs like ms word or wordpad can cause problems because they add extra stuff to the text that you don't want in there. anyway, type in the following code: :: Code ::
<?php echo 'hello world'; ?> Then save this file as 'test.php' Upload the file in ASCII mode with your ftp client to your website, assuming you have one. Load it into the root folder. Now use your browser, go to your website, type in 'www.<put in your yourdomain name here>.com/test.php' You should see a white screen with the words 'hello world' on it. If you see this, :: Code :: hello worldyou have php support. If you see exactly the code you uploaded, including the <?php stuff, you don't have php support. Most windows servers don't offer php support, although you can actually run php on IIS if you absolutely must, but it's a bad idea in general, apache just works better, supports things like .htaccess which are really powerful tools. How PHP works PHP must have two things to work: a webserver that support php, like apache, and the php processor. If you are missing either all you will see when you run a php page is the php code itself, it will be treated as normal html/text, the browser will display it like it would display any other text it doesn't recognize as html. In the above example, you'll notice that the php code is contained in brackets, like this: <?php ...... ?> All php on your page must be contained in that kind of declaration, it's the same thing as using this type of construction to contain a javascript statement, in this case the same one as above, only with javascript syntax instead of php syntax. :: Code :: <script type="text/javascript">document.write('hello world');</script>What happens there is two things: 1: by giving your file name the extension php, you've told apache webserver to bring in the php engine for that page. 2: Once the page is in the hands of the php processor, it will execute any code that it finds between the <?php and the ?> Don't skip the 'php' in that, it's a bad habit some people have, start out doing it right and you won't have to break any bad habits in the future. Another good habit to get into is to always end every single statement with ';' [a semicolon, that is]. This tells the php parser that it has reached the end of that particular statement. On single line things it isn't necessary, but it's smart to get into good programming habits as soon as possible, it makes debugging code much easier. Now to see what happens when you code something wrong, or make a typo, create this file: :: Code ::
<?php -echo 'hello world'; ?> Upload that, and you should get a parse error on line 2 of the file, php has pretty good error detection. Debugging is part of programming, so the sooner you learn to recognize various error codes, the better, but that will come soon enough, trust me. Now, there's a problem here: in order for you to run your pages as php, you need to change all their extensions to php, which ruins all inbound links, messes with your search engine placement, and in general just doesn't look that good, it's better to keep this processing stuff behind the scenes and not confuse visitors with different file extensions, they all know and understand .htm or .html. There's a great way to solve this problem if you have apache webserver, you can find how to use htaccess to run .htm files as .php files in that thread. That's a really great technique, this site is actually the only site we do that uses php extensions, all the other sites run as .htm being parsed by php. Of course, last but not least, what does PHP mean? In 1995 Rasmus Lerdorf started with PHP 1, it meant: Personal Home Page tools. In 1998 PHP 3 was released, and really started picking up steam. Amazing growth for what started as a way to add server side scripting support to home pages. Of course, open source has a sense of humor, so now PHP generally is said to mean: PHP Hypertext Preprocessor, or PHP. That's in the GNU tradition, where GNU means GNU is Not Unix, if I remember right. Because it's open source, it tends to work the way you want it to, it's written by web people to do web things, there will generally be almost nothing you can think of to do with it that hasn't already been thought up and added as a feature, from image processing, pdf creation, database programming, to full site/blog/forum projects like the one that runs this forum, PHPBB. If you really want to do yourself a favor, setup up mysql, apache, and php on your home development computer, that makes development a breeze, but for now using your web hosting server will work fine. Now that you have the utter basics down, feel free to ask more questions, and we'll try to deal with them one by one. PHP resources The open source web home of PHP is found at php.net. That's a good place to find an online function list, but it's not super great as a tutorial resource, although they do have tutorials too. Back to top |
|||||
:: Quote :: I currently code to validate in html401 strict and use css for all my layouts, but at the moment I have to hand code (or cut and paste) every page, with content. It's laborious, and getting old fast. PHP is ideal for this kind of thing. Keep in mind that php is going to produce your html for you, in one way or another, whether through simply including sections of your page, or actually writing the code out for you. PHP includes are probably the easiest way to get into using PHP to help manage your website, and are also the easiest to learn, since really all you are doing is cutting out a piece of your page HTML, say the header section, then saving it as say 'header.htm', then to include that on every page you simply include it, like this: :: Code :: <?php
include('header.htm'); ?> You can read more on how to set up PHP includes if you are interested in learning that. Or you can read the actual include documentation on php.net, which is a good resource to learn how to use, that's an online source for php functions and methods, always has a decent example given, plus user comments that might help clear up confusing issues. Have fun with PHP, Erik Back to top |
|||||
Wonderful.
This is exactly the 'primer' info I was looking for. PHP seems very similar to javaScript in it's syntax (semicolon end lines, parenthesis around function parameters, etc), which is good news for me since I'm already familiar with JS. 'Includes' are definitely what I'm looking for at the moment, so thanks for the link about them. I am in the process now of downloading and (trying to) setting up Apache, PHP and MySQL on my home computer so I can play around with PHP. Once this is done, and I start to tinker, I'm certain a plethora of questions will arise. Until then... Thanks for your help. It's great to know the spirit of open assistance is alive in another forum. Back to top |
|||||
Setting up MySQL, Apache, and PHP
When you set up PHP, Apache, and Mysql, make sure to read all of the documentation before beginning.
MySQL must be installed and running as a service before you install Apache, then install PHP. This stuff doesn't work out of the box when you install individual components, although that's the best way to do it. You have to go into the various configuration files to create some capabilities, but just make sure that MySql is running correctly before you go to the next step. It's quite a bit easier to get all this stuff running if your OS is either Windows 2000 or XP, I wouldn't advise trying it on windows ME or 98. It's also easier if your OS is on the default c:\ partition. If you have questions on the mysql or apache/php installation, please post them in the apache forum. Back to top |
|||||
All times are GMT - 8 Hours
|