Using variables deinfed later

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pat

    Using variables deinfed later

    Is it possible in anyway to use a variable that is initialied *after*
    its
    use.

    What I am trying to achieve is the following, i.e. ,using a PHP
    variable that is initialised later to display a HTML Title, code
    snippet below. As it
    stands at present nothing is echoed!

    <html>
    <head>
    <title><?php echo ($strTitle);?></title>
    </head>
    <body>
    ......
    <?php
    ....
    $strTitle= getTitle();
    ...
    ?>
  • Ian.H

    #2
    Re: Using variables deinfed later

    On Fri, 30 Jul 2004 08:50:30 -0700, pat wrote:
    [color=blue]
    > Is it possible in anyway to use a variable that is initialied *after*
    > its
    > use.
    >
    > What I am trying to achieve is the following, i.e. ,using a PHP
    > variable that is initialised later to display a HTML Title, code
    > snippet below. As it
    > stands at present nothing is echoed![/color]


    Wow that's some task! Like asking to be buried before you're born ;)

    Silly question time, what results to you expect?

    In short, your example below _will_ work with in PHP4 (not 5 though AFAIK)
    but $strTitle will be empty in the first instance. You should still define
    the variable as below, but I suspect this isn't strictly the answer you're
    looking for or rather, hoping for.


    <?php
    $strTitle = '';
    ?>[color=blue]
    > <html>
    > <head>
    > <title><?php echo ($strTitle);?></title>
    > </head>
    > <body>
    > ......
    > <?php
    > ....
    > $strTitle= getTitle();
    > ...
    > ?>[/color]

    --
    Ian.H
    digiServ Network
    London, UK


    Comment

    • Ian.H

      #3
      Re: Using variables deinfed later

      On Fri, 30 Jul 2004 16:16:07 +0000, Ian.H wrote:
      [color=blue]
      > In short, your example below _will_ work with in PHP4 (not 5 though AFAIK)
      > but $strTitle will be empty in the first instance. You should still define
      > the variable as below, but I suspect this isn't strictly the answer you're
      > looking for or rather, hoping for.
      >
      >
      > <?php
      > $strTitle = '';
      > ?>[color=green]
      >> <html>
      >> <head>
      >> <title><?php echo ($strTitle);?></title>
      >> </head>
      >> <body>
      >> ......
      >> <?php
      >> ....
      >> $strTitle= getTitle();
      >> ...
      >> ?>[/color][/color]


      Oops, should have added:

      If I understand you correctly, you want the title to display but you're
      actualy getting the title half way through the script. You maybe able to
      do this with output buffering (see ob_start() and other relative functions
      on php.net for more info) but it's something I don't use very often at
      all, so can't really point you in a better direction than the manual
      unfortunately.

      Hope this sort of helps though.



      Regards,

      Ian
      --
      Ian.H
      digiServ Network
      London, UK


      Comment

      • Andy Hassall

        #4
        Re: Using variables deinfed later

        On 30 Jul 2004 08:50:30 -0700, wmrpbhhapzlm@sp ammotel.com (pat) wrote:
        [color=blue]
        >Is it possible in anyway to use a variable that is initialied *after*
        >its
        >use.
        >
        >What I am trying to achieve is the following, i.e. ,using a PHP
        >variable that is initialised later to display a HTML Title, code
        >snippet below. As it
        >stands at present nothing is echoed!
        >
        ><html>
        > <head>
        > <title><?php echo ($strTitle);?></title>
        > </head>
        > <body>
        > ......
        > <?php
        > ....
        > $strTitle= getTitle();
        > ...
        > ?>[/color]

        Hm, time travel variables - sounds like this could work nicely with the
        mythical COME FROM command (opposite of GOTO) to write truly evil programs.



        But seriously - no.

        --
        Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
        http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

        Comment

        • Jeffrey Silverman

          #5
          Re: Using variables deinfed later

          On Fri, 30 Jul 2004 08:50:30 -0700, pat wrote:
          [color=blue]
          > Is it possible in anyway to use a variable that is initialied *after*
          > its
          > use.[/color]

          Try the output buffer functions:



          1) Turn on output buffering.

          2) Do stuff.

          3) Flush buffer.

          --
          Jeffrey Silverman
          jeffreyPANTS@jh u.edu
          ** Drop "PANTS" to reply by email


          Comment

          • Chung Leong

            #6
            Re: Using variables deinfed later

            "pat" <wmrpbhhapzlm@s pammotel.com> wrote in message
            news:92c8e841.0 407300750.29a7e 259@posting.goo gle.com...[color=blue]
            > Is it possible in anyway to use a variable that is initialied *after*
            > its
            > use.
            >
            > What I am trying to achieve is the following, i.e. ,using a PHP
            > variable that is initialised later to display a HTML Title, code
            > snippet below. As it
            > stands at present nothing is echoed!
            >
            > <html>
            > <head>
            > <title><?php echo ($strTitle);?></title>
            > </head>
            > <body>
            > ......
            > <?php
            > ....
            > $strTitle= getTitle();
            > ...
            > ?>[/color]

            A syndrome of the lame-o single-entry pointer architecture... . Ahhh! Ahhhh!
            Ahhhhhhh!


            Comment

            • Pjotr Wedersteers

              #7
              Re: Using variables deinfed later

              pat wrote:[color=blue]
              > Is it possible in anyway to use a variable that is initialied *after*
              > its
              > use.
              >
              > What I am trying to achieve is the following, i.e. ,using a PHP
              > variable that is initialised later to display a HTML Title, code
              > snippet below. As it
              > stands at present nothing is echoed!
              >
              > <html>
              > <head>
              > <title><?php echo ($strTitle);?></title>
              > </head>
              > <body>
              > ......
              > <?php
              > ....
              > $strTitle= getTitle();
              > ...[/color]

              Yes you can -as explained by others- using output-buffering. But it's not
              exactly desirable programming style. Try to avoid constructions like this.


              Comment

              Working...