[Solved] Undefined Variable with included functions.php

Problems with the Windows version of XAMPP, questions, comments, and anything related.

[Solved] Undefined Variable with included functions.php

Postby Crystaltears83 » 24. June 2012 19:22

Hello and howdy everyone! :)

After searching the forum for various different permutations of this question, I've still not found an answer that helps me so I'm asking here.

I've run into an issue on my live server and decided to use XAMPP in order to attempt to troubleshoot my problem. So basically I started out with a very simple setup:

I exported my live database, and then imported it into localhost/phpmyadmin.

I removed all users so that there is only one set of data to deal with.

Then I tried calling something like $user_data['first_name']; or something in my test.php page... below are images that show the setup of the database and my file structure, etc:

I do apologize for the dimensions of this image. I attempted to resize it to make it smaller however doing so made the text virtually unreadable so it is at its base size of 1585 by 106. Screenshot at http://valkyries.usa.cc/images/ref1.jpg

And here is the structure of my database: Screenshot at http://valkyries.usa.cc/images/ref2.jpg

Here is my file structure: Screenshot at http://valkyries.usa.cc/images/ref3.jpg

So rather than trying to work with the full site from my live server, I just copied the users.php and general.php from my live site which contains all the functions I need.

I have my main page, which at the moment is simply named test.php and includes the init.php file at the top:

Code: Select all
<?php require "core/init.php";
?>
<html>
<head>
<title>Test Site</title>
</head>
<body>
<?php
echo "Hello world! $foo"
?>
</body>


My init.php:

Code: Select all
<?php
session_start();
//error_reporting(0);


require 'database/connect.php';  // Tells where to look for database connection info.
require 'functions/general.php';  // Tells where to look for general functions.
require 'functions/users.php';  // Tells where to look for user related functions.

$current_file = explode('/', $_SERVER['SCRIPT_NAME']);
$current_file = end($current_file);

if (logged_in() === true) {
   $session_user_id = $_SESSION['user_id'];
   $user_data = user_data($session_user_id, 'user_id', 'username', 'password', 'first_name', 'last_name', 'email', 'legion_char', 'password_recover', 'rank', 'allow_emails', 'rules');
   if (user_active($user_data['username']) === false){
      session_destroy();
      header('Location: index.php');
      exit();
   }
   if ($current_file !== 'changepassword.php' && $current_file !== 'logout.php' && $user_data['password_recover'] == 1) {
   $URL="changepassword.php?force";
   echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">';
   exit();
   }

   
   // Code for outputting specific data: echo $user_data['username'];
}
$errors = array();
?>


The Init.php does still include some things from my live server but I didn’t think they would really hurt anything. (Could be wrong?)

I tried to call one of the functions from users.php and received the undefined variable notice/error and so I tried defining
Code: Select all
function foo(){
    echo("foobar");
}

in the users.php file and that too gave me this notice/error:
Notice: Undefined variable: foo in C:\xampp\htdocs\a\test.php on line 9


So anyway I’m not entirely sure what the problem is, but perhaps you can help.  Thanks!
Last edited by Crystaltears83 on 25. June 2012 04:59, edited 2 times in total.
User avatar
Crystaltears83
 
Posts: 3
Joined: 24. June 2012 14:23
Location: USA
Operating System: Windows 7 Ultimate Sp1, 64-bit

Re: Undefined Variable with included functions.php

Postby Altrea » 24. June 2012 20:12

Hi Crystaltears83 (there is ALWAYS time for a short salutation, especially ifyou are asking for help at a community board. It's a matter of politeness),
Crystaltears83 wrote:[...] test.php [...]
Code: Select all
[...]
echo "Hello world! $foo"
[...]

What do you think is $foo? What should it contain?
It is defined nowhere.

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Undefined Variable with included functions.php

Postby Crystaltears83 » 24. June 2012 21:50

Hey there. Sorry for not greeting before. I've just been up for ages trying to work this out and worked for a couple hours on that post to make sure I fit it in as best I could with the rules.


So what is foo... foo is defined in users.php via:

Code: Select all
function foo(){
    echo("foobar");
}


Therefore, foo contains foobar and

$foo

should echo out:
foobar

This was my understanding at least. Only been trying to learn PHP for about a week now so please go easy, but I am trying :)
User avatar
Crystaltears83
 
Posts: 3
Joined: 24. June 2012 14:23
Location: USA
Operating System: Windows 7 Ultimate Sp1, 64-bit

Re: Undefined Variable with included functions.php

Postby Altrea » 24. June 2012 22:46

Crystaltears83 wrote:So what is foo... foo is defined in users.php

No, it is not. $foo is a variable, not a function call or return value.
If you want to access foo() you should simply call it:

Code: Select all
[...]
echo "Hello world!";
foo();
[...]


or assign it's return value to a vaiable which is the better way.
For this you should follow the best practise to not echo anything out in functions directly but use return value:

Code: Select all
[...]
function foo() {
    return 'foobar';
}
$foo = foo();
echo "Hello world!" .$foo;
[...]


best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Undefined Variable with included functions.php

Postby Crystaltears83 » 25. June 2012 04:59

Ahhhh! :) Thank you!
User avatar
Crystaltears83
 
Posts: 3
Joined: 24. June 2012 14:23
Location: USA
Operating System: Windows 7 Ultimate Sp1, 64-bit

Re: [Solved] Undefined Variable with included functions.php

Postby Altrea » 25. June 2012 05:22

You are welcome :D
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 114 guests