FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » echo other way to output a constant?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: echo other way to output a constant? [message #169502 is a reply to message #169500] Wed, 15 September 2010 15:57 Go to previous messageGo to previous message
matt[1] is currently offline  matt[1]
Messages: 40
Registered: September 2010
Karma:
Member
On Sep 15, 11:25 am, "Peter H. Coffin" <hell...@ninehells.com> wrote:
> On Tue, 14 Sep 2010 20:52:31 -0500, MikeB wrote:
>> Peter H. Coffin wrote:
>>> On Tue, 14 Sep 2010 17:12:38 -0500, MikeB wrote:
>>>> Say I have the following code:
>
>>>> <?php
>
>>>> define('MAX_FILE_SIZE', 300);
>
>>>> echo<<<_END
>>>> <html><head><title>PHP Form Upload</title></head><body>
>>>> <form method='post' action='UploadFile2.php' enctype='multipart/form-data'>
>>>> <!-- MAX_FILE_SIZE must precede the file input field -->
>>>> <input type="hidden" name="MAX_FILE_SIZE" value="" />
>>>> Select a JPG, GIF, PNG or TIF File:
>>>> <input type='file' name='filename' size='60' />
>>>> <br/><input type='submit' value='Upload' /></form>
>>>> _END;
>
>>>> echo "</body></html>";
>>>> ?>
>
>>>> How can I get PHP to place the value of the constant MAX_FILE_SIZE in
>>>> the value attribute for the hidden field MAX_FILE_SIZE?
>
>>>> Since it does not start with a $-sign it is not interpreted as a
>>>> variable. I tried putting it in curly braces {}, which sometimes work,
>>>> but that didn't do the trick  either.
>
>>>> I was hoping that print_r() would do it, but I could not get PHP to
>>>> interpret that either.
>
>>>> So any way or do I have to a) put the constant in a variable or b) echo
>>>> each line individually so that I can do concatenation of the output lines?
>
>>> PHP puts responses to forms sent using method post into a special array
>>> called "_POST". So, to get at the contents of an input control called
>>> "MAX_FILE_SIZE", you use the variable $_POST['MAX_FILE_SIZE']. There is
>>> some trickiness with input types that can return multiple values, but we
>>> can get to that once you've gotten the above bits working and are
>>> comfortable with them.
>
>> Perhaps I expressed myself poorly.
>
> Or perhaps I read poorly.
>
>
>
>> I want PHP to send html to the browser.
>
>> It should look like this:
>
>> <input type="hidden" name="MAX_FILE_SIZE" value="300" />
>
>> This is as part of an echo <<<_END statement.
>
>> The number 300 in the above instance is a constant in my PHP code
>> defined as:
>
>> define('MAX_FILE_SIZE', 300);
>
>> If I try to put the following in my program, the literal MAX_FILE_SIZE
>> appears in the html:
>
>> echo <<<_END
>> <input type="hidden" name="MAX_FILE_SIZE" value="MAX_FILE_SIZE" />
>> _END
>
>> if I do this, I get what I want:
>
>> $mfs = MAX_FILE_SIZE;
>> echo <<<_END
>> <input type="hidden" name="MAX_FILE_SIZE" value="$mfs" />
>> _END
>
>> So are you saying that an alternate method is to initialize the
>> MAX_FILE_SIZE element in the _POST array and do it that way?
>
>> Thanks.
>
> No, but I don't think you can do variable or constant expansion inside a
> heredoc, the thing with the <<< operation. You can terminate it early,
> before the part that you want to contain the variable, print a bit that
> includes the variable expansion, then do more heredoc dumping. Thusly:
>
> <?php
>
> $MAX_FILE_SIZE=300;
>
> echo<<<_END
> <html><head><title>PHP Form Upload</title></head><body>
> <form method='post' action='UploadFile2.php'
> enctype='multipart/form-data'>
> <!-- MAX_FILE_SIZE must precede the file input field -->
> _END;
> print "<input type='hidden' name='$MAX_FILE_SIZE' value='' />";
> echo<<<_END
> Select a JPG, GIF, PNG or TIF File:
> <input type='file' name='filename' size='60' />
> <br/><input type='submit' value='Upload' /></form>
> _END;
>
> echo "</body></html>";
> ?>

No, PHP won't extrapolate a defined constant in heredoc syntax,
anymore than it would in a regular string:

define("MY_CONSTANT", 4);
echo "MY_CONSTANT";

But, it will work just fine if you use a regular variable:

$MY_CONSTANT = 4;
echo <<<END
$MY_CONSTANT
END;

It has nothing to do with heredoc and everything to do with PHP's
string parsing. heredoc and double quoted strings behave exactly the
same in this capacity.

Personally, I'd love to see the complex syntax expanded to retrieve
constants as such:

echo "{MY_CONSTANT}";

> As far as I can remember, heredocs do zero procession on the output
> aside from checking for line-ending-followed-by-end-tag. Which means
> you can potentially be bitten by line ending problems, character set
> problems, etc, etc.

You're not quite right on the heredoc parsing. I have gotten bitten
before when Windows style line endings have crept into code, however
to me that's more of a problem with repository control than the
heredoc syntax. You'd have the same problem if you were writing a
command line utility with a she-bang at the top.

more here: http://www.php.net/manual/en/language.types.string.php
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Removing insignificant decimal characters?
Next Topic: Reference # in var_dump output?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 20:46:36 GMT 2024

Total time taken to generate the page: 0.04236 seconds