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
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?

Edited by Jessica
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.