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

Home » Imported messages » comp.lang.php » pass variable in php construct
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
pass variable in php construct [message #172174] Thu, 03 February 2011 04:27 Go to next message
juliani(dot)moon(at)gmail(dot)co is currently offline  juliani(dot)moon(at)gmail(dot)co
Messages: 6
Registered: January 2011
Karma: 0
Junior Member
I have these simple lines in my php script:

<?php
if (!empty($_GET[req])) {
print "<h2>$_GET[req]</h2>";
?>
<h3>"$_GET[req]"</h3>
<?php
} else {
...
}
?>

I wonder why the $_GET[req] value shows up within the php code block
(print to <h2> tags), but not showing up in the html block (between
<h3> tags)? Is it suppose so or I missed something? (I tried to pass
it to a local defined variable but still cannot be used in 'html'
block).

Thanks in advance,
joe
Re: pass variable in php construct [message #172175 is a reply to message #172174] Thu, 03 February 2011 05:04 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/2/2011 11:27 PM, Joe wrote:
> I have these simple lines in my php script:
>
> <?php
> if (!empty($_GET[req])) {
> print "<h2>$_GET[req]</h2>";
> ?>
> <h3>"$_GET[req]"</h3>
> <?php
> } else {
> ...
> }
> ?>
>
> I wonder why the $_GET[req] value shows up within the php code block
> (print to<h2> tags), but not showing up in the html block (between
> <h3> tags)? Is it suppose so or I missed something? (I tried to pass
> it to a local defined variable but still cannot be used in 'html'
> block).
>
> Thanks in advance,
> joe

First thing is to turn on all errors and notices on your development
system (you DO have a development system, don't you?)

This will give you an E_NOTICE on the line

print "<h2>$_GET[req]</h2>"


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: pass variable in php construct [message #172176 is a reply to message #172174] Thu, 03 February 2011 06:36 Go to previous messageGo to next message
Curtis Dyer is currently offline  Curtis Dyer
Messages: 34
Registered: January 2011
Karma: 0
Member
Joe <juliani(dot)moon(at)gmail(dot)com> wrote:

> I have these simple lines in my php script:
>
> <?php
> if (!empty($_GET[req])) {

You need quotes around ``req''.

> print "<h2>$_GET[req]</h2>";

Here, too.

As Jerry suggests, make sure your error reporting is enabled and
you have "display_errors" on in php.ini on your development
environment.

> ?>
> <h3>"$_GET[req]"</h3>

This is simply output, and never parsed.

See: <http://php.net/manual/en/language.basic-syntax.phpmode.php>

> <?php
> } else {
> ...
> }
> ?>
>
> I wonder why the $_GET[req] value shows up within the php code
> block (print to <h2> tags), but not showing up in the html block
> (between <h3> tags)?

You might consider starting at the beginning to get a better feel
for PHP:

<http://php.net/manual/en/tutorial.php>

> Is it suppose so or I missed something? (I
> tried to pass it to a local defined variable but still cannot be
> used in 'html' block).

It seems to me, you just need a bit more time to get acclimated to
PHP.

> Thanks in advance,
> joe

HTH

--
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>
Re: pass variable in php construct [message #172177 is a reply to message #172174] Thu, 03 February 2011 08:39 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 03/02/2011 5:27, Joe escribió/wrote:
> I have these simple lines in my php script:
>
> <?php
> if (!empty($_GET[req])) {
> print "<h2>$_GET[req]</h2>";
> ?>
> <h3>"$_GET[req]"</h3>
> <?php
> } else {
> ...
> }
> ?>
>
> I wonder why the $_GET[req] value shows up within the php code block
> (print to<h2> tags), but not showing up in the html block (between
> <h3> tags)? Is it suppose so or I missed something? (I tried to pass
> it to a local defined variable but still cannot be used in 'html'
> block).

That's the whole point of having PHP blocks. If PHP was parsed and
executed everywhere in the file, why would you need them?

You should also note the difference between 'foo' (a string) and foo (a
constant). I suggest you read the "Language Reference" chapters in the
PHP manual. It explains very important basics about the language:

http://es.php.net/manual/en/langref.php


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: pass variable in php construct [message #172184 is a reply to message #172175] Thu, 03 February 2011 13:54 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
On 2/3/2011 12:04 AM, Jerry Stuckle wrote:
> On 2/2/2011 11:27 PM, Joe wrote:
>> I have these simple lines in my php script:
>>
>> <?php
>> if (!empty($_GET[req])) {
>> print "<h2>$_GET[req]</h2>";
>> ?>
>> <h3>"$_GET[req]"</h3>
>> <?php
>> } else {
>> ...
>> }
>> ?>
>>
>> I wonder why the $_GET[req] value shows up within the php code block
>> (print to<h2> tags), but not showing up in the html block (between
>> <h3> tags)? Is it suppose so or I missed something? (I tried to pass
>> it to a local defined variable but still cannot be used in 'html'
>> block).
>>
>> Thanks in advance,
>> joe
>
> First thing is to turn on all errors and notices on your development
> system (you DO have a development system, don't you?)
>
> This will give you an E_NOTICE on the line
>
> print "<h2>$_GET[req]</h2>"
>
>

If you wanted your line to get the $_GET then you needed to do:

<h3><?php print $_GET['foo']; ?></h3>

or simply moved your firs ?> to after the <h3> stuff and use another print.

PHP _ONLY_ does PHP stuff inside <?php ..... ?>. Outside those blocks
it is simply text like any other text.

--
Shelly
Re: pass variable in php construct [message #172186 is a reply to message #172184] Thu, 03 February 2011 14:45 Go to previous messageGo to next message
juliani(dot)moon(at)gmail(dot)co is currently offline  juliani(dot)moon(at)gmail(dot)co
Messages: 6
Registered: January 2011
Karma: 0
Junior Member
Thank you all - Got it!

joe
Re: pass variable in php construct [message #172214 is a reply to message #172176] Fri, 04 February 2011 15:42 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(Curtis Dyer)

> Joe <juliani(dot)moon(at)gmail(dot)com> wrote:
>
>> I have these simple lines in my php script:
>>
>> <?php
>> if (!empty($_GET[req])) {
>
> You need quotes around ``req''.

Yes.

>> print "<h2>$_GET[req]</h2>";
>
> Here, too.

No. Note the double quotes around the string. Quotes around the array
key are not allowed here, unless you use curly syntax. So both of these
ways are correct:

print "<h2>$_GET[req]</h2>";
print "<h2>{$_GET['req']}</h2>";

Micha
Re: pass variable in php construct [message #172215 is a reply to message #172214] Fri, 04 February 2011 16:29 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 04-02-11 16:42, Michael Fesser wrote:
> .oO(Curtis Dyer)
>
>> Joe <juliani(dot)moon(at)gmail(dot)com> wrote:
>>
>>> I have these simple lines in my php script:
>>>
>>> <?php
>>> if (!empty($_GET[req])) {
>>
>> You need quotes around ``req''.
>
> Yes.
>
>>> print "<h2>$_GET[req]</h2>";
>>
>> Here, too.
>
> No. Note the double quotes around the string. Quotes around the array
> key are not allowed here, unless you use curly syntax. So both of these
> ways are correct:
>
> print "<h2>$_GET[req]</h2>";
confusing syntax this is.....

But it works, and even better:
print "<h2>$_GET['req']</h2>"; <== produces an error
(PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE,
expecting T_STRING or T_VARIABLE or T_NUM_STRING)


> print "<h2>{$_GET['req']}</h2>";
>
> Micha


--
Luuk
Re: pass variable in php construct [message #172218 is a reply to message #172214] Fri, 04 February 2011 22:11 Go to previous messageGo to next message
Curtis Dyer is currently offline  Curtis Dyer
Messages: 34
Registered: January 2011
Karma: 0
Member
Michael Fesser <netizen(at)gmx(dot)de> wrote:

> .oO(Curtis Dyer)
>
>> Joe <juliani(dot)moon(at)gmail(dot)com> wrote:

<snip>

>>> <?php
>>> if (!empty($_GET[req])) {
>>
>> You need quotes around ``req''.
>
> Yes.
>
>>> print "<h2>$_GET[req]</h2>";
>>
>> Here, too.
>
> No. Note the double quotes around the string. Quotes around the
> array key are not allowed here, unless you use curly syntax. So
> both of these ways are correct:

Thanks, good catch. Been away from PHP too long, I guess.

> Micha

--
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>
Re: pass variable in php construct [message #172219 is a reply to message #172215] Fri, 04 February 2011 22:17 Go to previous message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
On 2/4/2011 11:29 AM, Luuk wrote:
> On 04-02-11 16:42, Michael Fesser wrote:
>> .oO(Curtis Dyer)
>>
>>> Joe<juliani(dot)moon(at)gmail(dot)com> wrote:
>>>
>>>> I have these simple lines in my php script:
>>>>
>>>> <?php
>>>> if (!empty($_GET[req])) {
>>>
>>> You need quotes around ``req''.
>>
>> Yes.
>>
>>>> print "<h2>$_GET[req]</h2>";
>>>
>>> Here, too.
>>
>> No. Note the double quotes around the string. Quotes around the array
>> key are not allowed here, unless you use curly syntax. So both of these
>> ways are correct:
>>
>> print "<h2>$_GET[req]</h2>";
> confusing syntax this is.....
>
> But it works, and even better:
> print "<h2>$_GET['req']</h2>";<== produces an error
> (PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE,
> expecting T_STRING or T_VARIABLE or T_NUM_STRING)
>
>
>> print "<h2>{$_GET['req']}</h2>";
>>
>> Micha
>
>

I prefer:
print "<h2>" . $_GET['req'] . "</h2>";

No confusion thee.



--
Shelly
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Help with my website
Next Topic: validating / converting time for insertion into mysql DB??
Goto Forum:
  

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

Current Time: Sun Nov 10 10:24:06 GMT 2024

Total time taken to generate the page: 0.02773 seconds