Welcome Guest, Not a member yet? Register   Sign In
can't use global keyword in a view.
#1

[eluser]GSV Sleeper Service[/eluser]
now this is odd.
I'm in the process of making a view that will eventually be the body of an email. yes, I know I should put these functions in a helper, I will do eventually, but can someone explain why $nl is not available within the hr() function, even though I'm using the global keyword?
Code:
$nl = "\n";

function hr($cnt)
{
    global $nl; //this does not work!
    return str_pad("",$cnt,"-").$nl;
}

//example
echo hr(10);
echo "test".$nl;
echo "test2";

//expected output
//----------
//test
//test2

//actual output
//----------test
//test2
#2

[eluser]Nick Husher[/eluser]
My intuition is that the view isn't being compiled and executed in a global scope, so $nl is not actually a global variable. You can try nixing the "global $nl;", or you can try doing something like "define('NL','\n');"
#3

[eluser]bitist[/eluser]
Declare everywhere as global the $nl
So this:
Code:
$nl = "\n";
function hr($cnt)
{
    global $nl; //this does not work!
    return str_pad("",$cnt,"-").$nl;
}

Become this:
Code:
global $nl;
$nl = "\n";
function hr($cnt)
{
    global $nl; //it should work
    return str_pad("",$cnt,"-").$nl;
}
#4

[eluser]Michael Wales[/eluser]
Code:
return str_page('', $cnt, '-') . PHP_EOL;
#5

[eluser]GSV Sleeper Service[/eluser]
devpedia : I'll give that a try tomorrow.
Michael : nice idea, but eventually $nl will be set in the controller, "\n" for plaintext, "<br />" for HTML.

thanks for the suggestions, it's not really a problem, I was just curious why global wasn't behaving like it normally does.




Theme © iAndrew 2016 - Forum software by © MyBB