Best way to learn PHP basics

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SharonB
Forum Newbie
Posts: 3
Joined: Sun Mar 30, 2008 4:15 pm

Best way to learn PHP basics

Post by SharonB »

Can anyone suggest the best way to go about learning some basic PHP coding?

I have recently begun work on a website and trying to take the easy option, being a newbie to website design, I purchased a template which is coded in PHP.

I am trying to customise my template to make it more original, using Dreamweaver as an editing tool. I can handle some of the very basic changes through trial and error etc but I would really like to gain some basic knowledge of PHP which would help me in this sort of task.

Any reccomendations of where to start?

Thanks.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Best way to learn PHP basics

Post by aceconcepts »

Hi,

I would recommend purchasing a beginners book http://www.wrox.com/WileyCDA/WroxTitle/ ... 57440.html

If you don't want to buy a book then there are some pretty good tutorials on the net.

What will you be doing?

I think some of the most common beginner's task would include variable management and passing them through the url.

Will you be using a database too?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Best way to learn PHP basics

Post by JAB Creations »

Some pointers....

PHP is a dynamic language where you're trying to take different bits of information from different places and merge them in to one place.

I think the first thing people should learn in PHP is how to use includes, everyone could benefit from only having to edit one file for their menu after all! It's very comfortable for those who greatly appreciate the ease of CSS (provided they haven't yet encountered IE). :mrgreen:

I would also recommend getting to know more about variables. For the longest time I didn't know a $variable could be accessed between files so long as it was not inside a function. If it is inside of a function you have to return it I think.

If you know anything about JavaScript then you're probably constantly using alert to figure out what you're working with. In PHP you'll want to use echo.

Code: Select all

echo 'hello';
echo $my_variable;
echo 'this variable = '.$myvariable
echo $this.$that.$everything_else;
Notice the usage of the period, it's a connector of sorts (sorry for the lack of the proper reference term).

A huge change and improvement in my work occurred after I learned how to do object oriented programming with PHP...

Code: Select all

class settings {
 private $audio;
public function set($name,$value) {$this->$name = $value;}
 public function get($name){return $this->$name;}
}
 
     if ($_GET['audio'] == "0") {$the_audio = 0; if (!headers_sent()) {setcookie('audio','0',time()+2592000,'/');}}
else if ($_GET['audio'] == "1") {$the_audio = 1; if (!headers_sent()) {setcookie('audio','1',time()+2592000,'/');}}
else if ($_GET['audio'] == "2") {$the_audio = 2; if (!headers_sent()) {setcookie('audio','2',time()+2592000,'/');}}
else if ($_COOKIE['audio'] == "0") {$the_audio = 0;}
else if ($_COOKIE['audio'] == "1") {$the_audio = 1;}
else if ($_COOKIE['audio'] == "2") {$the_audio = 2;}
else if (isset($_GET['audio'])) {$error = audio;}
else {$the_audio = 0;}
 
     if ($_GET['audio'] == "0") {$the_audio = 0; if (!headers_sent()) {setcookie('audio','0',time()+2592000,'/');}}
else if ($_GET['audio'] == "1") {$the_audio = 1; if (!headers_sent()) {setcookie('audio','1',time()+2592000,'/');}}
else if ($_GET['audio'] == "2") {$the_audio = 2; if (!headers_sent()) {setcookie('audio','2',time()+2592000,'/');}}
else if ($_COOKIE['audio'] == "0") {$the_audio = 0;}
else if ($_COOKIE['audio'] == "1") {$the_audio = 1;}
else if ($_COOKIE['audio'] == "2") {$the_audio = 2;}
else if (isset($_GET['audio'])) {$error = audio;}
else {$the_audio = 0;}
 
$settings = new settings();
$settings->set('audio',$the_audio);
 
// use as...
// $settings->get('audio')
 
if ($settings->get('audio') == "0") {echo 'audio disabled';}
if ($settings->get('audio') == "1") {echo 'audio = lofi';}
if ($settings->get('audio') == "2") {echo 'audio = hifi';}
If you can grasp PHP classes your code will be much cleaner and easier to maintain over the long term. I'd recommend saving your classes in a header-class.php file.

It's referred to as object oriented because your procedural programming occurs only once and you merely reference the class in the rest of your PHP. Otherwise if you change your procedural programming you'll have to rewrite or heavily modify it in all instances across all of your PHP. Duplicate the audio class with something and try to echo it.

...and most importantly when you ask a question ask it as specifically as you can. You will never find me posting something like, "So hey guys how do I build a website that will make me a million dollars in a week?" or "How do I make a database?" Reading before asking a question shows you've made an effort. :D

Also you can usually reference things in PHP by looking them up on php.net/item such as php.net/echo.

Best of luck! You have it if you've ended up here for help with PHP. The folk here really know their stuff! :mrgreen:
Post Reply