0

Bit of a problem here, I have a file with the following code:

<?php 
$kvkvariable = 000050650459;   
include("../../../../../../httpdocs/profiel/indextemplate.php"); 
?> 

Inside the included PHP file, I need $kvkvariable. However, when I echo this variable it's not showing the number I want it to show?

If I place the defining code of the $kvkvariable inside indextemplate.php it works fine. I thought including was the same as pasting the code in the same file? How do I fix this?

Thanks!

EDIT:

indextemplate.php immediatly starts with this:

<?php

echo "Dit is kvk: ".$kvkvariable."";

It gives the following result:

Dit is kvk: 1337893

To be honest with you, I have no idea where he gets that number!?

10
  • 2
    Could you add the code of indextemplate.php? You're quite right about how includes work, so it might be an issue of scope. Commented Sep 5, 2012 at 20:02
  • The scope should be the same as the included file. Are you using namespaces? Commented Sep 5, 2012 at 20:02
  • I guess he is talking about global scope Commented Sep 5, 2012 at 20:03
  • Are you sure the file is included properly? Change include with require and check for errors Commented Sep 5, 2012 at 20:03
  • "when I echo this variable it's not showing the number I want it to show?" what number is it showing? Commented Sep 5, 2012 at 20:04

4 Answers 4

2
<?php 
$kvkvariable = 000050650459;   
include("../../../../../../httpdocs/profiel/indextemplate.php"); 
?> 

When PHP sees a number beginning with a 0, it assumes it's octal - and if you run 000050650459 through an octal to decimal converter, it gives you 1337893.

So your include is working as expecting; it's PHP's variable typing that's not doing what you expect.

Sign up to request clarification or add additional context in comments.

1 Comment

Absolutely true! Double quotes around the number solved the problem!
1

how about:

<?php
$kvkvariable = 000050650459;   
include( "../../../../../../httpdocs/profiel/indextemplate.php?myvar=" . $kvkvariable ); 
?>

And you treat the variable inside your include as $_GET[ 'myvar' ]

Then your second file should be like this:

<?php
echo "Dit is kvk: " . $_GET[ 'myvar' ] . "";

Comments

0

Try using php's global keyword.

<?php 
global $kvkvariable;
$kvkvariable = 000050650459;   
include("../../../../../../httpdocs/profiel/indextemplate.php"); 
?> 

1 Comment

Unfortunately that's not the solution, I'm not using it in a function, just a simple echo ;)
0

The answer to my own question was fairly simple, use "" around the number defining the variable! My bad.. Maybe this helps someone out!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.