Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post

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

    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:
    PHP Code:
    $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":
    PHP Code:
    id 'Jul120128AM_list' 
    I tried this:
    PHP Code:
    id='"$M3$hi$Y"8AM_list' 
    and the browser received this:
    PHP Code:
    id='"Jul12012"8AM_list' 
    I tried this:
    PHP Code:
    id='".$M3.$hi.$Y."8AM_list' 
    and the browser received this:
    PHP Code:
    id='".Jul.1.2012."8AM_list' 
    I'm trying to get the browser to display this:
    PHP Code:
    id='Jul120128AM_list' 
    Again, this is inside a heredoc. Does anyone know how to do this?
    Thank you.

  • #2
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    891
    Thanks
    174
    Thanked 102 Times in 102 Posts
    Hi

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


    PHP Code:
    $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
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #3
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    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:
    PHP Code:
    id '{$M3}{$hi}{$Y}8AM_list' 
    This is sent to the browser:
    PHP Code:
    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.
    Last edited by milesdriven; 07-22-2012 at 05:59 AM.

  • #4
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    891
    Thanks
    174
    Thanked 102 Times in 102 Posts
    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

    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
    Last edited by low tech; 07-22-2012 at 12:17 PM.
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #5
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by low tech View Post
    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.
    Last edited by milesdriven; 07-22-2012 at 03:55 PM.

  • #6
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    891
    Thanks
    174
    Thanked 102 Times in 102 Posts
    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:
    PHP Code:
    echo "some string here { $variable['key'] }."

    LT
    Last edited by low tech; 07-24-2012 at 02:57 AM.
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #7
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by low tech View Post

    PHP Code:
    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?

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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.

  • Users who have thanked Keleth for this post:

    low tech (07-24-2012)

  • #9
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    Working on new post. I'll start a new thread in a few minutes - thanks.

  • #10
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    891
    Thanks
    174
    Thanked 102 Times in 102 Posts
    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
    Last edited by low tech; 07-24-2012 at 02:58 AM.
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •