User Name
Password
AppleNova Forums » Genius Bar »

PHP : Passing declared variables to REQUIRE'd files.


Register Members List Calendar Search FAQ Posting Guidelines
PHP : Passing declared variables to REQUIRE'd files.
Thread Tools
drewprops
Space Pirate
 
Join Date: May 2004
Location: Atlanta
 
2005-01-08, 15:50

I suppose that I've hit a PHP milestone in that I -expect- to be able to include/require sections of code. Something that I really need to learn NOW is how to pass values from my main file, my "PHP-engine", to the snippets of code that are included to handle certain user actions.

So, if in my main page I have this:

$value='blue';
echo "Is the sky blue?<br/>";
require ('colortest.txt');
echo "$decision";

and I want the 'outsource' file named "colortest.txt" to hand back a value of yes or no based on the contents of the variable $value, shown in this routine:

echo "You selected $decision";
if ($value=='blue'){
$decision='Yes';
}else{
$decision='No';
}

Thanks!

Steve Jobs ate my cat's watermelon.
Captain Drew on Twitter
  quote
ast3r3x
25 chars of wasted space.
 
Join Date: May 2004
Send a message via AIM to ast3r3x  
2005-01-08, 16:48

I'm not quite sure what you mean, because copying and pasting your two snippets of code I got it to work, like you said you wanted I believe.

What you can do is make the user action a function so that you can just call it...

index.php
[PHP]<?php

$value= "blue";
echo("Is the sky blue?<br/>\r");

include("colorcheck.php");

if(ColorCheck($value))
{
echo("yes");
}
else
{
echo("no");
}

?>[/PHP]

colorcheck.php
[PHP]
<?php

function ColorCheck($color)
{
if($color == "blue")
{
return true;
}

return false;
}

?>
[/PHP]
  quote
drewprops
Space Pirate
 
Join Date: May 2004
Location: Atlanta
 
2005-01-08, 18:12

Okay then, sounds like its time for me to learn functions!
Doesn't actually sound terribly complicated considering what I've been doing so far... thnx!
  quote
SilentEchoes
Unique Like Everyone Else
 
Join Date: May 2004
Location: Rochester, NY
Send a message via AIM to SilentEchoes  
2005-01-08, 18:58

You have to make sure they are Global Variables.

So in your included file you want to global the variable like so:

Say the page that is doing the including has variable $var

and you want to use the contents of that variable in an included file. You would add this line, Usually at the top of the file so that way your sure you dont try and use the variable before you have made it global.

Just add this

Global $var;

then go ahead and use it like normal.

Hope this helps!

WARNING: Do not let Dr. Mario touch your genitals. He is not a real doctor.
  quote
SilentEchoes
Unique Like Everyone Else
 
Join Date: May 2004
Location: Rochester, NY
Send a message via AIM to SilentEchoes  
2005-01-08, 19:03

Asterex, You should check and see if you have Registered Global Variables on or off in your PHP.ini file. That could be a reason why it worked for you.

Using a function for this part is not entirely necessary. It only serves to write cleaner code IMO

Also what version of PHP are you running?

WARNING: Do not let Dr. Mario touch your genitals. He is not a real doctor.
  quote
ast3r3x
25 chars of wasted space.
 
Join Date: May 2004
Send a message via AIM to ast3r3x  
2005-01-08, 19:17

I'm running PHP 5.0.2, and I do have globals on.

I don't run in an environment where I have to worry about security, so I just always have register_globals on.

Not that my opinion means much, but I prefer to do almost everything in functions because I slightly obsessed about clean looking code...damn high school C++ teacher.
  quote
Gargoyle
http://ga.rgoyle.com
 
Join Date: May 2004
Location: In your dock hiding behind your finder icon!
 
2005-01-08, 19:36

I think you completely over complicated the situation for the poor lad! Here's an example...

[php]
<?php
$variable_one = "Hello World";
$variable_two = "My name is bob";

require("some_other_file.php");
/* $variable_one & $variable_two are available
for use in this included file as if it were a block
of code right here. */

[/php]
  quote
ast3r3x
25 chars of wasted space.
 
Join Date: May 2004
Send a message via AIM to ast3r3x  
2005-01-08, 19:52

That is what I was saying in my original post, how it worked.

Last edited by ast3r3x : 2005-01-08 at 20:07.
  quote
drewprops
Space Pirate
 
Join Date: May 2004
Location: Atlanta
 
2005-01-09, 10:26

Urgh.
So, first of all I went back and looked at my example code and the logic of the included file is incorrect (notice that I put part of the output line in the included file and called for a variable that had yet to be defined) but more importantly I actually ran the simple experiment myself and it worked. Which is confusing because the REAL stuff I'm doing doesn't seem to be working so I need to go back and play with the code on the site I'm developing and figure out what I've done wrong... so wait right here until I get back.

Don't wander off to other threads.... hey. HEY! Come back!

Steve Jobs ate my cat's watermelon.
Captain Drew on Twitter
  quote
drewprops
Space Pirate
 
Join Date: May 2004
Location: Atlanta
 
2005-01-11, 00:03

I just wanted to do a followup on this topic and say that the problem is "fixed" and that it actually worked in the method that I originally wished.

The problem I was having is due to the fact that my coding was a little more complicated than I let on, in that I am SERIOUSLY bending Movable Type (2.5.1) into a PHP-driven site and was encountering an issue that was related to MT and not to PHP. There are certain structures in MT that do not allow for you to issue PHP calls; they just don't work.

Thanks you guys!

Steve Jobs ate my cat's watermelon.
Captain Drew on Twitter
  quote
Posting Rules Navigation
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Post Reply

Forum Jump
Thread Tools

« Previous Thread | Next Thread »

All times are GMT -5. The time now is 06:39.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004 - 2024, AppleNova