mdonders Posted January 14, 2013 Share Posted January 14, 2013 I have a quick web application I am writing where I read in an XML file and then parse it for variables I want to display on the page, but for some reason it just isn't working. Want to make sure I am not missing something. Here is the code where I retrieve my variables: function parseRecipeData($path) { global $db_recipe_type; $xml = simplexml_load_file($path); var_dump($xml); // Get App Details // $recipe_application = $xml->recipe[0]->attributes()->application; // $recipe_appVersion = $xml->recipe->attributes()->version; // Get Main Details $recipe_title = $xml->title; $recipe_brewer = $xml->brewer; $recipe_style = $xml->style; $recipe_quantity = $xml->batch[0]->attributes()->quantity; $recipe_type = $db_recipe_type; echo $recipe_title . ' by ' . $recipe_brewer; } Here is the code I am trying to display the above variables, called above the <html> tag of my PHP file. <div id="container"> <h1>Recipe: <?php echo $recipe_title . ' by ' . $recipe_brewer; ?></h1> </div> For some reason it only displays "Recipe: by" without the details I pulled from the XML file. Doing a var_dump shows the actual contents of the variables are there. Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/273155-variable-wont-echo-in-html/ Share on other sites More sharing options...
Jessica Posted January 14, 2013 Share Posted January 14, 2013 Turn on error reporting, and post the actual output - ie, does it say Recipe by two times?? Quote Link to comment https://forums.phpfreaks.com/topic/273155-variable-wont-echo-in-html/#findComment-1405634 Share on other sites More sharing options...
mdonders Posted January 14, 2013 Author Share Posted January 14, 2013 I turned on error reporting with error_reporting(-1); and got no errors on the page. The top of my page (above my HTML Layout) it shows the actual information: Pasta Salad by Test Chef Quote Link to comment https://forums.phpfreaks.com/topic/273155-variable-wont-echo-in-html/#findComment-1405636 Share on other sites More sharing options...
Jessica Posted January 14, 2013 Share Posted January 14, 2013 (edited) And what did it show when you changed <div id="container"> <h1>Recipe: <?php echo $recipe_title . ' by ' . $recipe_brewer; ?></h1> </div> to <div id="container"> <?php var_dump($recipe_title); ?> </div> ?? What code is between these two snips? Edited January 14, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/273155-variable-wont-echo-in-html/#findComment-1405639 Share on other sites More sharing options...
Jessica Posted January 14, 2013 Share Posted January 14, 2013 Oh I just noticed you have all that in a function. You have a variable scope issue. If you're not returning the variables from the function you can't use them. Quote Link to comment https://forums.phpfreaks.com/topic/273155-variable-wont-echo-in-html/#findComment-1405640 Share on other sites More sharing options...
mdonders Posted January 14, 2013 Author Share Posted January 14, 2013 Oh I just noticed you have all that in a function. You have a variable scope issue. If you're not returning the variables from the function you can't use them. So would I be better off (is it safe to) declare them as global at the top of my function for use later on in my HTML or is there a better way to do that? function parseRecipeData($path) { global $recipe_title, $recipe_brewer; } Quote Link to comment https://forums.phpfreaks.com/topic/273155-variable-wont-echo-in-html/#findComment-1405661 Share on other sites More sharing options...
Jessica Posted January 14, 2013 Share Posted January 14, 2013 No, return the values. Quote Link to comment https://forums.phpfreaks.com/topic/273155-variable-wont-echo-in-html/#findComment-1405663 Share on other sites More sharing options...
mdonders Posted January 14, 2013 Author Share Posted January 14, 2013 No, return the values. But you can't return multiple values from a function - so is it recommended that I put these into an array or maybe do this processing outside of the function itself? Any insight? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/273155-variable-wont-echo-in-html/#findComment-1405671 Share on other sites More sharing options...
Jessica Posted January 14, 2013 Share Posted January 14, 2013 Probably best to use an array, but you could also return the string you plan on echoing. Quote Link to comment https://forums.phpfreaks.com/topic/273155-variable-wont-echo-in-html/#findComment-1405674 Share on other sites More sharing options...
mdonders Posted January 14, 2013 Author Share Posted January 14, 2013 Probably best to use an array, but you could also return the string you plan on echoing. I'll be echoing multiple variables though - what is unsafe about declaring global or using $GLOBALS out of curiosity. Thanks for all your help though - it is greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/273155-variable-wont-echo-in-html/#findComment-1405695 Share on other sites More sharing options...
Jessica Posted January 14, 2013 Share Posted January 14, 2013 I'm sick, google it. Quote Link to comment https://forums.phpfreaks.com/topic/273155-variable-wont-echo-in-html/#findComment-1405696 Share on other sites More sharing options...
mdonders Posted January 14, 2013 Author Share Posted January 14, 2013 I'm sick, google it. Fine - thanks for your help anyway. I'll just move on to Google now I guess. Quote Link to comment https://forums.phpfreaks.com/topic/273155-variable-wont-echo-in-html/#findComment-1405698 Share on other sites More sharing options...
Jessica Posted January 14, 2013 Share Posted January 14, 2013 Btw I said string - a string can contain multiple variables. If you need them separate, use an array. Or separate the logic into two functions. Quote Link to comment https://forums.phpfreaks.com/topic/273155-variable-wont-echo-in-html/#findComment-1405701 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.