Rich464 Posted July 21, 2006 Share Posted July 21, 2006 I was wondering if someone could help me with what is likely a simple variable scope problem.as per my code below, variables declared in an include file arent available inside functions that I declare the include from. Simplified version is below. Now I've read the PHP manual and searched the net on variable scope and I understand most of it, but none seem to cover this exact situation.It's a silly thing, and if someone could help me with it I'd really appreciate it, I'm using php 5 and MySQL 5[code]config.php<?php$user = "user"; $pass = "pass";?>display.php<?phpinclude ('config.php');echo $user; //these seem to work fineecho $pass; //values are displayed correctlyfunction display_vars(){ echo $user; //these give undefined variable warnings echo $pass; //have tried using global too}display_vars() ?>[/code]Cheers in advance :-* Link to comment https://forums.phpfreaks.com/topic/15264-variable-scope-problem/ Share on other sites More sharing options...
spyke01 Posted July 21, 2006 Share Posted July 21, 2006 in display.php do the foloowing[code]include 'config.php';[/code] Link to comment https://forums.phpfreaks.com/topic/15264-variable-scope-problem/#findComment-61681 Share on other sites More sharing options...
Rich464 Posted July 21, 2006 Author Share Posted July 21, 2006 oh bugger, sorry.No I did put that (duh!)sorry I'm an idiot.ill amend it :P Link to comment https://forums.phpfreaks.com/topic/15264-variable-scope-problem/#findComment-61682 Share on other sites More sharing options...
spyke01 Posted July 21, 2006 Share Posted July 21, 2006 ok is there a link to this page?tryconfig.php[code]<?php$user = "user"; $pass = "pass";?>[/code]display.php[code]<?phpinclude ('config.php');echo $user; //these seem to work fineecho $pass; //values are displayed correctlyfunction display_vars(){ echo $user; //these give undefined variable warnings echo $pass; //have tried using global too}display_vars();?>[/code]i changed display_vars() to display_vars(); Link to comment https://forums.phpfreaks.com/topic/15264-variable-scope-problem/#findComment-61685 Share on other sites More sharing options...
Oldiesmann Posted July 21, 2006 Share Posted July 21, 2006 If you use [tt]global $user, $pass;[/tt] at the beginning of display_vars() now (after including config.php), it should work. Link to comment https://forums.phpfreaks.com/topic/15264-variable-scope-problem/#findComment-61686 Share on other sites More sharing options...
Rich464 Posted July 21, 2006 Author Share Posted July 21, 2006 no,sorry.Man I really screwed this up trying to simplify it. Here I'll post the actual code.[code]config.php<?php$hostname = "Localhost"; //the hostname for the database server$username = "user"; //The username to access the database with$password = "password"; //the password to use to access the database$dbname = "db name"; //the actual name of the database to use?>dbconnect.php<?phprequire ('config.php');function db_connect(){ //use values pulled from config.php $db = new mysqli($hostname,$username,$password,$dbname); return $db;}$conn = db_connect();?>[/code]the point is when the db_connect() function is called I get undefined variable warnings. I need to figure out why, or some way around itWhy aren't the variables declared in the include file available inside the function? Link to comment https://forums.phpfreaks.com/topic/15264-variable-scope-problem/#findComment-61688 Share on other sites More sharing options...
spyke01 Posted July 21, 2006 Share Posted July 21, 2006 tryconfig.php[code]<?php$hostname = "Localhost"; //the hostname for the database server$username = "user"; //The username to access the database with$password = "password"; //the password to use to access the database$dbname = "db name"; //the actual name of the database to use?>[/code]dbconnect.php[code]<?phprequire ('config.php');function db_connect(){ global $hostname, $username, $password; //use values pulled from config.php $db = mysql_connect($hostname,$username,$password); if (!$db) die('Could not connect:' . mysql_error()); return $db;}$conn = db_connect();mysql_select_db($dbname, $conn)or die('Could not open database:' . mysql_error());?>[/code]why not just dodbconn.php[code]<?php$mysql_location = 'localhost';$mysql_user = 'user';$mysql_password = 'pass';$mysql_db = 'database';$link = mysql_connect($mysql_location, $mysql_user, $mysql_password);if (!$link) die('Could not connect:' . mysql_error());mysql_select_db($mysql_db, $link)or die('Could not open database:' . mysql_error());?>[/code]you can just do include 'dbconn.php'; on each page, much simpler Link to comment https://forums.phpfreaks.com/topic/15264-variable-scope-problem/#findComment-61691 Share on other sites More sharing options...
Rich464 Posted July 21, 2006 Author Share Posted July 21, 2006 Well I managed to gather what I needed to do from that, as your code still reports $dbname as an undefined variable.It works when the variables are declared as global INSIDE the function.....weird.Anyway thanks for your help :) :D Link to comment https://forums.phpfreaks.com/topic/15264-variable-scope-problem/#findComment-61694 Share on other sites More sharing options...
kenrbnsn Posted July 21, 2006 Share Posted July 21, 2006 [quote]It works when the variables are declared as global INSIDE the function.....weird.[/quote]That's how global works in PHP.Ken Link to comment https://forums.phpfreaks.com/topic/15264-variable-scope-problem/#findComment-61696 Share on other sites More sharing options...
spyke01 Posted July 21, 2006 Share Posted July 21, 2006 yep always remember that, if your in a function use global Link to comment https://forums.phpfreaks.com/topic/15264-variable-scope-problem/#findComment-61698 Share on other sites More sharing options...
king arthur Posted July 21, 2006 Share Posted July 21, 2006 What you really need to be doing is [code]function display_vars($username, $password){ echo $username; //these give undefined variable warnings echo $password; //have tried using global too}display_vars($user, $pass)[/code]This is a far more structured way than using global variables. Link to comment https://forums.phpfreaks.com/topic/15264-variable-scope-problem/#findComment-61709 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.