 |
 |
|
 |
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
I just learned PHP. I feel unclean...
[Seriously. The *introductory tutorial* is littered with warnings about
obscure functionallity which used to be broken but now isn't, and
strange features that used to work one way but now work a slightly
different way. I'm almost scared to write anything with this...]
Still, for some reason PHP is extremely popular, so I guess it would be
useful to learn it. Certainly if most of the complex control logic is
implemented in a real programming language, PHP looks quite good for
stitching complex blocks of HTML together with relative ease.
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Invisible wrote:
> I just learned PHP. I feel unclean...
And here's a small pointless toy I just developed...
<html>
<head>
<title>PHP Test #4 (Output)</title>
</head>
<body>
<h1>PHP Test #4 (Output)</h1>
<p>
<a href="Test4-In.html">Return to input form</a>
</p>
<h2>Input</h2>
<pre><?php echo htmlspecialchars($_POST['code']); ?></pre>
<p>Length: <?php echo strlen($_POST['code']); ?> characters.</p>
<h2>Frequency Table</h2>
<?php
$table = array();
$in = $_POST['code'];
for ($pos=0; $pos < strlen($in); $pos++)
{
$symbol = $in[$pos];
if (isset($table[$symbol])) $table[$symbol] = $table[$symbol] + 1;
else $table[$symbol] = 1;
}
?>
<table border="1">
<tr>
<th>Symbol</th>
<th>Count</th>
<th>Probability</th>
<th>Bits</th>
</tr>
<?php
foreach ($table as $symbol => $count)
{
echo '<tr><td align="center">‘<code>';
echo htmlspecialchars($symbol);
echo '</code>’</td><td align="right">';
echo $count;
echo '</td><td align="right">';
echo (int)($count / strlen($in) * 100);
echo '%</td><td align="right">';
echo abs(log($count / strlen($in)) / log(2));
echo '</td></tr>';
echo "\n";
}
?>
<tr>
<td align="center">Total</td>
<td align="right"><?php echo strlen($in); ?></td>
<td align="right">100%</td>
<td align="right">0</td>
</tr>
</table>
</body>
</html>
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Invisible <voi### [at] dev null> wrote:
> Still, for some reason PHP is extremely popular, so I guess it would be
> useful to learn it.
You won't like it. It looks a bit like C++.
--
- Warp
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Invisible <voi### [at] dev null> wrote:
> And here's a small pointless toy I just developed...
Don't mix HTML and PHP. That's just asking for maintenance nightmares.
> echo '<tr><td align="center">‘<code>';
> echo htmlspecialchars($symbol);
> echo '</code>’</td><td align="right">';
> echo $count;
> echo '</td><td align="right">';
> echo (int)($count / strlen($in) * 100);
> echo '%</td><td align="right">';
> echo abs(log($count / strlen($in)) / log(2));
> echo '</td></tr>';
> echo "\n";
You don't need to make a separate 'echo' for each of those things.
--
- Warp
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Warp wrote:
> Don't mix HTML and PHP. That's just asking for maintenance nightmares.
I thought that was the design goal of PHP?
[Er, to be mixed with HTML, that is.]
>> echo '<tr><td align="center">‘<code>';
>> echo htmlspecialchars($symbol);
>> echo '</code>’</td><td align="right">';
>> echo $count;
>> echo '</td><td align="right">';
>> echo (int)($count / strlen($in) * 100);
>> echo '%</td><td align="right">';
>> echo abs(log($count / strlen($in)) / log(2));
>> echo '</td></tr>';
>> echo "\n";
>
> You don't need to make a separate 'echo' for each of those things.
You do if you only have a 40x40 character text window and a text editor
which malfunctions on long lines...
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Warp wrote:
> Invisible <voi### [at] dev null> wrote:
>> Still, for some reason PHP is extremely popular, so I guess it would be
>> useful to learn it.
>
> You won't like it. It looks a bit like C++.
Quoting somebody else:
"Haskell is really bad - it makes you hate normal languages."
But then, I never really liked scripting languages much, and that was
way before Haskell, so... ;-)
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Invisible <voi### [at] dev null> wrote:
> Warp wrote:
> > Don't mix HTML and PHP. That's just asking for maintenance nightmares.
> I thought that was the design goal of PHP?
Maybe it was, but in practice it's usually a bad idea. I have seen
horrible, horrible examples.
This may be an acceptable exception:
<?php
... lots of PHP code here
?>
<html>
...
<?php (something which calls the code above) ?>
...
</html>
> > You don't need to make a separate 'echo' for each of those things.
> You do if you only have a 40x40 character text window and a text editor
> which malfunctions on long lines...
No you don't:
echo
'hello ' .
$user .
", how are you?\n";
At least it saves you from writing 'echo' a million times.
--
- Warp
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Warp wrote:
>>> Don't mix HTML and PHP. That's just asking for maintenance nightmares.
>
>> I thought that was the design goal of PHP?
>
> Maybe it was, but in practice it's usually a bad idea. I have seen
> horrible, horrible examples.
I can imagine...
The PHP tutorial explains how you can do "really cool" things like this:
<?php if ($somecondition) { ?>
Lots of HTML here...
<?php } else { ?>
lots more HTML here...
<?php } ?>
Which... uh... well I can imagine it becoming messy quite fast.
> This may be an acceptable exception:
>
> <?php
> ... lots of PHP code here
> ?>
>
> <html>
> ...
> <?php (something which calls the code above) ?>
> ...
> </html>
Yeah, that seems more like the model to follow. (In fact, some of the
user comments I've seen suggest that all the "real code" should be in
include files instead and just called from the HTML pages.)
>>> You don't need to make a separate 'echo' for each of those things.
>
>> You do if you only have a 40x40 character text window and a text editor
>> which malfunctions on long lines...
>
> No you don't:
>
> echo
> 'hello ' .
> $user .
> ", how are you?\n";
Ooo... you can do that?
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Invisible escribió:
> Warp wrote:
>> No you don't:
>>
>> echo
>> 'hello ' .
>> $user .
>> ", how are you?\n";
>
> Ooo... you can do that?
>
Yes, and you can also pass multiple arguments to 'echo' (ie. what Warp
said, but with a comma instead of the concat operator). It's probably a
millisecond faster to do it that way too.
http://blog.libssh2.org/index.php?/archives/28-How-long-is-a-piece-of-string.html
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
"Invisible" <voi### [at] dev null> a écrit dans le message de news:
47a87b06@news.povray.org...
> Warp wrote:
>
>> Don't mix HTML and PHP. That's just asking for maintenance nightmares.
>
> I thought that was the design goal of PHP?
I second Warp. You can mix HTML and PHP in the same page but it's likely to
turn into some horrible mess before you know it.
The trick is to build long text strings in PHP (that include the HTML tags
and everything you want to see on screen) and then echo the result.
G.
--
**********************
http://www.oyonale.com
**********************
- Graphic experiments
- POV-Ray and Poser computer images
- Posters
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Gilles Tran wrote:
> I second Warp. You can mix HTML and PHP in the same page but it's likely to
> turn into some horrible mess before you know it.
I can believe that...
> The trick is to build long text strings in PHP (that include the HTML tags
> and everything you want to see on screen) and then echo the result.
Really? It seems being able to write a basically normal HTML page with
just the odd snippet of PHP here and there to insert some computed value
is the main appeal of the language. Trying to build giant strings tends
to be awkward.
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Gilles Tran <gitran_nospam_@wanadoo.fr> wrote:
> The trick is to build long text strings in PHP (that include the HTML tags
> and everything you want to see on screen) and then echo the result.
Even if some library or whatever just echoes/prints everything it
produces (instead of returning a string), there's a way to redirect
that printing to a string. (I don't remember now the exact command
for that, though.)
--
- Warp
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Warp escribió:
> Gilles Tran <gitran_nospam_@wanadoo.fr> wrote:
>> The trick is to build long text strings in PHP (that include the HTML tags
>> and everything you want to see on screen) and then echo the result.
>
> Even if some library or whatever just echoes/prints everything it
> produces (instead of returning a string), there's a way to redirect
> that printing to a string. (I don't remember now the exact command
> for that, though.)
>
ob_start();
function_that_prints_stuff();
$string = ob_get_clean();
ob_get_clean is like getting the string with ob_get_contents, and then
throwing away the buffer with ob_end_clean. There is a dozen functions
for output buffering, so be sure to rtfm :)
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Invisible wrote:
> I just learned PHP. I feel unclean...
Good! That means you have a sense of elegance. :-) But we knew that.
> [Seriously. The *introductory tutorial* is littered with warnings about
> obscure functionallity which used to be broken but now isn't, and
> strange features that used to work one way but now work a slightly
> different way. I'm almost scared to write anything with this...]
Yes. You should be.
That and the complete clusterfuck that the naming of all the
functionality has.
> Still, for some reason PHP is extremely popular,
Because, sadly, it has huge quivering gobs of useful libraries in it.
> stitching complex blocks of HTML together with relative ease.
Don't do that on a real project. Use actual data-like templates. The
problem is that the person writing the code isn't the person who decides
what goes in the HTML.
--
Darren New / San Diego, CA, USA (PST)
On what day did God create the body thetans?
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Invisible escribió:
> stitching complex blocks of HTML together with relative ease.
http://www.odi.ch/prog/design/php/index.php
http://www.odi.ch/prog/design/php/guide.php
I disagree with "no reason to use PHP5". PHP5 has a lot of great
features (most of what the previous section says PHP4 lacks), and PHP4
is officially unmaintained since 01/01/2008.
"As PHP does not have a namespace facility like Java packages"
PHP6 will. WAY too late, but credits to them...
All the rest is quite good advice IMHO.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
>> I just learned PHP. I feel unclean...
>
> Good! That means you have a sense of elegance. :-) But we knew that.
;-)
>> I'm almost scared to write anything with this...
>
> Yes. You should be.
Hmm, well, it's not so bad if you purposely avoid all the unpredictable
edge cases. But what if you have to work on somebody else's code? My
God, it doesn't bear thinking about...
> That and the complete clusterfuck that the naming of all the
> functionality has.
Beings a whole new being to "it's massively clustered". ;-)
>> Still, for some reason PHP is extremely popular,
>
> Because, sadly, it has huge quivering gobs of useful libraries in it.
Yes, I noticed that.
In Haskell, accessing a database involves downloading source code,
trying to sort out a huge undocumented dependency hell, trying to make
the thing compile and using voodoo dolls. And then you *might* be able
to access a database.
In PHP, you just... install PHP. And it works. Immediately.
Gee, which one would you choose?
I guess this is why we have Type-I and Type-II languages. *sigh*
>> stitching complex blocks of HTML together with relative ease.
>
> Don't do that on a real project. Use actual data-like templates. The
> problem is that the person writing the code isn't the person who decides
> what goes in the HTML.
Hmm. That sounds like an interesting problem to attempt to solve...
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Invisible wrote:
> I just learned PHP. I feel unclean...
Ooo... POST data has all single-quote characters escaped by prefixing
with a backslash, and PHP doesn't unescape them before giving the string
back to you. That's a nice feature.
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Nicolas Alvarez wrote:
> http://www.odi.ch/prog/design/php/index.php
"Uhm.. err.. I have never thought about this you may anser now. And that
is exactly the point! Any unwary programmer will write codde like the
line above and introduce severe security and stability problems in his
code."
Irony, much? You're talking about unwary programmers and you apparently
haven't spent much effort proof-reading what you just typed. ;-)
"No major other language requires you to redeclare global variables
inside a function..."
Apparently Tcl isn't "major". ;-) It has precisely the same irritating
feature.
"I am writing this guide to provide useful ways how to best structure
your code."
Not your sentences, then?
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Invisible wrote:
>> Don't do that on a real project. Use actual data-like templates. The
>> problem is that the person writing the code isn't the person who
>> decides what goes in the HTML.
>
> Hmm. That sounds like an interesting problem to attempt to solve...
Meh. I use a templating thing. I have a call to which you pass a
template name, a style, an array of name->value pairs, and an array of
name->function mappings.
The template name and style maps (eventually) to a file, with stuff like
<p>Welcome, <{name}>!</p>
<p><red><{{errormessage}}>></red></p>
Lines with <{{stuff}}> where there's no "stuff" key in the name->value
array disappear completely. The rest get substituted. If the name has a
"*" on the end, it doesn't get HTML-escaped, so you can pass raw HTML
from the code. The name->function lets you do things like "display
values greater than five minutes in red" or "NULL turns into 'N/A'"
On top of that, I built one that takes a head, row, and foot template,
as well as a record resource (as returned by mysql_query()) and
generates the whole page.
--
Darren New / San Diego, CA, USA (PST)
On what day did God create the body thetans?
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Invisible wrote:
> Apparently Tcl isn't "major". ;-) It has precisely the same irritating
> feature.
It's not irritating, it's modular! ;-)
Seriously, if you don't declare variables first, there really isn't a
particularly good way of creating a global from inside a procedure, is
there?
--
Darren New / San Diego, CA, USA (PST)
On what day did God create the body thetans?
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Darren New escribió:
> Invisible wrote:
>> Apparently Tcl isn't "major". ;-) It has precisely the same irritating
>> feature.
>
> It's not irritating, it's modular! ;-)
>
> Seriously, if you don't declare variables first, there really isn't a
> particularly good way of creating a global from inside a procedure, is
> there?
>
$GLOBALS['varname'] = "stuff";
(no, I'm not being serious)
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Darren New wrote:
> Seriously, if you don't declare variables first, there really isn't a
> particularly good way of creating a global from inside a procedure, is
> there?
Yeah there is: Declare variables first. :-P
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Invisible wrote:
> Darren New wrote:
>
>> Seriously, if you don't declare variables first, there really isn't a
>> particularly good way of creating a global from inside a procedure, is
>> there?
>
> Yeah there is: Declare variables first. :-P
Sure. That's what [global] gets you.
Anyway, [global] is a special case of [upvar] and just basically has
default arguments. Complaining you have to use [global] in Tcl is silly.
PHP has no [upvar], and PHP has "superglobals" which you don't have to
redeclare inside procedures, so it's just a mess.
--
Darren New / San Diego, CA, USA (PST)
On what day did God create the body thetans?
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
On 5 Feb 2008 10:36:30 -0500, Warp wrote:
> echo
> 'hello ' .
> $user .
> ", how are you?\n";
Or rather:
echo
'hello ',
$user,
", how are you?\n";
Or in this case:
echo "Hello, $user, how are you?\n";
Interesting article touching the issue:
http://blog.libssh2.org/index.php?/archives/28-How-long-is-a-piece-of-string.html
--
Joel Yliluoma - http://iki.fi/bisqwit/
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
On Tue, 05 Feb 2008 13:51:33 -0200, Nicolas Alvarez wrote:
> Yes, and you can also pass multiple arguments to 'echo' (ie. what Warp
> said, but with a comma instead of the concat operator). It's probably a
> millisecond faster to do it that way too.
>
> http://blog.libssh2.org/index.php?/archives/28-How-long-is-a-piece-of-string.html
Oh, already posted. :o
--
Joel Yliluoma - http://iki.fi/bisqwit/
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
On Wed, 06 Feb 2008 11:52:41 +0000, Invisible wrote:
> Invisible wrote:
>> I just learned PHP. I feel unclean...
>
> Ooo... POST data has all single-quote characters escaped by prefixing
> with a backslash, and PHP doesn't unescape them before giving the string
> back to you. That's a nice feature.
\"Magic quotes\". Extremely annoying. Disable at sight.
--
Joel Yliluoma - http://iki.fi/bisqwit/
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |