0

I have a variable named $url in the top of my file:

<?php
$url = "http://myurl.com";

Later in the same file, I have this code:

<?php
$url = "http://myurl.com";
[...]
function errorOut($error, $type = "info", $rel = "/")
{
    echo $url;
}
?>

However, that doesn't work, because it says $url isn't a valid variable. I have to do this:

<?php
$url = "http://myurl.com";
[...]
function errorOut($error, $type = "info", $rel = "/")
{
    $url = "http://myurl.com";
    echo $url;
}
?>

This doesn't make any sense to me because it shouldn't be out of scope because it's a layer above the function. How do I make it use the earlier $url variable?

Jon
  • 2,566
  • 6
  • 32
  • 52

3 Answers3

1

by passing the $url in your function:

function errorOut($error, $type = "info", $rel = "/", $url) //<<< here

and also calling it:

errorOut('...','...','...',$url);

NON WORKING EXAMPLE AS SEEN IN YOUR ANSWER

$a = 'test1';
$b = 'test2';
define ('URL','one');
define ('URL', 'two');
test($a,$b);

function test ($a,$b){
    echo $a;
    echo $b;
    echo URL;
}

Won't work, URL will stay at 'one' // Will only work if you never want to change URL

baao
  • 71,625
  • 17
  • 143
  • 203
  • I can't do `function errorOut($error, $type = "info", $rel = "/", $url = $url)` and I don't want to change the way I call it every time because I call it all the time. Is there something else you suggest? – Jon Nov 05 '14 at 00:00
  • Well, no. You can use the other answer from @Omnikrys, but using globals is evil... http://stackoverflow.com/questions/5166087/php-global-in-functions/5166527#5166527 – baao Nov 05 '14 at 00:02
1

They are not in the same scope. You have to let PHP know you will be using that global locally. It is preferable to not use a global and instead pass it as a variable though.

<?php
$url = "http://myurl.com";
[...]
function errorOut($error, $type = "info", $rel = "/")
{
    global $url;
    echo $url;
}
?>

See Variable scope for more information.

Omnikrys
  • 2,538
  • 18
  • 13
  • Incoming downvotes in 3...2...1. Please check out [dependency injection](http://en.wikipedia.org/wiki/Dependency_injection). – Dave Chen Nov 04 '14 at 23:56
  • Why downvote? His answer is correct, and even states that it is not the best way to do this.... – TomDillinger Nov 04 '14 at 23:59
  • 1
    The question was why doesn't it work. I explained that. Not sure where the problem is here. – Omnikrys Nov 05 '14 at 00:01
  • @Omnikrys Because as `errorOut` expands into a larger function, and other functions that use `$url` modify it, then you'll start to have difficult code to debug because you won't know which function changed `$url`. When you start to have more variables that are globalized, the problem will only magnify. – Dave Chen Nov 05 '14 at 00:19
  • 1
    I'm well aware of the problem of globals and best practices but the question wasn't "should I use a global?" it was "why doesn't this work?". Throwing principles and patterns at someone who doesn't understand variable scope in PHP when that wasn't even their question makes no sense to me. – Omnikrys Nov 05 '14 at 00:26
1

I was talking to someone in an IRC about this (he posted on here too before I joined), he said I should use

define("URL", 'http://example.com');

And whenever I reference that variable I should use URL, not $url

Jon
  • 2,566
  • 6
  • 32
  • 52
  • If you are using this, you will not be able to change URL, I've edited my answer with an example – baao Nov 05 '14 at 00:19