Functions and Variables

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
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Functions and Variables

Post by Straterra »

I have this code, where the variables are clearly defined, yet, the stupid function cannot read them. I get an undefined variable!!

Code: Select all

<?php
$dbname = "musicdatabase";//Set this to your database name
$cook = "sitemusic";//Set this to the cookie name
$table = "musictable";//Set this to the table name
$menupage = "pickmusic.php";//Set this to the page where the music menu will be present
$musicpage = "playmusic.php";//Set this to the page where the music will be played from
$addmusicpage = "addmusic.php";//Set this to the page where you can add music
$addmusicpageform = "addmusicform.php";//Set this to the form of where you can add music
$cookiepage = "cookie.php";//Set this to the page where it processes the data and puts it into a cookie
$deletemusicpage = "deletemusic.php";//Set this to the page where you can delete music
$deletemusicpageform = "deletemusicform.php";//Set this to the form of where you can delete music
$loop = "true";//If you want the music to play foreever, then make this true. If not, then set it to false
$autostart = "true";//If you want the music to play automatically, then set this to true. If you don't, set it to false
$visible = "true";//Keep this true if you want to allow the user to stop, pause and restart the song. BEWARE, if this is set to false, many people will NOT like your site, as it does not allow the user to disable the music. Also, the only way to disable the music when this value is set to false, is to stop the page. By stopping it, you also stop all animated images (.gif) and perhaps maybe even stop your Javascript
function playmusic() {
if ( isset($_COOKIE[$cook]) == true ) {//Tests to see if the cookie exists
$cookiename = $_COOKIE[$cook];//Assigns the cookie into an easier to manage variable
if ( $cookiename == "none" ) {
return "<center>You have selected to have no music. <br>";//Displays if the person has selected to not play any music.
} else {
if ($db = sqlite_open($dbname, 0666, $sqliteerror)){//Tries to open the database.
$result = sqlite_query($db, "select * from $table where cookiename = '$cookiename'");//Selects everything from the table.
$result = sqlite_fetch_array($result);
return '<center> <embed src="'.stripslashes($result['url']).'" visible="'.$visible.'" autostart="'.$autostart.'" loop="'.$loop.'"><br>Click <a href="'.$menupage.'">here</a> to pick another song.';
} else {
return ($sqliteerror);//If the database could not be opened, it will return this error
}//Closes the database opening if statement (By default, its on line 
}//Closes the  if statement that checks to see if the cookie contains the value "none" (By default, its on line 5)
} elseif ( isset($_COOKIE[$cook]) == false ) {
return header('Location: '.$menupage);//Redirects the user to pick a song, if they didn't before.
}//Closes the cookie checking if statement (By default, its on line 3)
}
?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

The variables are declared outside the function and therefore arn't in scope inside the function. So either define the variables inside the function, pass the variables to the function or make them global. I'd say making them global is the simplest solution for the time being whilst you work out how scoping works ;)

E.g

Code: Select all

$foo = 'hello';
$bar = 'goodbye';
function test(){
    global $foo, $bar;  //this is the bit your code is missing
    echo $foo; echo $bar;
}
Post Reply