Learning PHP and MySQL
Okay, I'm not going to teach you PHP this is up to you. To start learning PHP, go through the w3schools tutorial. For reference use the online manual at php.net. Each page has programming examples, for example the str_len() function. If you get stuck, use a forum such as http://www.devnetwork.net/. Also download the free php 5.3 study guide.
You will also need to know SQL and learn MySql - PHP can communicate with several databases but MySql is the most popular with PHP. For reference the online manual is at http://dev.mysql.com/doc/. which also has examples, for example length(). And if you get stuck, use the forums http://forums.mysql.com/.
To run php scripts you'll basically need a web server. If you already have a web host (for example hostgator) then you can already run php scripts. Just create a text file on your computer, give it a .php extension and copy it to your website (either through the websites control panel or by using an ftp program such as filezilla)
For example the classic hello world program - copy this and save as helloworld.php, upload it to your web host and then call http://www.yourwebsite.com/helloworld.php
<?php echo '<p>Hello World</p>'; ?>
<?php echo '<p>Hello World</p>'; ?>
Installing a local web server
You can continue to use a remote web server, nothing wrong with that, but it does depend on having an internet connection and it is a live site so you need to be careful. For development, it would be better to install a local web server. Then its not so critical if you make a mistake, then transfer to your webhost when you are ready.
The main web server used for PHP development is Apache. This can be installed as part of a bundle called LAMP - which contains Linux, Apache, Mysql and PHP. There are bundles for Windows and Mac too, the most popular being XAMPP.
I got pretty fed up of developing in Windows for lots of reasons so I moved to Ubuntu (Linux) in 2008. I would strongly recommend switching to Ubuntu for PHP development. The main reasons are its quicker, its really easy to install stuff and best of all, its all free. Rather than make a complete jump, I installed Ubuntu on my laptop as a dual boot alongside Windows. But I very rarely use Windows now so its just eating disk space now. So for the rest of the blog, I will be assuming you are using Ubuntu or any other flavour of Linux.
To install LAMP on Ubuntu, just follow these instructions. Basically go to the terminal Applications -> Accessories -> Terminal and enter
sudo apt-get install apache2 php5-mysql libapache2-mod-php5 mysql-server
Make sure you make a note of the root password for mysql.
Once installed you can go to http://localhost to test your webserver is working - if it says "It Works!" then erm... it works :)
The default folder for Apache web files is /var/www - so the "It works!" display is in the file /var/www/index.html. You can create another html file and drop it into /var/www
To test that php is working, try copying the helloworld.php file into /var/www then calling http://localhost/helloworld.php.
You will also need phpmyadmin which is a great application for managing MySql. To install it, again from the terminal enter
sudo apt-get install phpmyadmin
You will then be able to access it from http://localhost/phpmyadmin.
Files, Folders and Permissions
I like to keep all my code in /var/www/projectname type folders. But the problem is a normal user doesn't have access to the directory by default. So I run the following commands from the /var/www directory. Linux purists will say its a terrible thing to do, but it works for me.
sudo chgrp -R www-data projectnamefolder && sudo chown -R myusername projectnamefolder && sudo chmod -R 775 projectnamefolder
Replace projectnamefolder with your website folder name and myusername with your username.
You can also have a local directory eg /home/username/public_html and do lots of wizardry things with Apache virtual host, but I'd rather be developing than messing with Apache.
You might need to make changes to the php.ini configuration file. This is in /etc/php5/apache2/php.ini. You'll need to edit the as root - for example
gksudo gedit /etc/php5/apache2/php.ini
Post a Comment