Jump to content

Is it possible to use a variable inside a function


elentz

Recommended Posts

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

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);

}

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

 

 

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 ;D

 

    $this->Cell(100,10,"Proposal {$proposalid}",1,0,'C');

 

{}s arnt technically necessary, but a good precaution ;)

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

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.