Jump to content

Variable Won't Echo in HTML


mdonders

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/273155-variable-wont-echo-in-html/
Share on other sites

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?

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

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.

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.