leblanc Posted February 19, 2010 Share Posted February 19, 2010 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 More sharing options...
Catfish Posted February 19, 2010 Share Posted February 19, 2010 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. Link to comment https://forums.phpfreaks.com/topic/192598-simple-variable-question/#findComment-1014686 Share on other sites More sharing options...
leblanc Posted February 19, 2010 Author Share Posted February 19, 2010 Right right... thats what I thought. Calling something that doesnt exist. But I am trying to call a variable that is in the content, to the <title> So it somehow needs to be done I guess. Link to comment https://forums.phpfreaks.com/topic/192598-simple-variable-question/#findComment-1014698 Share on other sites More sharing options...
leblanc Posted February 19, 2010 Author Share Posted February 19, 2010 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. Link to comment https://forums.phpfreaks.com/topic/192598-simple-variable-question/#findComment-1014711 Share on other sites More sharing options...
leblanc Posted February 19, 2010 Author Share Posted February 19, 2010 Never mind, I had to change around 3 different files and how and when they called the header include, and got the $bike_name above the header. Thanks for your help. It put me on the right path. Link to comment https://forums.phpfreaks.com/topic/192598-simple-variable-question/#findComment-1014714 Share on other sites More sharing options...
Catfish Posted February 19, 2010 Share Posted February 19, 2010 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. Link to comment https://forums.phpfreaks.com/topic/192598-simple-variable-question/#findComment-1014719 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.