Jump to content

Simple Variable Question


leblanc

Recommended Posts

Hi everyone.... again,

 

I am really getting into php still. Learning more every day. I love it.

But I have run into a problem I cannot solve.

 

From what I understand, you can only "echo $variable", if the variable is created above the "echo".

short example

$something = "1999 kawasaki";
Echo $something;

----------

 

How would I echo or print a variable if it is created below the area where I want to place the "echo $variable"

Short Example

Echo $something;
**all of the page content here**
$something = "1999 kawasaki";

 

 

I have searched for hours and cannot find a solution.

Thanks in advance for any help.

 

 

Link to comment
https://forums.phpfreaks.com/topic/192598-simple-variable-question/
Share on other sites

You wont find a solution because you can't do that. How can you print something that doesn't exist?

 

The only option is to use a function (I think) but you are still only creating the value before the echo command, so you are only fooling yourself into believing you are doing what you are asking and making the code more complex than requires.

 

function example:

$something = functionCall('1999 kawasaki');
echo $something;

function functionCall($_string)
{
   return($_string);
   // pointless function WTF?
}

 

if you did this, you might as well do:

$something = '1999 kawasaki';

echo $something;

 

What are you actually trying to achieve? Why do you want to do this?

 

PS: You CAN'T do what you are asking?!

PPS: Only other option I can think of is to put the echo into a function:

function echoSomething($_something)
{
   if (echo $_something)
      return(TRUE);
   else
      return(FALSE);
}

***all of page content here***
$something = '1999 kawasaki';
echoSomething($something);
// again - pointless WTF?! just use: "echo $something" at this point.

 

I tried what you said with the

 

function echoSomething($_something)
{
   if (echo $_something)
      return(TRUE);
   else
      return(FALSE);
}

 

and i got this error.

Parse error: syntax error, unexpected T_ECHO in /b0142d1b/www/gallery.php on line 13

 

I like where you were going with that though. I understand the concept.

Any other ideas?

 

$something is actually $bike_name

and I am using $bike_name to specify the bikes name in an image gallery that is called between a header.php include, and a footer.php include.

and wanted the bikes name to appear in the title... If all that made sense.

 

Thanks again.

sometimes it is better to build your content into a string and then output it all at the end of the script rather than outputting it as your script executes. this will let you replace tokens in the content string with dynamic values. example:

$content = '<html><head><title></title></head><body>';
// get some dynamic values from a DB or something and put between <body></body>
$content .= $someDynamicValues;
$content .= '</body></html>';
// oops we need a title! and we would like to use some dynamic value in the title that we have in $aString
$content = str_replace('<title></title>', '<title>The title is: '.$aString.'</title>', $content);

// now output the content to the screen
print($content);

 

Check out my PHP tutorials (in signature) for some other beginner tips. Hopefully my writing style is good enough for you to understand theconcepts I'm trying to get across.

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.