Just Learning PHP

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 ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-05-2007
Blue_Hatter
 
Posts: n/a
Default Just Learning PHP

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?
Reply With Quote
  #2 (permalink)  
Old 09-05-2007
Ron Barnett
 
Posts: n/a
Default Re: Just Learning PHP

"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







Reply With Quote
  #3 (permalink)  
Old 09-06-2007
macca
 
Posts: n/a
Default Re: Just Learning PHP


> <?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?

Reply With Quote
  #4 (permalink)  
Old 09-06-2007
Tim Streater
 
Posts: n/a
Default Re: Just Learning PHP

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.
Reply With Quote
  #5 (permalink)  
Old 09-06-2007
Ian Hobson
 
Posts: n/a
Default Re: Just Learning PHP

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





Reply With Quote
  #6 (permalink)  
Old 09-07-2007
macca
 
Posts: n/a
Default Re: Just Learning PHP

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?

Reply With Quote
  #7 (permalink)  
Old 09-07-2007
Erwin Moller
 
Posts: n/a
Default Re: Just Learning PHP

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
Reply With Quote
  #8 (permalink)  
Old 09-07-2007
Kenoli
 
Posts: n/a
Default Re: Just Learning PHP

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

Reply With Quote
  #9 (permalink)  
Old 09-07-2007
Ian Hobson
 
Posts: n/a
Default Re: Just Learning PHP

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
Reply With Quote
  #10 (permalink)  
Old 09-08-2007
Ian Hobson
 
Posts: n/a
Default Re: Just Learning PHP

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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:50 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0