This is a discussion on Just Learning PHP within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I know a thing or two about HTML writing, what I want to know is how to get the simple ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
"Blue_Hatter" <Bluehatchet@Gmail.com> wrote in message
news:fblk10$ovb$1@aioe.org... >I know a thing or two about HTML writing, what I want to know is how to get >the simple PHP scripts I am writing to match the HTML formatting I want on >my Page? PHP can output whatever you want it to : <?php echo "<a href='www.mydomain.com'>Click here to visit my site</a>"; ?> is pretty much the same as <a href='www.mydomain.com'>Click here to visit my site</a> and you can add variables : <?php $fred = 'Click here to visit my site; echo "<a href='www.mydomain.com'>$fred</a>"; ?> because the variable $fred is inside double quotes it will get expanded to its string value on output. If you are looking for more powerful formatting features try phpclasses : -- http://ronbarnett.users.phpclasses.org/ or the pear extensions on PHP.net If you do understand HTML using PHP to tweak it and make pages dynamic is easy. Cheers Ron |
|
|||
|
In article <1189090148.229899.215350@w3g2000hsg.googlegroups. com>,
macca <ptmcnally@googlemail.com> wrote: > > <?php > > $fred = 'Click here to visit my site; > > echo "<a href='www.mydomain.com'>$fred</a>"; > > ?> > > > > Shouldnt that be : > > echo "<a href='www.mydomain.com'>{$fred}</a>"; > > > Seriously, What's the deal with using brackets inside am > interpolation? I always do this: echo "<a href='www.mydomain.com'>" . $fred . "</a>"; Then it's a lot clearer (to me, at any rate), due to reduced nesting. |
|
|||
|
macca wrote:
>> <?php >> $fred = 'Click here to visit my site; >> echo "<a href='www.mydomain.com'>$fred</a>"; >> ?> > > > > Shouldnt that be : > > echo "<a href='www.mydomain.com'>{$fred}</a>"; > > > Seriously, What's the deal with using brackets inside am > interpolation? > If by "Whats the deal?" you want to know why they are used.... Both versions will work inside a double quotes string (or here doc). Single quoted strings are not expanded. If the variable reference is compound (e.g. $a[4], $this->var) then braces are necessary. [echo "<a href='www.mydomain.com'>$fred</a>";] is essentially the same speed as [echo "<a href='www.mydomain.com'>{$fred}</a>";] while [echo "<a href='www.mydomain.com'>".$fred."</a>"; ] is slower. Leaving braces out when they are not necessary makes the code clearer. If you mean "Why shouldn't I do it?"... Its your code. <shrug> Regards Ian |
|
|||
|
Ok, thanks Ian.
I did a little reading on compound variables. The problem I was'nt quite understanding is where the braces are needed, as the string "<a href=\"page.htm\">$var</a>"; has other characters (HTML) directly after the name of the variable without any spaces. Does this not matter? |
|
|||
|
Tim Streater wrote:
> In article <1189090148.229899.215350@w3g2000hsg.googlegroups. com>, > macca <ptmcnally@googlemail.com> wrote: > >>> <?php >>> $fred = 'Click here to visit my site; >>> echo "<a href='www.mydomain.com'>$fred</a>"; >>> ?> >> >> >> Shouldnt that be : >> >> echo "<a href='www.mydomain.com'>{$fred}</a>"; >> >> >> Seriously, What's the deal with using brackets inside am >> interpolation? > > I always do this: > > echo "<a href='www.mydomain.com'>" . $fred . "</a>"; > > Then it's a lot clearer (to me, at any rate), due to reduced nesting. Same here. If the expression is complex I prefer jumping out of the string. When it is a simple variable, I often put it in the string. I prefer readability of code always over speed (if it matters at all). And when you need a lot of HTML, just jump out of PHP, like: <?php // some db action ?> <table> <tr> <td> <?php echo $row["name"]; ?> </td> <td> <?php echo $row["favcolor"]; ?> </td> </tr> </table> <?php // back into PHP ?> That avoids the messy echos. But in the end, I think, it is a matter of taste. Regards, Erwin Moller |
|
|||
|
I have faced confusing situations using curly braces around an array,
as well as quotes within square brackets in an array. I wish had hard examples to offer, but perhaps someone knows the general rule. I have had expressions involving arrays refuse to work when I put curly braces around them and then work when I removed them. Other times the expression doesn't work until I add them. Similarly with arrays, at times I have had to add and at other times remove quotes within square brackets in order to get the expression to work ($array['key']). I have never been able to derive a consistent rule for either of these two constructs. Does anyone know the rules here for when one or the other is required or not allowed? --Kenoli |
|
|||
|
macca wrote:
> Ok, thanks Ian. > > I did a little reading on compound variables. > > The problem I was'nt quite understanding is where the braces are > needed, > > as the string > > "<a href=\"page.htm\">$var</a>"; > > has other characters (HTML) directly after the name of the variable > without any spaces. > > Does this not matter? > Hi Macca, No - the parser will recognise the $ and what follows it as a variable name, and will stop with the next character that can't be part of a variable name (IIRC only letters, digits, and _ are allowed). thus $var = "fred"; $array[2] = "bill"; echo ">$var<"; // = '>fred<' echo ">$array[2]<"; // error ($array is not convertible to a string.) echo ">{$array[2]}<" // = '>bill<' Regards Ian |
|
|||
|
Kenoli wrote:
> > I have never been able to derive a consistent rule for either of these > two constructs. > > Does anyone know the rules here for when one or the other is required > or not allowed? > Hi Kenoli, It is explained very well in the manual - with examples. The key part about curly braces is here. http://www.php.net/manual/en/languag...arsing.complex Regards Ian |