elentz Posted April 21, 2009 Share Posted April 21, 2009 I want to use a variable in a text area inside a Function. Here is my code function Header() { //Logo $this->Image('cqilogo.PNG',5,4,50); //Arial bold 15 $this->SetFont('Arial','B',15); //Move to the right $this->Cell(75); //Title $this->Cell(100,10,'Proposal',1,0,'C'); $this->SetFont('Arial','',10); $this->Setxy(6,30); $this->Cell(200,10,'5103 Eastman ,0,1,'C'); $this->Line(16,40,200,40);//**16 is the starting point, 200 is the end point and 57 is the position on the page //Line break $this->Ln(1); } I have a variable $proposalid that I want at the end of (100,10,'Proposal $proposalid',1,0,'C'); I've tried to include it in "" and '' and the variable won't show. I can use it in other areas of my script. Is it because it is inside a Function that it won't work? Thanks Link to comment https://forums.phpfreaks.com/topic/154962-is-it-possible-to-use-a-variable-inside-a-function/ Share on other sites More sharing options...
alphanumetrix Posted April 21, 2009 Share Posted April 21, 2009 type this line in somewhere in your function, global $proposalid; - then just use your variable as you would. This is not prefered as you are using OOP, but should work, nevertheless. Link to comment https://forums.phpfreaks.com/topic/154962-is-it-possible-to-use-a-variable-inside-a-function/#findComment-815111 Share on other sites More sharing options...
mrMarcus Posted April 21, 2009 Share Posted April 21, 2009 you should really do some reading on PHP functions. assuming it's being set from the outside, just include it in the brackets there .. actually doesn't matter what you name it when including it in the function .. could be : function Header($googooGaGa) {} calling the function has to be a little more accurate though, ie. Header($proposalid); function Header($proposalid) { //Logo $this->Image('cqilogo.PNG',5,4,50); //Arial bold 15 $this->SetFont('Arial','B',15); //Move to the right $this->Cell(75); //Title $this->Cell(100,10,'Proposal',1,0,'C'); $this->SetFont('Arial','',10); $this->Setxy(6,30); $this->Cell(200,10,'5103 Eastman ,0,1,'C'); $this->Line(16,40,200,40);//**16 is the starting point, 200 is the end point and 57 is the position on the page //Line break $this->Ln(1); } Link to comment https://forums.phpfreaks.com/topic/154962-is-it-possible-to-use-a-variable-inside-a-function/#findComment-815112 Share on other sites More sharing options...
elentz Posted April 21, 2009 Author Share Posted April 21, 2009 I will get into functions as soon as I can. I tried your suggestion and added the $proposal where I wanted with no result. I want the field to say Proposal # 1234 But I am either getting Proposal # "$proposalid" or $proposal or nothing at all. Thanks Link to comment https://forums.phpfreaks.com/topic/154962-is-it-possible-to-use-a-variable-inside-a-function/#findComment-815124 Share on other sites More sharing options...
taith Posted April 21, 2009 Share Posted April 21, 2009 change the 's around the variable to "s... the ' is a literal output... meaning exactly what you put in, is what you get out... while "s is a relative output, meaning it'll substitute out the variables $this->Cell(100,10,"Proposal {$proposalid}",1,0,'C'); {}s arnt technically necessary, but a good precaution Link to comment https://forums.phpfreaks.com/topic/154962-is-it-possible-to-use-a-variable-inside-a-function/#findComment-815129 Share on other sites More sharing options...
elentz Posted April 21, 2009 Author Share Posted April 21, 2009 I guess it's not my day. I copied and pasted your suggestion into my code and it failed to show the variable Link to comment https://forums.phpfreaks.com/topic/154962-is-it-possible-to-use-a-variable-inside-a-function/#findComment-815135 Share on other sites More sharing options...
elentz Posted April 21, 2009 Author Share Posted April 21, 2009 I've been to 3 tutorial sites and all the info I can find references the exact opposite as what I want to do. All I found was being able to use what happens inside the Function in code outside said function. So, any others know how to do this? Thanks Link to comment https://forums.phpfreaks.com/topic/154962-is-it-possible-to-use-a-variable-inside-a-function/#findComment-815165 Share on other sites More sharing options...
Axeia Posted April 21, 2009 Share Posted April 21, 2009 The global suggestion posted earlier allows you manipulate variables outside the scope of the function. It is however as mentioned considered bad practice, and a better way would be to feed it to the function as a parameter and return the results. Both parameters and return values are explained on http://www.w3schools.com/PHP/php_functions.asp It seems like you're manipulating an image however so you probably just need to figure out how parameters work. Link to comment https://forums.phpfreaks.com/topic/154962-is-it-possible-to-use-a-variable-inside-a-function/#findComment-815239 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.