how call a variable in a text

how call a variable in a text

am 21.10.2009 22:17:23 von bmalik

I 'll send a mail in html form using php5.

cat send.php
$variable="date1" ;
...
...
$message='

There is a text $variable trial.
';

mail($to, $subject, $message, $headers) ;
?>

when I run send.php, I get the mail. But I can't call variable called
variable. it comes as string.
How can I correct this?


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: how call a variable in a text

am 21.10.2009 22:23:22 von Andrew Ballard

2009/10/21 Bulend Kolay :
> I 'll send a mail in html form using php5.
>
> cat send.php
> > $variable=3D"date1" ;
> ..
> ..
> $message=3D'
>
> There is a text $variable  trial.
> ';
>
> mail($to, $subject, $message, $headers) ;
> ?>
>
> when I run send.php, I get the mail. But I can't call variable called
> variable. it comes as string.
> How can I correct this?
>

You need to use double quotes (or HEREDOC) if you want PHP to replace
$variable with its value in the string:

$message=3D"

There is a text $variable trial.
";

or

$message =3D <<
There is a text $variable trial.
MESSAGE;



Andrew

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: how call a variable in a text

am 21.10.2009 22:26:57 von Shawn McKenzie

Bulend Kolay wrote:
> I 'll send a mail in html form using php5.
>
> cat send.php
> > $variable="date1" ;
> ..
> ..
> $message='
>
> There is a text $variable trial.
> ';
>
> mail($to, $subject, $message, $headers) ;
> ?>
>
> when I run send.php, I get the mail. But I can't call variable called
> variable. it comes as string.
> How can I correct this?

http://us2.php.net/manual/en/language.types.string.php

$message= " There is a text $variable trial. ";

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: how call a variable in a text

am 21.10.2009 22:40:00 von David Murphy

--=======AVGMAIL-499D6E1D=======
Content-Type: text/plain;
charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

This is actually much better the { and } make it very obvious where =
the variable is and also it can keep odd issues from occurring =
sometimes.
=09
$message=3D" There is a text {$variable} trial. ";

There is always sprint type functions also.


David

-----Original Message-----
From: Andrew Ballard [mailto:aballard@gmail.com]=20
Sent: Wednesday, October 21, 2009 3:23 PM
To: Bulend Kolay
Cc: php-general@lists.php.net
Subject: Re: [PHP] how call a variable in a text

2009/10/21 Bulend Kolay :
> I 'll send a mail in html form using php5.
>
> cat send.php
> > $variable=3D"date1" ;
> ..
> ..
> $message=3D'
>
> There is a text $variable trial. ';
>
> mail($to, $subject, $message, $headers) ; ?>
>
> when I run send.php, I get the mail. But I can't call variable called=20
> variable. it comes as string.
> How can I correct this?
>

You need to use double quotes (or HEREDOC) if you want PHP to replace =
$variable with its value in the string:

$message=3D"

There is a text $variable trial. ";

or

$message =3D <<
There is a text $variable trial. MESSAGE;



Andrew

--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: =
http://www.php.net/unsub.php

--=======AVGMAIL-499D6E1D=======
Content-Type: multipart/alternative;
boundary="=======AVGMAIL-5AD16850======="

--=======AVGMAIL-5AD16850=======
Content-Type: text/plain; x-avg=cert; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Description: "AVG certification"


No virus found in this outgoing message.
Checked by AVG - www.avg.com
Version: 8.5.423 / Virus Database: 270.14.24/2449 - Release Date: 10/20/09 1=
8:42:00

--=======AVGMAIL-5AD16850=======--

--=======AVGMAIL-499D6E1D=======
Content-Type: text/plain; charset=us-ascii

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--=======AVGMAIL-499D6E1D=======--

RE: how call a variable in a text

am 21.10.2009 22:43:05 von Ashley Sheridan

--=-zEf9ycVlHaRcT1WWdw6n
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Wed, 2009-10-21 at 15:40 -0500, David Murphy wrote:

> This is actually much better the { and } make it very obvious where the variable is and also it can keep odd issues from occurring sometimes.
>
> $message=" There is a text {$variable} trial. ";
>
> There is always sprint type functions also.
>
>
> David
>
> -----Original Message-----
> From: Andrew Ballard [mailto:aballard@gmail.com]
> Sent: Wednesday, October 21, 2009 3:23 PM
> To: Bulend Kolay
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] how call a variable in a text
>
> 2009/10/21 Bulend Kolay :
> > I 'll send a mail in html form using php5.
> >
> > cat send.php
> > > > $variable="date1" ;
> > ..
> > ..
> > $message='
> >
> > There is a text $variable trial. ';
> >
> > mail($to, $subject, $message, $headers) ; ?>
> >
> > when I run send.php, I get the mail. But I can't call variable called
> > variable. it comes as string.
> > How can I correct this?
> >
>
> You need to use double quotes (or HEREDOC) if you want PHP to replace $variable with its value in the string:
>
> $message="
>
> There is a text $variable trial. ";
>
> or
>
> $message = << >
> There is a text $variable trial. MESSAGE;
>
>
>
> Andrew
>
> --
> PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
> No virus found in this outgoing message.
> Checked by AVG - www.avg.com
> Version: 8.5.423 / Virus Database: 270.14.24/2449 - Release Date: 10/20/09 18:42:00
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php


The {} only become really useful when you're trying to reference arrays
within a string:

$var = array('great', 'boring');

$text = "this is {$var[0]}.";

Without the curly braces, PHP wouldn't be able to figure out whether you
wanted the end string to be 'This is great.' or 'This is [0].' despite
the variable itself clearly being an array.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-zEf9ycVlHaRcT1WWdw6n--

RE: how call a variable in a text

am 21.10.2009 22:51:36 von David Murphy

--=======AVGMAIL-4B9E0F12=======
Content-Type: multipart/alternative;
boundary="----=_NextPart_000_11CB_01CA5266.5F0C3DA0"


This is a multipart message in MIME format.

------=_NextPart_000_11CB_01CA5266.5F0C3DA0
Content-Type: text/plain;
charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

True however K.I.S.S would say , if you can use it like

=20

=20

echo â€=9CThis is a statement {$Blah}.â€=9D;

echo â€=9CThis is also a statement {$objBlah->BlahString}.â€=9D;

echo â€=9CThis is also a statement =
{$tBlah[â€=98BlahKeyâ€=99]}.â€=9D;

=20

=20

You should do it so you are always using the same expected format, =
cleaner for readability and training other people to understand how you =
code.

=20

=20

This is my personal thoughts on it, everyone has their own prefs.

=20

David

=20

From: Ashley Sheridan [mailto:ash@ashleysheridan.co.uk]=20
Sent: Wednesday, October 21, 2009 3:43 PM
To: David Murphy
Cc: php-general@lists.php.net
Subject: RE: [PHP] how call a variable in a text

=20

On Wed, 2009-10-21 at 15:40 -0500, David Murphy wrote:=20

=20
This is actually much better the { and } make it very obvious where =
the variable is and also it can keep odd issues from occurring =
sometimes.
=20
$message=3D" There is a text {$variable} trial. ";
=20
There is always sprint type functions also.
=20
=20
David
=20
-----Original Message-----
From: Andrew Ballard [mailto:aballard@gmail.com]=20
Sent: Wednesday, October 21, 2009 3:23 PM
To: Bulend Kolay
Cc: php-general@lists.php.net
Subject: Re: [PHP] how call a variable in a text
=20
2009/10/21 Bulend Kolay :
> I 'll send a mail in html form using php5.
>=20
> cat send.php
> > $variable=3D"date1" ;
> ..
> ..
> $message=3D'
>=20
> There is a text $variable trial. ';
>=20
> mail($to, $subject, $message, $headers) ; ?>
>=20
> when I run send.php, I get the mail. But I can't call variable called=20
> variable. it comes as string.
> How can I correct this?
>=20
=20
You need to use double quotes (or HEREDOC) if you want PHP to replace =
$variable with its value in the string:
=20
$message=3D"
=20
There is a text $variable trial. ";
=20
or
=20
$message =3D << =20
There is a text $variable trial. MESSAGE;
=20
=20
=20
Andrew
=20
--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: =
http://www.php.net/unsub.php
No virus found in this outgoing message.
Checked by AVG - www.avg.com=20
Version: 8.5.423 / Virus Database: 270.14.24/2449 - Release Date: =
10/20/09 18:42:00
--=20
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


The {} only become really useful when you're trying to reference arrays =
within a string:

$var =3D array('great', 'boring');

$text =3D "this is {$var[0]}.";

Without the curly braces, PHP wouldn't be able to figure out whether you =
wanted the end string to be 'This is great.' or 'This is [0].' despite =
the variable itself clearly being an array.


Thanks,
Ash
http://www.ashleysheridan.co.uk



=20


------=_NextPart_000_11CB_01CA5266.5F0C3DA0
Content-Type: text/html;
charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

xmlns:o=3D"urn:schemas-microsoft-com:office:office" =
xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" =
xmlns=3D"http://www.w3.org/TR/REC-html40"> http-equiv=3DContent-Type content=3D"text/html; charset=3Dutf-8"> name=3DGenerator content=3D"Microsoft Word 14 (filtered =
medium)"> vlink=3Dpurple>

style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;color:#1F497=
D'>True however   K.I.S.S  would say , if  you can =
use it  like

style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;color:#1F497=
D'> 

style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;color:#1F497=
D'> 

style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;color:#1F497=
D'>echo â€=9CThis is a statement =
{$Blah}.â€=9D;

style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;color:#1F497=
D'>echo â€=9CThis is also a statement =
{$objBlah->BlahString}.â€=9D;

class=3DMsoNormal> style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;color:#1F497=
D'>echo â€=9CThis is also a statement =
{$tBlah[â€=98BlahKeyâ€=99]}.â€=9D;

class=3DMsoNormal> style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;color:#1F497=
D'> 

style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;color:#1F497=
D'> 

style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;color:#1F497=
D'>You should do it so you are always using the same expected format, =
cleaner for readability and training other people to understand how you =
code.

style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;color:#1F497=
D'> 

style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;color:#1F497=
D'> 

style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;color:#1F497=
D'>This is my personal thoughts on it, everyone has their own =
prefs.

style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;color:#1F497=
D'> 

style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;color:#1F497=
D'>David

style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;color:#1F497=
D'> 

style=3D'border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in =
0in 0in'>

style=3D'font-size:10.0pt;font-family:"Tahoma","sans-serif"' >From:=
=
Ashley Sheridan [mailto:ash@ashleysheridan.co.uk]
Sent: =
Wednesday, October 21, 2009 3:43 PM
To: David =
Murphy
Cc: php-general@lists.php.net
Subject: RE: =
[PHP] how call a variable in a text

class=3DMsoNormal> 

On Wed, =
2009-10-21 at 15:40 -0500, David Murphy wrote: =

 
This is actually much =
better  the {  and } make it very obvious where the =
 variable is and also it can keep odd issues from occurring =
sometimes.
       =
        =
$message=3D"<b> There is a text {$variable}  trial. =
</b> =
";
 
There is always =
sprint type functions =
also.
 
 <=
/pre>
David
 
-----Ori=
ginal Message-----
From: Andrew Ballard [
href=3D"mailto:aballard@gmail.com">mailto:aballard@gmail.com] =
Sent: Wednesday, October 21, 2009 3:23 =
PM
To: Bulend Kolay
Cc: 
href=3D"mailto:php-general@lists.php.net">php-general@lists.php.net
:p>
Subject: Re: [PHP] how call a variable in a =
text
 
2009/10/21 Bulend =
Kolay < href=3D"mailto:bmalik@ihlas.net.tr">bmalik@ihlas.net.tr>: p>
> I 'll send a mail in html form using =
php5.
> 
> cat =
send.php
> <?php
> =
$variable=3D"date1" ;
> =
...
> ..
> =
$message=3D'
> 
> =
<b> There is a text $variable  trial. </b> =
';
> 
> mail($to, =
$subject, $message, $headers) ; =
?>
> 
> when I =
run send.php, I get the mail. But I can't call variable called =
> variable. it comes as =
string.
> How can I correct =
this?
> 
 
:p>
You need to use double quotes (or HEREDOC) if you want PHP =
to replace $variable with its value in the =
string:
 
$message=3D"=
;
 
<b> There is a =
text $variable  trial. </b> =
";
 
or
re>
 
$message =3D =
<<<MESSAGE
 
<=
b> There is a text $variable  trial. </b> =
MESSAGE;
 
 
p>
 
Andrew
--
PHP General Mailing List =
() To =
unsubscribe, visit: href=3D"http://www.php.net/unsub.php">http://www.php.net/unsub.php p>
No virus found in this outgoing =
message.
Checked by AVG - 
href=3D"http://www.avg.com">www.avg.com =
Version: 8.5.423 / Virus Database: 270.14.24/2449 =
- Release Date: 10/20/09 18:42:00
-- =
PHP General Mailing List (
href=3D"http://www.php.net/">http://www.php.net/)
e>To unsubscribe, visit: href=3D"http://www.php.net/unsub.php">http://www.php.net/uns ub.php p>

style=3D'margin-bottom:12.0pt'>
The {} only become really useful when =
you're trying to reference arrays within a string:

$var =3D =
array('great', 'boring');

$text =3D "this is =
{$var[0]}.";

Without the curly braces, PHP wouldn't be able =
to figure out whether you wanted the end string to be 'This is great.' =
or 'This is [0].' despite the variable itself clearly being an =
array.

cellspacing=3D0 cellpadding=3D0 width=3D"100%" =
style=3D'width:100.0%'>

class=3DMsoNormal style=3D'margin-bottom:12.0pt'>Thanks,
Ash
href=3D"http://www.ashleysheridan.co.uk">http://www.ashleysh eridan.co.uk<=
/a>

class=3DMsoNormal> 


------=_NextPart_000_11CB_01CA5266.5F0C3DA0--

--=======AVGMAIL-4B9E0F12=======
Content-Type: multipart/alternative;
boundary="=======AVGMAIL-03DE5038======="

--=======AVGMAIL-03DE5038=======
Content-Type: text/plain; x-avg=cert; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Description: "AVG certification"


No virus found in this outgoing message.
Checked by AVG - www.avg.com
Version: 8.5.423 / Virus Database: 270.14.24/2449 - Release Date: 10/20/09 1=
8:42:00

--=======AVGMAIL-03DE5038=======--

--=======AVGMAIL-4B9E0F12=======
Content-Type: text/plain; charset=us-ascii

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--=======AVGMAIL-4B9E0F12=======--

RE: how call a variable in a text

am 21.10.2009 22:53:48 von Ashley Sheridan

--=-t1G0/v+cWGVhjwv6tc/d
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Wed, 2009-10-21 at 15:51 -0500, David Murphy wrote:
> True however K.I.S.S would say , if you can use it like
>=20
> =20
>=20
> =20
>=20
> echo â€=9CThis is a statement {$Blah}.â€=9D;
>=20
> echo â€=9CThis is also a statement {$objBlah->BlahString}.â€=9D;
>=20
> echo â€=9CThis is also a statement {$tBlah[â€=98BlahKeyâ€=99]=
}.â€=9D;
>=20
> =20
>=20
> =20
>=20
> You should do it so you are always using the same expected format,
> cleaner for readability and training other people to understand how
> you code.
>=20
> =20
>=20
> =20
>=20
> This is my personal thoughts on it, everyone has their own prefs.
>=20
> =20
>=20
> David
>=20
> =20
>=20
>=20
> From: Ashley Sheridan [mailto:ash@ashleysheridan.co.uk]=20
> Sent: Wednesday, October 21, 2009 3:43 PM
> To: David Murphy
> Cc: php-general@lists.php.net
> Subject: RE: [PHP] how call a variable in a text
>=20
>=20
>=20
> =20
>=20
> On Wed, 2009-10-21 at 15:40 -0500, David Murphy wrote:=20
>=20
>=20
> =20
> This is actually much better the { and } make it very obvious where the=
variable is and also it can keep odd issues from occurring sometimes.
> =20
> $message=3D" There is a text {$variable} trial. ";
> =20
> There is always sprint type functions also.
> =20
> =20
> David
> =20
> -----Original Message-----
> From: Andrew Ballard [mailto:aballard@gmail.com]=20
> Sent: Wednesday, October 21, 2009 3:23 PM
> To: Bulend Kolay
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] how call a variable in a text
> =20
> 2009/10/21 Bulend Kolay :
> > I 'll send a mail in html form using php5.
> >=20
> > cat send.php
> > > > $variable=3D"date1" ;
> > ..
> > ..
> > $message=3D'
> >=20
> > There is a text $variable trial. ';
> >=20
> > mail($to, $subject, $message, $headers) ; ?>
> >=20
> > when I run send.php, I get the mail. But I can't call variable called=20
> > variable. it comes as string.
> > How can I correct this?
> >=20
> =20
> You need to use double quotes (or HEREDOC) if you want PHP to replace $va=
riable with its value in the string:
> =20
> $message=3D"
> =20
> There is a text $variable trial. ";
> =20
> or
> =20
> $message =3D << > =20
> There is a text $variable trial. MESSAGE;
> =20
> =20
> =20
> Andrew
> =20
> --
> PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: htt=
p://www.php.net/unsub.php
> No virus found in this outgoing message.
> Checked by AVG - www.avg.com=20
> Version: 8.5.423 / Virus Database: 270.14.24/2449 - Release Date: 10/20/0=
9 18:42:00
> --=20
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>=20
>=20
>=20
> The {} only become really useful when you're trying to reference
> arrays within a string:
>=20
> $var =3D array('great', 'boring');
>=20
> $text =3D "this is {$var[0]}.";
>=20
> Without the curly braces, PHP wouldn't be able to figure out whether
> you wanted the end string to be 'This is great.' or 'This is [0].'
> despite the variable itself clearly being an array.
>=20
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>=20
>=20
>=20
>=20
>=20
> =20
>=20
>=20
>=20
> No virus found in this outgoing message.
> Checked by AVG - www.avg.com=20
> Version: 8.5.423 / Virus Database: 270.14.24/2449 - Release Date: 10/20/0=
9 18:42:00
> --=20
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php


I reckon that part is all down to personal preference. Interesting to
see it works on objects too though. I've not seen that before, I was
always breaking outside of the strings for that type of thing.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-t1G0/v+cWGVhjwv6tc/d--

Re: how call a variable in a text

am 21.10.2009 22:54:34 von Kim Madsen

Ashley Sheridan wrote on 2009-10-21 22:43:

> The {} only become really useful when you're trying to reference arrays
> within a string:
>
> $var = array('great', 'boring');
>
> $text = "this is {$var[0]}.";
>
> Without the curly braces, PHP wouldn't be able to figure out whether you
> wanted the end string to be 'This is great.' or 'This is [0].' despite
> the variable itself clearly being an array.

Ehh what? This has never been a problem for me:

$text = "this is $var[0].";

However this does give an error (or notice, don't recall, haven't seen
the error in quite a while):

$text = "this is $var['0'].";

In that case the solution is the curly brackets:

$text = "this is {$var['0']}.";

--
Kind regards
Kim Emax - masterminds.dk

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: how call a variable in a text

am 21.10.2009 22:56:28 von Ashley Sheridan

--=-dpk6s+bJBLl4fyw96CxY
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Wed, 2009-10-21 at 22:54 +0200, Kim Madsen wrote:

> Ashley Sheridan wrote on 2009-10-21 22:43:
>
> > The {} only become really useful when you're trying to reference arrays
> > within a string:
> >
> > $var = array('great', 'boring');
> >
> > $text = "this is {$var[0]}.";
> >
> > Without the curly braces, PHP wouldn't be able to figure out whether you
> > wanted the end string to be 'This is great.' or 'This is [0].' despite
> > the variable itself clearly being an array.
>
> Ehh what? This has never been a problem for me:
>
> $text = "this is $var[0].";
>
> However this does give an error (or notice, don't recall, haven't seen
> the error in quite a while):
>
> $text = "this is $var['0'].";
>
> In that case the solution is the curly brackets:
>
> $text = "this is {$var['0']}.";
>
> --
> Kind regards
> Kim Emax - masterminds.dk
>

Try this though:


$var = array(array('great','alright'), 'boring');

print "This is $var[0][0].";

?>

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-dpk6s+bJBLl4fyw96CxY--

Re: how call a variable in a text

am 21.10.2009 23:11:11 von Kim Madsen

Ashley Sheridan wrote on 2009-10-21 22:56:

> Try this though:
>
> >
> $var = array(array('great','alright'), 'boring');
>
> print "This is $var[0][0].";

Print "This is different from your previous example :-)";

--
Kind regards
Kim Emax - masterminds.dk

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: how call a variable in a text

am 21.10.2009 23:12:31 von Ashley Sheridan

--=-JXIhL7SYqGBqkAi1LaMX
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Wed, 2009-10-21 at 23:11 +0200, Kim Madsen wrote:

> Ashley Sheridan wrote on 2009-10-21 22:56:
>
> > Try this though:
> >
> > > >
> > $var = array(array('great','alright'), 'boring');
> >
> > print "This is $var[0][0].";
>
> Print "This is different from your previous example :-)";
>
> --
> Kind regards
> Kim Emax - masterminds.dk
>


Yeah, I just forgot that PHP will correctly work with an array to a
depth of 1 inside of a string. Much like the above advice, I'd taken to
always using {} for arrays inside of strings, although not for strings
inside of strings unless I needed non-white-space text to immediately
follow said string. Would make more sense if I used the braces for
everything, but nobody has ever accused me of making too much sense
before!

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-JXIhL7SYqGBqkAi1LaMX--

Re: how call a variable in a text

am 22.10.2009 02:24:43 von Shawn McKenzie

Ashley Sheridan wrote:
> On Wed, 2009-10-21 at 22:54 +0200, Kim Madsen wrote:
>
>> Ashley Sheridan wrote on 2009-10-21 22:43:
>>
>>> The {} only become really useful when you're trying to reference arrays
>>> within a string:
>>>
>>> $var = array('great', 'boring');
>>>
>>> $text = "this is {$var[0]}.";
>>>
>>> Without the curly braces, PHP wouldn't be able to figure out whether you
>>> wanted the end string to be 'This is great.' or 'This is [0].' despite
>>> the variable itself clearly being an array.
>> Ehh what? This has never been a problem for me:
>>
>> $text = "this is $var[0].";
>>
>> However this does give an error (or notice, don't recall, haven't seen
>> the error in quite a while):
>>
>> $text = "this is $var['0'].";
>>
>> In that case the solution is the curly brackets:
>>
>> $text = "this is {$var['0']}.";
>>
>> --
>> Kind regards
>> Kim Emax - masterminds.dk
>>
>
> Try this though:
>
> >
> $var = array(array('great','alright'), 'boring');
>
> print "This is $var[0][0].";
>
> ?>
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>

And since we are trying to cover all possible ways (works with double
quotes also):

$message = ' There is a text ' . $variable . ' trial. ';

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: how call a variable in a text

am 22.10.2009 08:01:53 von Samrat Kar

This is always safe to use variables like this...

$text = "This is " . $variable;

Or

$text = "This is " . $variable[0];

Regards,

Samrat Kar
FRD, BARC

Tel: 022-25597295
Alternate Email: esamrat@yahoo.com


-----Original Message-----
From: Kim Madsen [mailto:php.net@emax.dk]
Sent: Thursday, October 22, 2009 2:25 AM
To: ash@ashleysheridan.co.uk
Cc: David Murphy; php-general@lists.php.net
Subject: Re: [PHP] how call a variable in a text

Ashley Sheridan wrote on 2009-10-21 22:43:

> The {} only become really useful when you're trying to reference arrays
> within a string:
>
> $var = array('great', 'boring');
>
> $text = "this is {$var[0]}.";
>
> Without the curly braces, PHP wouldn't be able to figure out whether you
> wanted the end string to be 'This is great.' or 'This is [0].' despite
> the variable itself clearly being an array.

Ehh what? This has never been a problem for me:

$text = "this is $var[0].";

However this does give an error (or notice, don't recall, haven't seen
the error in quite a while):

$text = "this is $var['0'].";

In that case the solution is the curly brackets:

$text = "this is {$var['0']}.";

--
Kind regards
Kim Emax - masterminds.dk

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 8.5.423 / Virus Database: 270.14.25/2450 - Release Date: 10/21/09
16:44:00



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: how call a variable in a text

am 22.10.2009 08:53:52 von Goltsios Theodore

--------------020909000204080009080805
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit


> On Wed, 2009-10-21 at 23:11 +0200, Kim Madsen wrote:
>
>
>> Ashley Sheridan wrote on 2009-10-21 22:56:
>>
>>
>>> Try this though:
>>>
>>> >>>
>>> $var = array(array('great','alright'), 'boring');
>>>
>>> print "This is $var[0][0].";
>>>
>> Print "This is different from your previous example :-)";
>>
>> --
>> Kind regards
>> Kim Emax - masterminds.dk
>>
>>
>
>
> Yeah, I just forgot that PHP will correctly work with an array to a
> depth of 1 inside of a string. Much like the above advice, I'd taken to
> always using {} for arrays inside of strings, although not for strings
> inside of strings unless I needed non-white-space text to immediately
> follow said string. Would make more sense if I used the braces for
> everything, but nobody has ever accused me of making too much sense
> before!
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>
>

This will also work (shell like style):

print "Test: ${var[0]}";

but this won't based on the same principle you mention above:

print "Test: ${var[0][0]}";

--
Thodoris


--------------020909000204080009080805--

Re: how call a variable in a text

am 22.10.2009 11:13:46 von Ashley Sheridan

--=-pyFWL3fV4nsvk2+eDXmr
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Thu, 2009-10-22 at 09:53 +0300, Thodoris wrote:

> > On Wed, 2009-10-21 at 23:11 +0200, Kim Madsen wrote:
> >
> >
> >> Ashley Sheridan wrote on 2009-10-21 22:56:
> >>
> >>
> >>> Try this though:
> >>>
> >>> > >>>
> >>> $var = array(array('great','alright'), 'boring');
> >>>
> >>> print "This is $var[0][0].";
> >>>
> >> Print "This is different from your previous example :-)";
> >>
> >> --
> >> Kind regards
> >> Kim Emax - masterminds.dk
> >>
> >>
> >
> >
> > Yeah, I just forgot that PHP will correctly work with an array to a
> > depth of 1 inside of a string. Much like the above advice, I'd taken to
> > always using {} for arrays inside of strings, although not for strings
> > inside of strings unless I needed non-white-space text to immediately
> > follow said string. Would make more sense if I used the braces for
> > everything, but nobody has ever accused me of making too much sense
> > before!
> >
> > Thanks,
> > Ash
> > http://www.ashleysheridan.co.uk
> >
> >
> >
> >
>
> This will also work (shell like style):
>
> print "Test: ${var[0]}";
>
> but this won't based on the same principle you mention above:
>
> print "Test: ${var[0][0]}";
>

Erm, the braces are meant to go *around* the variable, not around a bit
of it:

print "Test: {$var[0][0]}";

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-pyFWL3fV4nsvk2+eDXmr--

RE: how call a variable in a text

am 22.10.2009 11:19:51 von Andrea Giammarchi

--_6eb932d1-e2db-467e-b594-9f3564b16672_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable


Curly brackets are usually highlighted as well so it is a good practice=2C =
generally speaking=2C to use them for double quoted strings and maintainabi=
lity increase automatically.

Regards

> From: esamrat@barc.gov.in
> To: php-general@lists.php.net
> Date: Thu=2C 22 Oct 2009 11:31:53 +0530
> Subject: RE: [PHP] how call a variable in a text
>=20
> This is always safe to use variables like this...
>=20
> $text =3D "This is " . $variable=3B
>=20
> Or
>=20
> $text =3D "This is " . $variable[0]=3B
>=20
> Regards=2C
>=20
> Samrat Kar
> FRD=2C BARC
>=20
> Tel: 022-25597295
> Alternate Email: esamrat@yahoo.com
>=20
>=20
> -----Original Message-----
> From: Kim Madsen [mailto:php.net@emax.dk]=20
> Sent: Thursday=2C October 22=2C 2009 2:25 AM
> To: ash@ashleysheridan.co.uk
> Cc: David Murphy=3B php-general@lists.php.net
> Subject: Re: [PHP] how call a variable in a text
>=20
> Ashley Sheridan wrote on 2009-10-21 22:43:
>=20
> > The {} only become really useful when you're trying to reference arrays
> > within a string:
> >=20
> > $var =3D array('great'=2C 'boring')=3B
> >=20
> > $text =3D "this is {$var[0]}."=3B
> >=20
> > Without the curly braces=2C PHP wouldn't be able to figure out whether =
you
> > wanted the end string to be 'This is great.' or 'This is [0].' despite
> > the variable itself clearly being an array.
>=20
> Ehh what? This has never been a problem for me:
>=20
> $text =3D "this is $var[0]."=3B
>=20
> However this does give an error (or notice=2C don't recall=2C haven't see=
n=20
> the error in quite a while):
>=20
> $text =3D "this is $var['0']."=3B
>=20
> In that case the solution is the curly brackets:
>=20
> $text =3D "this is {$var['0']}."=3B
>=20
> --=20
> Kind regards
> Kim Emax - masterminds.dk
>=20
> --=20
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe=2C visit: http://www.php.net/unsub.php
>=20
> No virus found in this incoming message.
> Checked by AVG - www.avg.com=20
> Version: 8.5.423 / Virus Database: 270.14.25/2450 - Release Date: 10/21/0=
9
> 16:44:00
>=20
>=20
>=20
> --=20
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe=2C visit: http://www.php.net/unsub.php
>=20
=20
____________________________________________________________ _____
Windows Live Hotmail: Your friends can get your Facebook updates=2C right f=
rom Hotmail=AE.
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_4:092=
009=

--_6eb932d1-e2db-467e-b594-9f3564b16672_--

Re: how call a variable in a text

am 22.10.2009 11:31:33 von Goltsios Theodore

--------------030609070007080107060009
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit



> Erm, the braces are meant to go *around* the variable, not around a
> bit of it:
>
> print "Test: {$var[0][0]}";
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>

In many cases braces can go around the variable name not the necessarily
around the whole variable (like the bash scripts). Those are coming from
the manual:

${date("M")} = "Worked";
echo ${date("M")};
?>

function test() {
// NULL -- not what initially expected
$string = '_POST';
var_dump(${$string});

// Works as expected
var_dump(${'_POST'});

// Works as expected
global ${$string};
var_dump(${$string});

}

So no they are not meant to go around. You can use them this way as well.

--
Thodoris


--------------030609070007080107060009--

RE: how call a variable in a text

am 22.10.2009 11:32:29 von Andrea Giammarchi

--_13a96a04-3af0-4aef-94ba-d5021d3e34a0_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable



> Erm=2C the braces are meant to go *around* the variable=2C not around a b=
it
> of it:
>=20
> print "Test: {$var[0][0]}"=3B

unrelated=2C just another usage of curly brackets

$_ =3D 'abc'=3B
$i =3D 0=3B
echo $_{++$i}=3B // b

Regards
=20
____________________________________________________________ _____
Windows Live: Friends get your Flickr=2C Yelp=2C and Digg updates when they=
e-mail you.
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_3:092=
010=

--_13a96a04-3af0-4aef-94ba-d5021d3e34a0_--

RE: how call a variable in a text

am 22.10.2009 11:36:21 von Andrea Giammarchi

--_f9c2dbc3-6dc3-4323-a858-7ecf86afc9a3_
Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable



> So no they are not meant to go around. You can use them this way as well.

that has almost the same meaning of=20

$_ =3D '_POST'=3B
echo count($$_)=3B

which again=2C for readability brackets are suggested to improve maintainab=
ility

$_ =3D '_POST'=3B
echo count(${$_})=3B

Regards=20
=20
____________________________________________________________ _____
Windows Live: Make it easier for your friends to see what you=92re up to on=
Facebook.
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_2:092=
009=

--_f9c2dbc3-6dc3-4323-a858-7ecf86afc9a3_--

Re: how call a variable in a text

am 22.10.2009 11:46:49 von Goltsios Theodore

--------------010700040406040204020703
Content-Type: text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding: 7bit


>
> > So no they are not meant to go around. You can use them this way as
> well.
>
> that has almost the same meaning of
>
> $_ = '_POST';
> echo count($$_);
>
> which again, for readability brackets are suggested to improve
> maintainability
>
> $_ = '_POST';
> echo count(${$_});
>
> Regards
>
> ------------------------------------------------------------ ------------
>

I don't think it is about readability:

$arr[3] = 'test';
$test = 3;

//This prints "$test"
echo "This doesn't work: $$arr[3]";

//This prints 3
echo "This works: ${$arr[3]}";

Using the same type way as before in this thread.

My point is that in the curly braces you protect the way the evaluation
is going to be made into the string. So you can put them anywhere as
long as it is meaningful.

See some examples:

http://php.net/manual/en/language.types.string.php

in the complex (curly) syntax section.

--
Thodoris


--------------010700040406040204020703--

Re: how call a variable in a text

am 22.10.2009 11:51:59 von Goltsios Theodore

>
>
> Using the same type way as before in this thread.
>
>

This was supposed to come out as "using the same way of thinking".

But the English->Nerdish dictionary came out...

--
Thodoris


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: how call a variable in a text

am 22.10.2009 13:18:57 von Andrea Giammarchi

--_db03778e-66fc-4b3f-b1a5-146476e654ab_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable



> I don't think it is about readability:
>=20
> $arr[3] =3D 'test'=3B
> $test =3D 3=3B
>=20
> //This prints "$test"
> echo "This doesn't work: $$arr[3]"=3B
>=20
> //This prints 3
> echo "This works: ${$arr[3]}"=3B
>=20
> Using the same type way as before in this thread.

Above example is a classic one where readability and maintainability deal w=
ell together.

First of all everything works as expected but obviously you need to know wh=
at you need.

It is ambiguous to write $$arr[3] ... what do you expect?

Did you mean the variable derived by $arr[3]?
echo "This works: {$$arr[3]}"=3B
since curly brackets make the meaning of the expression explicit=2C it will=
be 3 indeed.

What is the less ambiguous=2C readable=2C easy to maintain=2C way to obtain=
that result?

echo "This works: {${$arr[3]}}"=3B

If our aim is to get the variable with name equal to the value of $arr[3]

Can you see now why I am talking about good practice? Zero ambiguity=2C and=
that's how I like to code

Regards
=20
____________________________________________________________ _____
Windows Live: Friends get your Flickr=2C Yelp=2C and Digg updates when they=
e-mail you.
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_3:092=
010=

--_db03778e-66fc-4b3f-b1a5-146476e654ab_--

Re: how call a variable in a text

am 22.10.2009 13:40:37 von Goltsios Theodore

--------------010000030801050300070909
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit


>
>> I don't think it is about readability:
>>
>> $arr[3] = 'test';
>> $test = 3;
>>
>> //This prints "$test"
>> echo "This doesn't work: $$arr[3]";
>>
>> //This prints 3
>> echo "This works: ${$arr[3]}";
>>
>> Using the same type way as before in this thread.
>>
>
> Above example is a classic one where readability and maintainability deal well together.
>
> First of all everything works as expected but obviously you need to know what you need.
>
> It is ambiguous to write $$arr[3] ... what do you expect?]
>

No I don't think it is. It produces "$test" and if this is what you need
echo it works nice :-) .

> Did you mean the variable derived by $arr[3]?
> echo "This works: {$$arr[3]}";
> since curly brackets make the meaning of the expression explicit, it will be 3 indeed.
>
> What is the less ambiguous, readable, easy to maintain, way to obtain that result?
>
> echo "This works: {${$arr[3]}}";
>
> If our aim is to get the variable with name equal to the value of $arr[3]
>
> Can you see now why I am talking about good practice? Zero ambiguity, and that's how I like to code
>
> Regards
>
>

Although I totally agree with the way of thinking and it is my style as
well.

But I though that the point of the thread was to present ways of putting
vars inside strings...

--
Thodoris


--------------010000030801050300070909--

Re: how call a variable in a text

am 22.10.2009 16:24:41 von Martin Scotta

--00032555b3a22d29e3047686db8b
Content-Type: text/plain; charset=ISO-8859-1

On Thu, Oct 22, 2009 at 8:40 AM, Thodoris wrote:

>
>
>>
>>> I don't think it is about readability:
>>>
>>> $arr[3] = 'test';
>>> $test = 3;
>>>
>>> //This prints "$test"
>>> echo "This doesn't work: $$arr[3]";
>>>
>>> //This prints 3
>>> echo "This works: ${$arr[3]}";
>>>
>>> Using the same type way as before in this thread.
>>>
>>>
>>
>> Above example is a classic one where readability and maintainability deal
>> well together.
>>
>> First of all everything works as expected but obviously you need to know
>> what you need.
>>
>> It is ambiguous to write $$arr[3] ... what do you expect?]
>>
>>
>
> No I don't think it is. It produces "$test" and if this is what you need
> echo it works nice :-) .
>
> Did you mean the variable derived by $arr[3]?
>> echo "This works: {$$arr[3]}";
>> since curly brackets make the meaning of the expression explicit, it will
>> be 3 indeed.
>>
>> What is the less ambiguous, readable, easy to maintain, way to obtain that
>> result?
>>
>> echo "This works: {${$arr[3]}}";
>>
>> If our aim is to get the variable with name equal to the value of $arr[3]
>>
>> Can you see now why I am talking about good practice? Zero ambiguity, and
>> that's how I like to code
>>
>> Regards
>>
>>
>>
>
> Although I totally agree with the way of thinking and it is my style as
> well.
>
> But I though that the point of the thread was to present ways of putting
> vars inside strings...
>
> --
> Thodoris
>
>
PHP knows that before $ and {} there is a variable name.... so, this is
completely correct for PHP


$name = 'Martin';
$var = 'name';

echo "$name {$name} ${name} ${ 'name' } ${$var} {$$var} ${${ 'var' }}"
,PHP_EOL;
// am I missing any other way here?

// this is non-sense, but funny :)
$a = 'b';
$b = 'c';
$c = 'd';
$d = 'e';
$e = 'a';

$swap = 'a';

for($i=0,$e=rand(1, 100); $i<$e; ++$i)
$swap = ${ $swap };

echo $swap, PHP_EOL;

I feel ${ $var } safer for developers than $$var because I feel it more
readable.
$$var can be interpreted by a typo for somebody, while with ${ $var } there
is no doubt what was the coder intent.


--
Martin Scotta

--00032555b3a22d29e3047686db8b--