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).
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.
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!
