...

View Full Version : How do I use variables inside a heredoc to tell php to send a string to the browser?



milesdriven
07-22-2012, 03:37 AM
I'm trying to use php variables to create a unique string inside a heredoc.
The code below is only an example I'm using to help ask the question. These are the variables:


$M3 = 'Jul';
$hi = '1';
$Y = '2012';


The string I want php to send to the browser (using these variables) is shown below.The "8AM_list" part is a literal that I added in manually. There is no variable that represents "8AM_list":


id = 'Jul120128AM_list'


I tried this:


id='"$M3$hi$Y"8AM_list'


and the browser received this:


id='"Jul12012"8AM_list'


I tried this:


id='".$M3.$hi.$Y."8AM_list'


and the browser received this:


id='".Jul.1.2012."8AM_list'


I'm trying to get the browser to display this:


id='Jul120128AM_list'


Again, this is inside a heredoc. Does anyone know how to do this?
Thank you.

low tech
07-22-2012, 04:02 AM
Hi

Not sure this is what you want but it worked for me.



$M3 = 'Jul';
$hi = '1';
$Y = '2012';
$id="'".$M3.$hi.$Y.'8AM_list'."'";


echo <<<HEREDOC

<div id=$id>Log out</div>

HEREDOC;




output id='Jul120128AM_list'

LT

milesdriven
07-22-2012, 04:20 AM
Hi - thanks for your reply.

It seems using a heredoc has a different set of rules from "regular" php. I want to find a way to do it without defining a variable for the string first. Putting the string directly between the id element quotes will make things easier in the long term.

Your example gives me something to study while I'm figuring this out, and inspires me to try a bunch of different ways to learn how the heredoc sees things.

Thanks for your help.

UPDATE:

Indeed, php sees things differently when inside a heredoc. This works:


id = '{$M3}{$hi}{$Y}8AM_list'


This is sent to the browser:


id = 'Jul120128AM_list'


The last "}" in that line tells php "8AM_list" is a string, not a variable or part of a variable.

Thanks again low tech, you helped me think about how php sees things inside a heredoc. I put the example you gave me directly between the single quotes of the id element. It showed up in the browser with the variables themselves converted to their values, and absolutely everything else echoed literally as written directly to the browser.

That told me the dots and the quotes were probably not necessary at all, because they were not seen as code - except for the single beginning and ending quotes that html requires.

What's left? Brackets.

low tech
07-22-2012, 10:35 AM
Hi

I'm happy you made progress

I don't know the absolute best way to do it --- but your way looks good to my untrained eye:thumbsup:

surely if there is a better way someone will say so.


just a thought but shouldn't you try to echo out double quotes for legal html --- or doesn't that matter?

If it matters just change the single quotes to double.


Out of interest.

It seems using a heredoc has a different set of rules from "regular" php.

These are my limited notes I have on herdoc :

heredoc syntax follows the same parsing rules as strings enclosed in double quotes

variables and escape sequences are parsed

double quotes don't need to be escaped


LT

milesdriven
07-22-2012, 02:36 PM
just a thought but shouldn't you try to echo out double quotes for legal html --- or doesn't that matter? If it matters just change the single quotes to double.LT

Thanks. I didn't think of that. Will do. Thanks for the heredoc notes.

Just realized something. If a heredoc is parsed as a string, then an html element id inside a heredoc is seen by php as a string within a string. I don't know exactly how php sees the brackets when put inside the heredoc, but they seem to help php clear up the string within a string confusion.

low tech
07-23-2012, 01:18 AM
Hi


If a heredoc is parsed as a string, then an html element id inside a heredoc is seen by php as a string within a string. I don't know exactly how php sees the brackets when put inside the heredoc, but they seem to help php clear up the string within a string confusion.

These are my notes on that and I guess I should add heredoc to note 1 now hahahha


1) arrays and objects are not easily parsed when used in echo or print string.

2) Use curly braces to delimit the variable to solve the issue.


I think the curly braces help php to determine which parts of the string are dynamic and which are static.

example of curly brace use:

echo "some string here { $variable['key'] }.";


LT

milesdriven
07-23-2012, 04:05 PM
echo "some string here { $variable['key'] }.";

LT

I'm curious, why did you put the period in there? I ask because I'm trying to diagnose something.

I'm using javascript for a lot of stuff. The javascript works well when called directly by a browser. It works when a login form calls a php script and that php script sends it to the browser in a heredoc.

It stopped working when that heredoc was put in a for loop - even though the element id's look fine as shown in the browsers source code screen.

Maybe I should be using the concatenation period like you did? What do you think?

Keleth
07-23-2012, 04:41 PM
That period is not concatenation, just a regular, end of sentence period. Show the code you threw together, lets see whats wrong. You probably should add the rendered HTML as well.

milesdriven
07-23-2012, 05:02 PM
Working on new post. I'll start a new thread in a few minutes - thanks.

low tech
07-24-2012, 12:32 AM
Hi


I'm curious, why did you put the period in there?

Looking at it again it is as Keleth said

That period is not concatenation, just a regular, end of sentence period.

LT



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum